Mayor‘s posters
Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVA. Original ID: 10587
64-bit integer IO format: %lld Java class name: Main
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.
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.
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
Output for sample input
4 解题:线段树+离散化。挂了几次,居然还有贴在10-10这样位置的数据,简直太疯狂了。。这能贴么,一个点啊!好吧,改正后,终于Ac 了。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #include <map> 14 #define LL long long 15 #define pii pair<int,int> 16 #define INF 0x3f3f3f3f 17 using namespace std; 18 const int maxn = 100100; 19 set<int>st; 20 int a[maxn],b[maxn]; 21 struct node{ 22 int lt,rt,flag; 23 }; 24 node tree[maxn<<2]; 25 int lisan[maxn<<2]; 26 void build(int lt,int rt,int v){ 27 tree[v].lt = lt; 28 tree[v].rt = rt; 29 tree[v].flag = 0; 30 if(lt + 1 == rt) return; 31 int mid = (lt+rt)>>1; 32 build(lt,mid,v<<1); 33 build(mid,rt,v<<1|1); 34 } 35 void update(int lt,int rt,int v,int val){ 36 if(lisan[tree[v].lt] == lt && lisan[tree[v].rt] == rt){ 37 tree[v].flag = val; 38 return; 39 } 40 if(tree[v].flag){ 41 tree[v<<1].flag = tree[v<<1|1].flag = tree[v].flag; 42 tree[v].flag = 0; 43 } 44 int mid = (tree[v].lt+tree[v].rt)>>1; 45 if(rt <= lisan[mid]){ 46 update(lt,rt,v<<1,val); 47 }else if(lt >= lisan[mid]){ 48 update(lt,rt,v<<1|1,val); 49 }else{ 50 update(lt,lisan[mid],v<<1,val); 51 update(lisan[mid],rt,v<<1|1,val); 52 } 53 } 54 void query(int v){ 55 if(tree[v].flag){ 56 if(!st.count(tree[v].flag)) st.insert(tree[v].flag); 57 return; 58 } 59 if(tree[v].lt+1 == tree[v].rt) return; 60 query(v<<1); 61 query(v<<1|1); 62 } 63 int main() { 64 int t,i,j,n,cnt,tot; 65 scanf("%d",&t); 66 while(t--){ 67 tot = 1; 68 scanf("%d",&n); 69 for(i = 1; i <= n; i++){ 70 scanf("%d %d",a+i,b+i); 71 if(a[i] > b[i]) swap(a[i],b[i]); 72 lisan[tot++] = a[i]; 73 lisan[tot++] = ++b[i]; 74 } 75 sort(lisan+1,lisan+tot); 76 cnt = 1; 77 for(i = 2; i < tot; i++){ 78 if(lisan[i] == lisan[cnt]) continue; 79 lisan[++cnt] = lisan[i]; 80 } 81 build(1,cnt,1); 82 for(i = 1; i <= n; i++) update(a[i],b[i],1,i); 83 st.clear(); 84 query(1); 85 printf("%d\n",st.size()); 86 } 87 return 0; 88 }
BNUOJ 2528 Mayor's posters