This question is quite divine ... Film a half-day claris of the puzzle and the official puzzle.
One-dimensional casual,
Two-dimensional rotation of the coordinate system, the original point (x, y) into (x+y,x-y), the Manhattan distance into the Chebyshev distance. Then scan the lines after sorting, and the tree-like array is maintained.
Three-dimensional words ..... The official puzzle is a direct three-dimensional tree array after rotating the coordinate system? (Scare Cry
Claris The first two dimensions of rotation, preprocessing a wave prefix and on the line .... A specific push will know.
Three-dimensional official solution complexity is the nlog^3n,claris approach is nm. Come on.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 Const intmaxn=100233;8 intX[MAXN];9 structzs{intx, Y, Z;} A[MAXN];Ten ll ans; One inti,j,k,n,m,d,id,m; A - BOOLCMP (ZS A,zs b) {returna.x<b.x;} - intRaCharRx; theInlineintRead () { -Rx=getchar (), ra=0; - while(rx<'0'|| Rx>'9') rx=GetChar (); - while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; + } - +Inlinevoidrun1 () { ARegisterintI,l=1; at for(i=1; i<=n;i++) x[i]=read (); -Sort (x+1, x+1+n); - for(i=2; i<=n;ans+=i-l,i++) while(x[i]-x[l]>d) l++; - } - - intt[75233<<1]; inInlinevoidDelintx) { while(x<=m) t[x]--, x+=x&-x;} -InlinevoidAddintx) { while(x<=m) t[x]++,x+=x&-x;} toInline ll Query (intx) { + if(x<=0)return 0; - if(x>m) x=M; thell sm=0; * while(x) sm+=t[x],x-=x&-x; $ returnSM;Panax Notoginseng } -Inlinevoidrun2 () { theRegisterinti; + for(i=1; i<=n;i++) J=read (), K=read (), a[i].x=j+k,a[i].y=j-k+m; ASort (A +1, A +1+N,CMP);inttop=1; the for(i=1; i<=n;i++){ + while(a[top].x+d<a[i].x) del (A[TOP].Y), top++; -Ans+=query (a[i].y+d)-query (a[i].y-d-1), $ Add (a[i].y); $ } - } - thell mp[ the][ the<<1][ the<<1]; -inline ll GETSM (intXintX1,intY1,intX2,inty2) {Wuyi if(x1<1) x1=1;if(y1<1) y1=1; the if(x2>m) x2=m;if(y2>m) y2=M; - returnMp[x][x2][y2]-mp[x][x1-1][y2]-mp[x][x2][y1-1]+mp[x][x1-1][y1-1]; Wu } -InlinevoidRun3 () { AboutRegisterintI,j,k;intX;ll tmp=0; $ for(i=1; i<=n;i++) A[i].x=read (), J=read (), K=read (), a[i].y=j+k,a[i].z=j-k+m; -Sort (A +1, A +1+n,cmp); - for(i=1; i<=N;) { -x=a[i].x; A for(j=i;a[j+1].x==x;j++); + while(I<=J) mp[x][a[i].y][a[i].z]++,i++; the for(j=1; j<=m;j++) for(k=1; k<=m;k++) mp[x][j][k]+=mp[x][j-1][k]+mp[x][j][k-1]-mp[x][j-1][k-1]; - } $ for(i=1; i<=n;i++){ the for(j=a[i].x+1; j<=m&&j-a[i].x<=d;j++) theK=d-(j-a[i].x), ANS+=GETSM (j,a[i].y-k,a[i].z-k,a[i].y+k,a[i].z+k); theTMP+=GETSM (A[i].x,a[i].y-d,a[i].z-d,a[i].y+d,a[i].z+d)-1; the } -tmp>>=1; inans+=tmp; the } the intMain () { AboutId=read (), N=read (), D=read (), M=read (); m=m<<1; the if(D>=m*id) {printf ("%lld\n", (LL) n (n1)/2);} the if(id==1) run1 (); the if(id==2) run2 (); + if(id==3) Run3 (); -printf"%lld\n", ans); the return 0;Bayi}
View Code
[bzoj1807] [Ioi2007] Pairs of animals that can hear each other.