Mayor's posters poj 2528

 

 

View Code
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=100005;
 7 bool Hash[maxn];
 8 int li[maxn],ri[maxn];
 9 int num[maxn*3],spt[maxn<<4];
10 int cnt;
11 int GotNum(int key,int n){
12     int l=0,r=n-1;
13     while(l<=r){
14         int mid=(l+r)>>1;
15         if(num[mid]==key)return mid;
16         if(key<num[mid])r=mid-1;
17         else l=mid+1;
18     }
19     return -1;
20 }
21 void PushDown(int pos){
22     spt[pos*2]=spt[pos];
23     spt[pos*2+1]=spt[pos];
24     spt[pos]=0;
25 }
26 void Updata(int l,int r,int c,int left,int right,int pos){
27     if(l<=left&&right<=r){
28         spt[pos]=c;
29         return ;
30     }
31     if(spt[pos])PushDown(pos);
32     int mid=(left+right)>>1;
33     if(r<=mid)Updata(l,r,c,left,mid,pos*2);
34     else if(l>mid)Updata(l,r,c,mid+1,right,pos*2+1);
35     else{
36         Updata(l,r,c,left,mid,pos*2);
37         Updata(l,r,c,mid+1,right,pos*2+1);
38     }
39 }
40 void Query(int l,int r,int pos){
41     if(spt[pos]){
42         if(!Hash[spt[pos]]){
43             cnt++;
44             Hash[spt[pos]]=true;
45         }
46         return;
47     }
48     if(l==r)return;
49     int mid=(l+r)>>1;
50     Query(l,mid,pos*2);
51     Query(mid+1,r,pos*2+1);
52 }
53 int main()
54 {
55     int T,n;
56     scanf("%d",&T);
57     while(T--){
58         scanf("%d",&n);
59         int mn=0;
60         for(int i=0;i<n;i++){
61             scanf("%d %d",&li[i],&ri[i]);
62             num[mn++]=li[i];
63             num[mn++]=ri[i];
64         }
65         sort(num,num+mn);
66         int all=1;
67         for(int i=1;i<mn;i++)
68             if(num[i]!=num[i-1])
69                 num[all++]=num[i];
70         for(int i=all-1;i>0;i--)
71             if(num[i]!=num[i-1]+1)
72                 num[all++]=num[i-1]+1;
73         sort(num,num+all);
74         memset(spt,0,sizeof(spt));//建树
75         for(int i=0;i<n;i++){
76             int l=GotNum(li[i],all);
77             int r=GotNum(ri[i],all);
78             Updata(l,r,i+1,0,all-1,1);
79         }
80         cnt=0;
81         memset(Hash,false,sizeof(Hash));
82         Query(0,all-1,1);
83         printf("%d\n",cnt);
84     }
85     return 0;
86 }

 

posted on 2012-10-17 23:36  Acmer_Roney  阅读(157)  评论(0编辑  收藏  举报

导航