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:
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.
- 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. //图片粘不上来,一直转圈圈,uva链接,洛谷链接
The picture below illustrates the case of the sample input. //图片粘不上来,一直转圈圈,uva链接,洛谷链接
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
解题思路
按顺序一张张贴上,这就是线段树的区间修改,最后统计时把墙从左到右每个格子扫一遍,用一个桶统计还剩下哪些编号的海报,嗯,没了。
然后,MLE#滑稽,加个离散化。以前写的用map、set去重、离散化的方式效率太低,这次去洛谷上学了个更好用一点的离散化 链接 ,sort+unique+lower_bound(好像这才是别人的标配啊)
然后愉快交题,然后WA了。随便来一组数据——
1 3 1 6
1 3
5 6
离散化以后会发现,3和5之间的空隙4被离散化没了。解决方法——把每张海报结束的下一个编号也离散化一下,就是对每张海报,多离散化一个数据——7、4、7
然后,数组下标小心一点,AC。
源代码
1 #include<stdio.h> 2 #include<algorithm> 3 4 int n,T; 5 6 int post[10010][2]/*海报位置*/,input[30010]/*需要离散化的数*/; 7 8 struct Segtree{ 9 int l,r; 10 int c;//如果区间长度为1,则记录海报编号,否则随缘(记录的啥我不管)(这里好像可以再优化一下,把c弄出去,搞成一个长度1e5的数组) 11 }s[120010];//从1号开始 12 int lazy[120010]; 13 inline int lson(int a){return a<<1;} 14 inline int rson(int a){return (a<<1)|1;} 15 void maketree(int x,int l,int r) 16 { 17 lazy[x]=0; 18 if(l==r) 19 { 20 s[x]={l,r,0}; 21 return; 22 } 23 s[x].l=l; 24 s[x].r=r; 25 int mid=l+r>>1; 26 maketree(lson(x),l,mid); 27 maketree(rson(x),mid+1,r); 28 s[x].c=0; 29 } 30 inline void pushdown(int x) 31 { 32 if(!lazy[x]) return; 33 int ls=lson(x),rs=rson(x); 34 s[ls].c=lazy[x]; 35 lazy[ls]=lazy[x]; 36 s[rs].c=lazy[x]; 37 lazy[rs]=lazy[x]; 38 lazy[x]=0; 39 } 40 int quepos(int x,int pos) 41 { 42 int mid=s[x].l+s[x].r>>1; 43 if(s[x].l==s[x].r) return s[x].c; 44 if(lazy[x]) return lazy[x]; 45 if(pos<=mid) return quepos(lson(x),pos); 46 else return quepos(rson(x),pos); 47 } 48 void update(int x,int l,int r,int k) 49 { 50 if(l>s[x].r||r<s[x].l) return; 51 if(l<=s[x].l&&s[x].r<=r) 52 { 53 // s[x].sum+=k*(s[x].r-s[x].l+1); 54 if(s[x].l==s[x].r) s[x].c=k; 55 else lazy[x]=k; 56 return; 57 } 58 pushdown(x); 59 update(lson(x),l,r,k); 60 update(rson(x),l,r,k); 61 } 62 63 int main() 64 { 65 //freopen("test.in","r",stdin);//因为忘记注释这个,WA了不知多少 66 scanf("%d",&T); 67 while(T--) 68 { 69 scanf("%d",&n); 70 int len=0;//离散化数组input的长度 71 for(int i=1;i<=n;i++) 72 { 73 scanf("%d%d",&post[i][0],&post[i][1]); 74 input[len++]=post[i][0]; 75 input[len++]=post[i][1]; 76 input[len++]=post[i][1]+1; 77 } 78 79 std::sort(input,input+len+1); 80 len=std::unique(input,input+len+1)-input; 81 for(int i=1;i<=n;i++) 82 { 83 post[i][0]=std::lower_bound(input,input+len,post[i][0])-input; 84 post[i][1]=std::lower_bound(input,input+len,post[i][1])-input; 85 } 86 87 maketree(1,0,len); 88 for(int i=1;i<=n;i++) 89 update(1,post[i][0],post[i][1],i);//海报编号1~n 90 int *count; 91 count=new int[n](); 92 for(int i=0;i<=len;i++) 93 count[quepos(1,i)]=1; 94 95 int ans=0; 96 for(int i=1;i<=n;i++) ans+=count[i]; 97 delete count; 98 printf("%d ",ans); 99 } 100 return 0; 101 }