First, rotate the coordinate system to convert the range that each point can receive into a square.
The k-d tree is then established and the location of each point on the k-d tree is recorded.
For inquiries using the MO team algorithm, modify $o (\log N) $, query the expected $o (\log N) $.
Total complexity $o (n\sqrt{n}\log N) $.
#include <cstdio> #include <cmath> #include <algorithm>const int n=200010,m=10010;int t,n,m,d,i,x,y, Id[n],root,cmp_d,x1,x2,y1,y2,lim,l,r,k;struct p{int x, y;} A[n];struct q{int L,r,id;} Q[m];long Long now,ans[m];inline bool CMPQ (const Q&a,const q&b) {return a.l/lim<b.l/lim| | A.L/LIM==B.L/LIM&&A.R<B.R;} struct Node{int d[2],l,r,max[2],min[2],val,sum,f;} t[n];inline BOOL CMP (const Node&a,const node&b) {return a.d[cmp_d]<b.d[cmp_d];} inline void Umax (Int&a,int b) {if (a<b) a=b;} inline void Umin (Int&a,int b) {if (a>b) a=b;} inline void up (int x) {if (T[X].L) {UMAX (t[x]. MAX[0],T[T[X].L]. Max[0]); Umin (T[x]. MIN[0],T[T[X].L]. Min[0]); UMAX (T[x]. MAX[1],T[T[X].L]. MAX[1]); Umin (T[x]. MIN[1],T[T[X].L]. MIN[1]); } if (T[X].R) {UMAX (t[x]. MAX[0],T[T[X].R]. Max[0]); Umin (T[x]. MIN[0],T[T[X].R]. Min[0]); UMAX (T[x]. MAX[1],T[T[X].R]. MAX[1]); Umin (T[x]. MIN[1],T[T[X].R]. MIN[1]); }}int Build (int l,int r,int d,int f) {int mid= (L+R) >>1; CMp_d=d,std::nth_element (T+L+1,T+MID+1,T+R+1,CMP); Id[t[mid].f]=mid; T[mid].f=f; T[mid]. Max[0]=t[mid]. MIN[0]=T[MID].D[0]; T[mid]. Max[1]=t[mid]. MIN[1]=T[MID].D[1]; t[mid].val=t[mid].sum=0; if (l!=mid) T[mid].l=build (l,mid-1,! D,MID); else t[mid].l=0; if (r!=mid) T[mid].r=build (mid+1,r,! D,MID); else t[mid].r=0; Return up (mid), mid;} inline void Change (int. X,int p) {for (T[X].VAL+=P;X;X=T[X].F) t[x].sum+=p;} void ask (int x) {if (t[x]. min[0]>x2| | T[X]. max[0]<x1| | T[X]. min[1]>y2| | T[X]. max[1]<y1| |! T[x].sum) return; if (T[x]. MIN[0]>=X1&&T[X]. MAX[0]<=X2&&T[X]. MIN[1]>=Y1&&T[X]. Max[1]<=y2) {K+=t[x].sum;return;} if (t[x].d[0]>=x1&&t[x].d[0]<=x2&&t[x].d[1]>=y1&&t[x].d[1]<=y2) K+=t[x].val; if (T[X].L) ask (T[X].L); if (T[X].R) ask (T[X].R);} inline void Add (int x) {x1=a[x].x-d,x2=a[x].x+d,y1=a[x].y-d,y2=a[x].y+d; K=0,ask (root), now+=k; Change (id[x],1);} inline void del (int x) {change (id[x],-1); x1=a[x].x-d,x2=a[x].x+d,y1=a[x].y-D,y2=a[x].y+d; K=0,ask (root), now-=k;} int main () {while (~scanf ("%d%d%d", &n,&d,&m)) {printf ("Case%d:\n", ++t); lim= (int) std::sqrt (n+0.5); for (i=1;i<=n;i++) {scanf ("%d%d", &x,&y); T[i].d[0]=a[i].x=x+y,t[i].d[1]=a[i].y=x-y,t[i].f=i; } root=build (1,n,0,0); for (i=1;i<=m;i++) scanf ("%d%d", &Q[I].L,&Q[I].R), q[i].id=i; Std::sort (Q+1,Q+M+1,CMPQ); for (i=l=1,r=now=0;i<=m;i++) {int l=q[i].l,r=q[i].r; if (R<r) {for (r++;r<=r;r++) Add (r); r--;} if (r>r) for (; r>r;r--) del (R); if (l<l) for (; l<l;l++) del (L); else if (l>l) {for (l--;l>=l;l--) Add (l); l++;} Ans[q[i].id]=now; } for (i=1;i<=m;i++) printf ("%lld\n", Ans[i]); } return 0;}
Bzoj4255:keep fit!