Description
Piglet Ipig in PKU just finished bored pig sex algebra class, talent intelligent Ipig was this door to his very simple class made very lonely, in order to eliminate loneliness, he decided to play with his good friend Gipi (chicken skin) A more lonely game---hide and seek. But, they feel, play ordinary hide and seek no meaning, or is not lonely enough, so they decided to play the lonely and incomparable crab version of hide and Seek, as the name implies, that is, they play the game only along the horizontal or vertical direction. After a lonely pair of scissors and stone cloth, they decided to Ipig to catch Gipi. Because they are very familiar with the terrain of PKU, so Gipi will only hide in the PKU within the N secret location, obviously Ipig will only in that N locations to find Gipi. At the beginning of the game, they selected a location, Ipig remained motionless, and then Gipi fled the scene for 30 seconds (obviously, Gipi would not stay in place). Then Ipig will randomly go to find gipi until it is found. Because Ipig is very lazy, so he always take the shortest path, moreover, he chose the starting point is not random, he wanted to find a location, so that the location to the farthest point and the nearest location of the least distance difference. Ipig now wants to know what the minimum distance difference is. Because Ipig now has no computer on hand, so it can't be programmed to solve such a simple problem, so he immediately called and asked you to help him solve the problem. Ipig tells you the coordinates of PKU's n secret locations, and asks you to program Ipig problems.
Input
The first line enters an integer N 2~n+1 line, two integers per line x, y, representing the coordinates of the I-place
Output
An integer that is the minimum value for the distance difference.
Establish k-d tree, enumerate each point to query the nearest point and the farthest point of the Manhattan distance.
The worst time complexity O (n0.5) of a single query is usually not achieved.
#include <cstdio>#include<algorithm>#defineINF 2147483647inlineintAbsintx) {returnX>0? x:-x;} InlineintMaxintXintY) {returnX>y?x:y;} InlineintMinintXintY) {returnX<y?x:y;} InlinevoidMAXS (int&x,intY) {if(x<y) x=y;} Inlinevoidminsint&x,intY) {if(x>y) x=y;} InlineintMax0 (intx) {returnX>0? x:0;}structpos{intx, y;};structnode{intX1,y1,x2,y2; intXm,ym; intL,r; Node (): X1 (INF), Y1 (INF), X2 (-inf), Y2 (-inf) {} inlinevoid Set(intXinty) {X1=x2=xm=x; Y1=y2=ym=y; }};intdx=0;BOOL operator<(pos A,pos b) {if(DX)returna.x<b.x; returna.y<B.y;} Node ns[500005];p os ps[500005];intnp=1, N;intX,y,minv,maxv,ans (INF);intBuildintLintRintD=0){ if(L==R)return 0; DX=D; intM=l+r>>1; Node&w=ns[np++]; Std::nth_element (PS+l,ps+m,ps+R); W.Set(PS[M].X,PS[M].Y); D^=1; W.L=build (L,M,D); mins (w.x1,ns[w.l].x1); mins (w.y1,ns[w.l].y1); Maxs (W.X2,NS[W.L].X2); Maxs (W.Y2,NS[W.L].Y2); W.R=build (m+1, r,d); mins (w.x1,ns[w.r].x1); mins (w.y1,ns[w.r].y1); Maxs (W.X2,NS[W.R].X2); Maxs (W.Y2,NS[W.R].Y2); return&w-NS;} InlineintMaxd (intv) { if(!V)return 0; Node&w=Ns[v]; returnMax0 (x-w.x1) +max0 (w.x2-x) +max0 (y-w.y1) +max0 (w.y2-Y);} InlineintMindintv) { if(!V)returninf; Node&w=Ns[v]; returnMax0 (x-w.x2) +max0 (w.x1-x) +max0 (y-w.y2) +max0 (w.y1-Y);}voidntintv=1) {node&w=Ns[v]; intA=abs (X-W.XM) +abs (yW.ym); if(a) mins (minv,a); intLd=Mind (W.L); intrd=Mind (W.R); if(ld<Rd) { if(ld<MINV) NT (W.L); if(rd<MINV) NT (W.R); }Else{ if(rd<MINV) NT (W.R); if(ld<MINV) NT (W.L); }}voidFtintv=1) {node&w=Ns[v]; intA=abs (X-W.XM) +abs (yW.ym); if(a) maxs (maxv,a); intLd=Maxd (W.L); inttd{Maxd (W.R); if(ld>Rd) { if(ld>maxv) ft (W.L); if(rd>maxv) ft (W.R); }Else{ if(rd>maxv) ft (W.R); if(ld>maxv) ft (W.L); }}inlineintRead () {CharC=GetChar (); intx=0; while(c>'9'|| c<'0') c=GetChar (); while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); returnx;}intMain () {n=read (); for(intI=0; i<n;i++) Ps[i].x=read (), ps[i].y=read (); Build (0, N); for(intI=0; i<n;i++) {X=ps[i].x; y=ps[i].y; MINV=inf;maxv=0; NT (); ft (); mins (ANS,MAXV-MINV); } printf ("%d", ans); return 0;}
bzoj1941 Hide and Seek