Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39795 | Accepted: 11552 |
Description
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.Output
For each input data set print the number of visible posters after all the posters are placed.Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
6
5
1 4
2 6
8 10
3 4
7 10
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10
4
2 4
3 5
1 3
5 7
3
1 10
1 4
5 10
ans:
4
2
3
4
3
2
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 10000002
#define maxn 10002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
bool tree[maxn << 4];
struct Node{
int l, r;
} post[maxn];
int hash[inf], tmp[maxn << 1];
bool update(int left, int right, int l, int r, int rt)
{
if(tree[rt]) return false;
if(left == l && right == r){
return tree[rt] = true;
}
bool rst, a, b;
int mid = (l + r) >> 1;
if(right <= mid) rst = update(left, right, lson);
else if(left > mid) rst = update(left, right, rson);
else {
a = update(left, mid, lson);
b = update(mid + 1, right, rson);
rst = a || b;
}
tree[rt] = tree[rt << 1] && tree[rt << 1 | 1];
return rst;
}
int main()
{
//freopen("stdin.txt", "r", stdin);
int t, n, i, id, num, count, ans;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(i = id = 0; i < n; ++i){
scanf("%d%d", &post[i].l, &post[i].r);
tmp[id++] = post[i].l;
tmp[id++] = post[i].r;
}
sort(tmp, tmp + id);
num = unique(tmp, tmp + id) - tmp;
for(i = count = 0; i < num; ++i){
hash[tmp[i]] = count;
if(i < num - 1){
if(tmp[i] + 1 == tmp[i+1]) ++count;
else count += 2;
}
}
memset(tree, 0, sizeof(tree));
for(i = n - 1, ans = 0; i >= 0; --i){
if(update(hash[post[i].l], hash[post[i].r], 0, count, 1))
++ans;
}
printf("%d\n", ans);
}
return 0;
}
//#define DEBUG
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define maxn 10002
using std::sort;
int hash[maxn << 2], vis[maxn << 2], visColor[maxn], ans;
int tree[maxn << 4], ori[maxn << 1], idHash, idVis, idOri;
int findHash(int n)
{
int left = 0, right = idHash - 1, mid;
while(left <= right){
mid = (left + right) >> 1;
if(n < hash[mid]) right = mid - 1;
else if(n > hash[mid]) left = mid + 1;
else return mid;
}
}
void pushDown(int rt)
{
tree[rt << 1] = tree[rt << 1 | 1] = tree[rt];
tree[rt] = -1;
}
void build(int l, int r, int rt)
{
tree[rt] = -1;
if(r == l) return;
int mid = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int left, int right, int val, int l, int r, int rt)
{
if(left == l && right == r){
tree[rt] = val; return;
}
if(tree[rt] != -1) pushDown(rt);
int mid = (l + r) >> 1;
if(right <= mid) update(left, right, val, lson);
else if(left > mid) update(left, right, val, rson);
else{
update(left, mid, val, lson);
update(mid + 1, right, val, rson);
}
}
void query(int l, int r, int rt)
{
if(tree[rt] != -1){
if(!visColor[tree[rt]]){
++ans; visColor[tree[rt]] = 1;
}
return;
} //最后延迟段必定覆盖所有ori区间点,因为映射的每个点都被用到
if(l == r) return;
int mid = (l + r) >> 1;
query(lson);
query(rson);
}
int main()
{
#ifdef DEBUG
freopen("stdin.txt", "r", stdin);
freopen("stdout.txt", "w", stdout);
#endif
int cas, n, a, b, i, temp;
scanf("%d", &cas);
while(cas--){
scanf("%d", &n);
for(i = idVis = idOri = 0; i < n; ++i){
scanf("%d%d", &a, &b);
ori[idOri++] = a; ori[idOri++] = b;
vis[idVis++] = a; vis[idVis++] = b;
}
sort(vis, vis + idVis); //temporary
for(temp = idVis, i = 1; i < temp; ++i)
if(vis[i] - vis[i - 1] > 1) vis[idVis++] = vis[i] - 1;
sort(vis, vis + idVis);
hash[0] = vis[0];
for(i = idHash = 1; i < idVis; ++i)
if(vis[i] != vis[i - 1]) hash[idHash++] = vis[i];
for(i = 0; i < idOri; ++i)
ori[i] = findHash(ori[i]);
build(0, idHash - 1, 1); //映射区间
memset(visColor, 0, sizeof(visColor));
for(i = 0; i < n; ++i){
update(ori[i << 1], ori[i << 1 | 1], i, 0, idHash - 1, 1);
}
ans = 0; query(0, idHash - 1, 1);
printf("%d\n", ans);
}
return 0;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。