Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26882 | Accepted: 7767 |
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:
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 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.
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 4][2 6][8 10][3 4][7 10] 将他们排序,并且讲重复的数字去掉就成为1 2 3 4 6 7 8 10
然后 1 2 3 4 6 7 8 10
对应 1 2 3 4 5 6 7 8 所以 之前的范围变成[1 4][2 5][7 8][3 4][6 8] 这样就不会超时了。
之后就是线段数的所发了,每次改变一个区域的color ,最后统计color 不同的个数。
代码:
View Code
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <algorithm> 6 using namespace std; 7 8 const int M=20005; 9 struct Node{ 10 int right,left; 11 int color; 12 }tree[M*4]; //离散后的线段数 13 14 struct N{ 15 int li,num; //num表示第num张海报 16 }line[M]; 17 int set[M][2]; //set[][0]表示海报的左侧,set[][1]表示海报的右侧 18 bool vis[M]; //统计用 19 int ans; 20 int n,t; 21 22 void build(int ll,int rr,int root) 23 { 24 tree[root].left=ll; 25 tree[root].right=rr; 26 tree[root].color=0; 27 if(ll!=rr) 28 { 29 int mid=(ll+rr)/2; 30 build(ll,mid,root*2); 31 build(mid+1,rr,root*2+1); 32 } 33 } 34 35 void insert(int ll ,int rr, int root, int color) 36 { 37 if(tree[root].left==ll && tree[root].right==rr) 38 { 39 tree[root].color=color; 40 return ; 41 } 42 if(tree[root].color>0 && tree[root].color!=color) 43 { 44 tree[root*2].color=tree[root].color; 45 tree[root*2+1].color=tree[root].color; 46 tree[root].color=0; 47 } 48 int mid=(tree[root].left+tree[root].right)/2; 49 if(rr<=mid) insert(ll,rr,root*2,color); 50 else if(ll>mid) insert(ll,rr,root*2+1,color); 51 else 52 { 53 insert(ll,mid,root*2,color); 54 insert(mid+1,rr,root*2+1,color); 55 } 56 } 57 58 void query(int root) 59 { 60 if(tree[root].color!=0) 61 { 62 if(vis[tree[root].color]==0) 63 { 64 vis[tree[root].color]=true; 65 ans++; 66 } 67 return ; 68 } 69 query(2*root); 70 query(2*root+1); 71 } 72 73 74 int cmp(N a,N b) 75 { 76 return a.li<b.li; 77 } 78 79 int main() 80 { 81 int T; 82 while(scanf("%d",&T)!=EOF) 83 { 84 int i; 85 86 while(T--) 87 { 88 scanf("%d",&n); 89 for(i=0;i<n;i++) //离散化,参考的。 90 { 91 scanf("%d%d",&set[i][0],&set[i][1]); 92 line[2*i].li=set[i][0]; 93 line[2*i].num=-(i+1); 94 line[2*i+1].li=set[i][1]; 95 line[2*i+1].num=i+1; 96 } 97 sort(line,line+2*n,cmp); 98 int temp=line[0].li,num=1; 99 for(i=0;i<2*n;i++) 100 { 101 if(line[i].li!=temp) 102 { 103 num++; 104 temp=line[i].li; 105 } 106 if(line[i].num<0) 107 { 108 set[-line[i].num-1][0]=num; 109 } 110 else 111 { 112 set[line[i].num-1][1]=num; 113 } 114 115 } //离散化 116 117 build(1,num,1); 118 for(i=0;i<n;i++) 119 { 120 insert(set[i][0],set[i][1],1,i+1); 121 } 122 memset(vis,0,sizeof(vis)); 123 ans=0; 124 query(1); 125 printf("%d\n",ans); 126 } 127 } 128 return 0; 129 }