标签:
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int flag; 6 struct p 7 { 8 int x,d; 9 };p s[24000]; 10 bool com1(p A,p B) 11 { 12 return A.x<B.x; 13 } 14 bool com2(p A,p B) 15 { 16 if (A.d==B.d) return A.x<B.x; 17 return A.d>B.d; 18 } 19 struct pp 20 { 21 int l,r,v; 22 };pp tree[1000000]; 23 void build(int l,int r,int p) 24 { 25 tree[p].l=l; 26 tree[p].r=r; 27 tree[p].v=0; 28 if (l==r) return ; 29 int m=(l+r)/2; 30 build(l,m,2*p); 31 build(m+1,r,2*p+1); 32 return ; 33 } 34 void find(int l,int r,int p) 35 { 36 if (tree[p].v) return ; 37 if (tree[p].l==l&&tree[p].r==r) 38 { 39 flag=1; 40 tree[p].v=1; 41 return ; 42 } 43 int m=(tree[p].l+tree[p].r)/2; 44 if (l>m) find(l,r,2*p+1); 45 else if (m>=r) find (l,r,2*p); 46 else 47 { 48 find(l,m,2*p); 49 find(m+1,r,2*p+1); 50 } 51 tree[p].v=tree[2*p].v&&tree[2*p+1].v; 52 return ; 53 } 54 int main() 55 { 56 int c,n,i,j,ans; 57 scanf("%d",&c); 58 while (c--) 59 { 60 scanf("%d",&n); 61 for (i=1;i<=2*n;i+=2) 62 { 63 scanf("%d%d",&s[i].x,&s[i+1].x); 64 s[i].d=s[i+1].d=i; 65 } 66 sort (s+1,s+2*n+1,com1); 67 int p1=0,p2=0; 68 for (i=1;i<=2*n;i++) 69 { 70 if (s[i].x==p1) 71 s[i].x=p2; 72 else 73 { 74 p1=s[i].x; 75 s[i].x=++p2; 76 } 77 // printf("**%d**\n",s[i].x); 78 } 79 build(1,2*n,1); 80 ans=0; 81 sort(s+1,s+2*n+1,com2); 82 for (i=1;i<=2*n;i+=2) 83 { 84 flag=0; 85 find(s[i].x,s[i+1].x,1); 86 // printf("**%d***%d**\n",s[i].x,s[i+1].x); 87 if (flag) ans++; 88 } 89 printf("%d\n",ans); 90 } 91 return 0; 92 }
PO J 2528 Mayor's posters (线段树+离散化)
标签:
原文地址:http://www.cnblogs.com/pblr/p/4737715.html