Test instructions: Give some points on the plane, and then give some queries (x, y), that is, to set the coordinate system for the origin (x, y), a person takes the point of the I,III quadrant, and the other person take the Ii,iv quadrant, the point will not be on any one of the query axes, ask each time the difference between two people.
Solution: Offline tree-like array. Point is not on the axis, that is, the point is not collinear so that the problem is a lot simpler, can be discretized point, also can not be discretized, because X, y <= 500000, directly can engage. I am discrete here, in fact, there is no faster than direct.
See two tree-like arrays, one modify all points in, one waiting to add elements.
The query and the given points are then sorted in y-coordinates, and then the actions are taken offline for each query. Adds a tree-like array of points with a y-coordinate less than the current query point before each query.
At this point the lower left corner points are: LD = Getsum (c2,q[i].x-1);
upper right corner: UR = Getsum (C1,maxi)-getsum (c1,q[i].x)-(Getsum (C2,maxi)-getsum (c2,q[i].x)); That is, the number of the entire right minus the y-coordinate is less than this point (that is, the lower-right corner).
Then the review of the other two quadrants is N-ld-ur.
This will solve the problem.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;#defineN 100007structnode{intX,y,ind;} P[n],q[n];intN,m,maxi;intC1[n],c2[n],a[n],b[n],ans[n];intma[500008],mb[500008];intLowbit (intx) {returnx&-x;}intCMP (Node Ka,node KB) {returnKa.y <kb.y;}voidModifyint*c,intXintval) { while(x <=Maxi) c[x]+ = val, x + =lowbit (x);}intGetsum (int*c,intx) { intres =0; while(X >0) {res + = c[x]; x-=lowbit (x);} returnRes;}intMain () {inti,j,t; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(i=1; i<=n;i++) {scanf ("%d%d",&p[i].x,&p[i].y); A[i]= p[i].x, B[i] =p[i].y; } for(i=1; i<=m;i++) {scanf ("%d%d",&q[i].x,&q[i].y); A[i+n] = q[i].x, b[i+n] =q[i].y; Q[i].ind=i; } sort (A+1, a+n+m+1); Sort (b+1, b+n+m+1); intInda = Unique (A +1, a+n+m+1)-a-1; intIndb = Unique (b +1, b+n+m+1)-b-1; Maxi=Max (INDA,INDB); for(i=1; i<=inda;i++) Ma[a[i]] =i; for(i=1; i<=indb;i++) Mb[b[i]] =i; for(i=1; i<=n;i++) p[i].x = ma[p[i].x], p[i].y =MB[P[I].Y]; for(i=1; i<=m;i++) q[i].x = ma[q[i].x], q[i].y =MB[Q[I].Y]; Sort (P+1, p+n+1, CMP); Sort (Q+1, q+m+1, CMP); memset (C1,0,sizeof(C1)); memset (C2,0,sizeof(C2)); for(i=1; i<=n;i++) Modify (c1,p[i].x,1); J=1; for(i=1; i<=m;i++) { while(j <= N && p[j].y <=q[i].y) Modify (c2,p[j].x,1), J + +; intLD = Getsum (c2,q[i].x-1); intUR = Getsum (C1,maxi)-getsum (c1,q[i].x)-(Getsum (C2,maxi)-getsum (c2,q[i].x)); Ans[q[i].ind]= ABS (2* (Ld+ur)-N); } for(i=1; i<=m;i++) printf ("%d\n", Ans[i]); if(T >=1) puts (""); } return 0;}
View Code
Directly engage in discrete code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;#defineN 500007structnode{intX,y,ind;} P[n],q[n];intN,m,maxi;intC1[n],c2[n],a[n],b[n],ans[n];intLowbit (intx) {returnx&-x;}intCMP (Node Ka,node KB) {returnKa.y <kb.y;}voidModifyint*c,intXintval) { while(x <=Maxi) c[x]+ = val, x + =lowbit (x);}intGetsum (int*c,intx) { intres =0; while(X >0) {res + = c[x]; x-=lowbit (x);} returnRes;}intMain () {inti,j,t; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); for(i=1; i<=n;i++) {scanf ("%d%d",&p[i].x,&p[i].y); p[i].x+ +, p[i].y++; Maxi=Max (maxi,p[i].x); } for(i=1; i<=m;i++) {scanf ("%d%d",&q[i].x,&q[i].y); q[i].x+ +, q[i].y++; Q[i].ind=i; Maxi=Max (maxi,q[i].x); } sort (P+1, p+n+1, CMP); Sort (Q+1, q+m+1, CMP); memset (C1,0,sizeof(C1)); memset (C2,0,sizeof(C2)); for(i=1; i<=n;i++) Modify (c1,p[i].x,1); J=1; for(i=1; i<=m;i++) { while(j <= N && p[j].y <=q[i].y) Modify (c2,p[j].x,1), J + +; intLD = Getsum (c2,q[i].x-1); intUR = Getsum (C1,maxi)-getsum (c1,q[i].x)-(Getsum (C2,maxi)-getsum (c2,q[i].x)); Ans[q[i].ind]= ABS (2* (Ld+ur)-N); } for(i=1; i<=m;i++) printf ("%d\n", Ans[i]); if(T >=1) puts (""); } return 0;}
View Code
POJ 3416 Crossing--offline + tree-like array