题意是给你n张海报,告诉你每张海报的宽度和先后顺序,海报会重叠,问你露在外面的海报有多少张?这题主要是离散化理解了好久,关键在于建hash表时不能选择最普通的一一对应,为什么?看了网上一组数据后瞬间就明白了:1,10 1,4 6,10。
Mayor‘s posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 51347 | Accepted: 14875 |
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 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.
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<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 int n; 8 int ncount=0; 9 struct post 10 { 11 int l,r; 12 }posters[10100]; 13 int x[20200]; 14 int hash[10000010]; 15 struct node 16 { 17 int l,r; 18 bool flag; 19 int mid() 20 { 21 return (l+r)/2; 22 } 23 }tree[1000000]; 24 void buildtree(int root,int l,int r) 25 { 26 tree[root].l=l; 27 tree[root].r=r; 28 tree[root].flag=false; 29 if(l!=r) 30 { 31 buildtree(2*root+1,l,(l+r)/2); 32 buildtree(2*root+2,(l+r)/2+1,r); 33 } 34 } 35 bool judge(int root,int l,int r) 36 { 37 if(tree[root].flag) return false; 38 if(tree[root].l==l&&tree[root].r==r) 39 { 40 tree[root].flag=true; 41 return true; 42 } 43 bool result; 44 if(r<=tree[root].mid()) 45 result=judge(root*2+1,l,r); 46 else if(l>tree[root].mid()) 47 result=judge(root*2+2,l,r); 48 else 49 { 50 bool b1=judge(root*2+1,l,tree[root].mid()); 51 bool b2=judge(root*2+2,tree[root].mid()+1,r); 52 result=b1||b2; 53 } 54 if(tree[root*2+1].flag&&tree[root*2+2].flag) 55 tree[root].flag=true; 56 return result; 57 } 58 int main() 59 { 60 int T,i,j,k,t; 61 cin>>T; 62 while(T--) 63 { 64 cin>>n; 65 ncount=0; 66 for(i=0;i<n;i++) 67 { 68 cin>>posters[i].l>>posters[i].r; 69 x[ncount++]=posters[i].l; 70 x[ncount++]=posters[i].r; 71 } 72 sort(x,x+ncount); 73 ncount=unique(x,x+ncount)-x; 74 int num=0; 75 for(i=0;i<ncount;i++) 76 { 77 hash[x[i]]=num; 78 if(i<ncount-1) 79 { 80 if(x[i+1]-x[i]==1) 81 num++; 82 else 83 num+=2; 84 } 85 } 86 buildtree(0,0,num); 87 int sum=0; 88 for(i=n-1;i>=0;i--) 89 { 90 if(judge(0,hash[posters[i].l],hash[posters[i].r])) 91 sum++; 92 } 93 cout<<sum<<endl; 94 } 95 return 0; 96 }