1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 struct data 6 { 7 int x,y,k,r; 8 }a[1000008]; 9 struct dat 10 { 11 int x,y; 12 }b[1000008]; 13 bool cmp(dat a1,dat a2) 14 { 15 if(a1.x==a2.x) 16 return a1.y<a2.y; 17 return a1.x<a2.x; 18 } 19 int n,h[100008],h1[100009],shu[1000008],cnt,ans; 20 void jia1(int a1,int a2) 21 { 22 cnt++; 23 a[cnt].x=lower_bound(h+1,h+h[0]+1,b[a1].x)-h; 24 a[cnt].y=b[a1].y; 25 a[cnt].k=1; 26 cnt++; 27 a[cnt].x=a[cnt-1].x; 28 a[cnt].y=b[a2].y; 29 a[cnt].k=-1; 30 } 31 bool cmp1(dat a1,dat a2) 32 { 33 if(a1.y==a2.y) 34 return a1.x<a2.x; 35 return a1.y<a2.y; 36 } 37 void jia2(int a1,int a2) 38 { 39 cnt++; 40 a[cnt].y=b[a1].y; 41 a[cnt].x=lower_bound(h+1,h+h[0]+1,b[a1].x)-h; 42 a[cnt].r=lower_bound(h+1,h+h[0]+1,b[a2].x)-h; 43 } 44 bool cmp2(data a1,data a2) 45 { 46 if(a1.y==a2.y) 47 return a1.k<a2.k; 48 return a1.y<a2.y; 49 } 50 int zhao(int a1) 51 { 52 int sum=0; 53 for(;a1;a1-=a1&-a1) 54 sum+=shu[a1]; 55 return sum; 56 } 57 void gai(int a1,int a2) 58 { 59 for(;a1<=h[0];a1+=a1&-a1) 60 shu[a1]+=a2; 61 return; 62 } 63 int main() 64 { 65 scanf("%d",&n); 66 for(int i=1;i<=n;i++) 67 scanf("%d%d",&b[i].x,&b[i].y); 68 for(int i=1;i<=n;i++) 69 h1[i]=b[i].x; 70 sort(h1+1,h1+1+n); 71 h[1]=h1[1]; 72 h[0]=1; 73 for(int i=2;i<=n;i++) 74 if(h1[i]!=h1[i-1]) 75 { 76 h[0]++; 77 h[h[0]]=h1[i]; 78 } 79 sort(b+1,b+n+1,cmp); 80 for(int i=2;i<=n;i++) 81 if(b[i].x==b[i-1].x) 82 jia1(i-1,i); 83 sort(b+1,b+n+1,cmp1); 84 for(int i=2;i<=n;i++) 85 if(b[i].y==b[i-1].y) 86 jia2(i-1,i); 87 sort(a+1,a+cnt+1,cmp2); 88 for(int i=1;i<=cnt;i++) 89 if(a[i].k==0) 90 ans+=zhao(a[i].r-1)-zhao(a[i].x); 91 else 92 gai(a[i].x,a[i].k); 93 printf("%d",ans+n); 94 return 0; 95 }
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 struct data 6 { 7 int x,y,k,r; 8 }a[1000008]; 9 struct dat 10 { 11 int x,y; 12 }b[1000008]; 13 bool cmp(dat a1,dat a2) 14 { 15 if(a1.x==a2.x) 16 return a1.y<a2.y; 17 return a1.x<a2.x; 18 } 19 int n,h[100008],h1[100009],shu[1000008],cnt,ans; 20 void jia1(int a1,int a2) 21 { 22 cnt++; 23 a[cnt].x=lower_bound(h+1,h+h[0]+1,b[a1].x)-h; 24 a[cnt].y=b[a1].y; 25 a[cnt].k=1; 26 cnt++; 27 a[cnt].x=a[cnt-1].x; 28 a[cnt].y=b[a2].y; 29 a[cnt].k=-1; 30 } 31 bool cmp1(dat a1,dat a2) 32 { 33 if(a1.y==a2.y) 34 return a1.x<a2.x; 35 return a1.y<a2.y; 36 } 37 void jia2(int a1,int a2) 38 { 39 cnt++; 40 a[cnt].y=b[a1].y; 41 a[cnt].x=lower_bound(h+1,h+h[0]+1,b[a1].x)-h; 42 a[cnt].r=lower_bound(h+1,h+h[0]+1,b[a2].x)-h; 43 } 44 bool cmp2(data a1,data a2) 45 { 46 if(a1.y==a2.y) 47 return a1.k<a2.k; 48 return a1.y<a2.y; 49 } 50 int zhao(int a1) 51 { 52 int sum=0; 53 for(;a1;a1-=a1&-a1) 54 sum+=shu[a1]; 55 return sum; 56 } 57 void gai(int a1,int a2) 58 { 59 for(;a1<=h[0];a1+=a1&-a1) 60 shu[a1]+=a2; 61 return; 62 } 63 int main() 64 { 65 scanf("%d",&n); 66 for(int i=1;i<=n;i++) 67 scanf("%d%d",&b[i].x,&b[i].y); 68 for(int i=1;i<=n;i++) 69 h1[i]=b[i].x; 70 sort(h1+1,h1+1+n); 71 h[1]=h1[1]; 72 h[0]=1; 73 for(int i=2;i<=n;i++) 74 if(h1[i]!=h1[i-1]) 75 { 76 h[0]++; 77 h[h[0]]=h1[i]; 78 } 79 sort(b+1,b+n+1,cmp); 80 for(int i=2;i<=n;i++) 81 if(b[i].x==b[i-1].x) 82 jia1(i-1,i); 83 sort(b+1,b+n+1,cmp1); 84 for(int i=2;i<=n;i++) 85 if(b[i].y==b[i-1].y) 86 jia2(i-1,i); 87 sort(a+1,a+cnt+1,cmp2); 88 for(int i=1;i<=cnt;i++) 89 if(a[i].k==0) 90 ans+=zhao(a[i].r-1)-zhao(a[i].x); 91 else 92 gai(a[i].x,a[i].k); 93 printf("%d",ans+n); 94 return 0; 95 }
将点拆成横线与点,用树状数组维护。