标签:des style blog http color java os io
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.
1 5 1 4 2 6 8 10 3 4 7 10
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,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/crackpotisback/p/3922355.html