Mayor's posters
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 53838 |
|
Accepted: 15635 |
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:
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
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 l
i 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 <= l
i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l
i, l
i+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
题目大意:把n张海报贴在墙上,问最后墙上有多少张海报可以被看见(为被其他海报覆盖,或者只是部分被覆盖)
解题思路:这里区间长度太大,所以需要离散化.然后遍历每张海报时,从后面开始遍历,查找是否被覆盖,然后更新其父亲节点,当父亲节点的左右子结点都被覆盖,那么父亲节点当然也被覆盖.
代码如下:
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=100010;
int flag;
struct stu{
int l,r,vis;
};
stu node[N<<2];
struct Point{
int x,id;
};
Point p[N<<2];
void PUTUP(int rt){
node[rt].vis=node[rt<<1].vis&&node[rt<<1|1].vis;
}
int cmp1(Point a,Point b){
return a.x<b.x;
}
int cmp2(Point a,Point b){
if(a.id==b.id)
return a.x<b.x;
return a.id>b.id;
}
void build(int l,int r,int rt){
node[rt].l=l;
node[rt].r=r;
node[rt].vis=0;
if(l==r){
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
}
void query(int l,int r,int rt){
if(node[rt].vis)return ;
if(node[rt].l==l&&node[rt].r==r){
node[rt].vis=1;
flag=1;
return ;
}
int mid=(node[rt].l+node[rt].r)>>1;
if(r<=mid)query(l,r,rt<<1);
else if(l>mid)query(l,r,rt<<1|1);
else{
query(l,mid,rt<<1);
query(mid+1,r,rt<<1|1);
}
PUTUP(rt);
}
int main(){
int t,ans,n;
scanf("%d",&t);
while(t--){
ans=0;
scanf("%d",&n);
for(int i=0;i<2*n;i+=2){
scanf("%d%d",&p[i].x,&p[i+1].x);
p[i].id=p[i+1].id=i;
}
sort(p,p+2*n,cmp1);
int rank,pre;
rank=0;pre=0;
for(int i=0;i<2*n;i++){//离散化
if(p[i].x==pre)
p[i].x=rank;
else {
pre=p[i].x;
p[i].x=++rank;
}
}
build(1,rank,1);
sort(p,p+2*n,cmp2);//因为要从后面向前遍历
for(int i=0;i<2*n;i+=2){
int l=p[i].x;
int r=p[i+1].x;
flag=0;
query(l,r,1);//判断是否被覆盖
if(flag){
++ans;
}
}
printf("%d\n",ans);
}
return 0;
}