Mayor's posters (线段树加离散化)
个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下
解法:离散化,如下面的例子(题目的样例),因为单位1是一个单位长度,将下面的
1 2 3 4 6 7 8 10
— — — — — — — —
1 2 3 4 5 6 7 8
离散化 X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[7] = 8; X[8] = 10
于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),最后统计不同颜色的段数。
但是只是这样简单的离散化是错误的,
如三张海报为:1~10 1~4 6~10
离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的1~4被染为1;
第二张海报时:墙的1~2被染为2,3~4仍为1;
第三张海报时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)
X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10
这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3
最终,1~2为2,3为1,4~5为3,于是输出正确结果3。
看题目吧。
- 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
Output
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
1 #include <cstdio> 2 #include <cstring> 3 #include<iostream> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 const int inf=0xffffff0; 8 const int length=1000005; 9 const int hl=20000005; 10 struct tree 11 { 12 int l,r; 13 bool tcover; 14 int mid() 15 { 16 return (l+r)/2; 17 } 18 tree *left,*right; 19 }; 20 tree Tree[length]; 21 struct poster 22 { 23 int l,r; 24 }; 25 poster pos[20005]; 26 int hashb[hl]; 27 int x[length]; 28 int ntree=0; 29 void builttree(tree *t,int l,int r) 30 { 31 t->l=l; 32 t->r=r; 33 t->tcover=false; 34 if(l==r) return ; 35 ntree++; 36 t->left=Tree+ntree; 37 ntree++; 38 t->right=Tree+ntree; 39 builttree(t->left,l,(l+r)/2); 40 builttree(t->right,(l+r)/2+1,r); 41 } 42 bool post(tree *t,int l,int r) 43 { 44 if(t->tcover==true) return false; 45 if(t->l==l&&t->r==r){ 46 t->tcover=true; 47 return true; 48 } 49 bool result; 50 if(r<=t->mid()) 51 result=post(t->left,l,r); 52 else if(l>=t->mid()+1) 53 result=post(t->right,l,r); 54 else 55 { 56 bool b1=post(t->left,l,t->mid()); 57 bool b2=post(t->right,t->mid()+1,r); 58 result=b1||b2; 59 } 60 if(t->left->tcover==true&&t->right->tcover==true) 61 t->tcover=true; 62 return result; 63 64 } 65 int main() 66 { 67 int t; 68 scanf("%d",&t); 69 while(t--){ 70 int n; 71 ntree=0; 72 scanf("%d",&n); 73 int accout=0; 74 for(int i=0;i<n;i++) 75 { 76 scanf("%d%d",&pos[i].l,&pos[i].r); 77 x[accout++]=pos[i].l; 78 x[accout++]=pos[i].r; 79 } 80 sort(x,x+accout); 81 int m=unique(x,x+accout)-x; 82 int treel=0; 83 for(int i=0;i<m;i++) 84 { 85 hashb[x[i]]=treel; 86 if(i<m-1){ 87 if(x[i+1]-x[i]==1) 88 treel++; 89 else treel+=2; 90 } 91 } 92 builttree(Tree,0,treel); 93 int sum=0; 94 for(int i=n-1;i>=0;i--) 95 if(post(Tree,hashb[pos[i].l],hashb[pos[i].r])) 96 sum++; 97 printf("%d\n",sum); 98 99 100 } 101 return 0; 102 }