Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27702 | Accepted: 8001 |
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.
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
151 42 68 103 47 10
Sample Output
4
大致题意:
有一面墙,被等分为1QW份,一份的宽度为一个单位宽度。现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW。后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报。现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到。)
解题思路:
首先将输入的那些坐标离散化,然后再建点树,之后对每个元素进行插入,之后计算海报张数,这里用到一个ans数组,之后计算ans数组中为1的数据的个数即是海报的张数
1 /* 2 解题思路: 3 首先将输入的那些坐标离散化,然后再建点树,之后对每个元素进行插入,之后计算海报张数,这里用到一个ans数组, 4 之后计算ans数组中为1的数据的个数即是海报的张数 5 */ 6 #include7 #include 8 #include 9 #define le 20005 10 11 typedef struct 12 { 13 int b,e,c; 14 }ore; 15 ore d[4*le]; 16 17 int ar[le][2],br[le],ans[le/2]; 18 int n,len; 19 20 int cmp(const void *a,const void *b) 21 { 22 return *(int *)a>*(int *)b?1:-1; 23 } 24 25 void input() 26 { 27 int i,k; 28 scanf("%d",&n); 29 for(i=k=1;i<=n;i++,k+=2) 30 { 31 scanf("%d%d",&ar[i][0],&ar[i][1]); 32 br[k]=ar[i][0]; br[k+1]=ar[i][1]; 33 } 34 } 35 36 void discretize() 37 { 38 int i,m=2*n; 39 for(i=2,len=1;i<=m;i++) 40 if(br[i]!=br[len]) 41 br[++len]=br[i]; 42 } 43 44 void build(int lt,int rt,int v) 45 { 46 int mid=(lt+rt)/2; 47 d[v].b=lt; 48 d[v].e=rt; 49 d[v].c=0; 50 if(lt==rt) 51 return ; 52 build(lt,mid,2*v); 53 build(mid+1,rt,2*v+1); 54 } 55 56 int binary_search(int x) 57 { 58 int left=1,right=len,mid; 59 while(left<=right) 60 { 61 mid=(left+right)>>1; 62 if(br[mid]==x) 63 return mid; 64 else if(br[mid] 0) 81 { 82 d[2*v].c=d[2*v+1].c=d[v].c; 83 d[v].c=0; 84 } 85 mid=(d[v].b+d[v].e)>>1; 86 if(rt<=mid) 87 insert(lt,rt,c,2*v); 88 else if(lt>mid) 89 insert(lt,rt,c,2*v+1); 90 else 91 { 92 insert(lt,mid,c,2*v); 93 insert(mid+1,rt,c,2*v+1); 94 } 95 } 96 97 void query(int lt,int rt,int v) 98 { 99 int mid=(lt+rt)/2;100 if(d[v].c>0)101 {102 ans[d[v].c]=1;103 return ;104 }105 if(lt==rt)106 {107 ans[d[v].c]=1;108 return ;109 }110 query(lt,mid,2*v);111 query(mid+1,rt,2*v+1);112 }113 114 void deal()115 {116 int i,left,right,sum=0;117 qsort(br+1,2*n,sizeof(br[0]),cmp);118 discretize();119 build(1,len,1);120 for(i=1;i<=n;i++)121 {122 left=binary_search(ar[i][0]);123 right=binary_search(ar[i][1]);124 insert(left,right,i,1);125 }126 memset(ans,0,sizeof(ans));127 query(1,len,1);128 for(i=1;i<=n;i++)129 if(ans[i]==1)130 sum++;131 printf("%d\n",sum);132 }133 134 int main(void)135 {136 int t;137 scanf("%d",&t);138 while(t--)139 {140 input();141 deal();142 }143 return 0;144 }