Directly paste the code.
Recommend a KDT explanation, http://blog.csdn.net/wyfcyx_forever/article/details/40182345
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define N 501000#define inf 0x3f3f3f3f#define D (x x > (y))? ( (x)-(Y)):((y)-(x))) using namespace Std;int n,ans=inf;int judge;struct point{int x, y; Point (int _x=0,int _y=0): X (_x), Y (_y) {}bool operator < (const point &a) Const{return judge?y<a.y:x<a.x;}} p[n];inline int dis (point &a,point &b) {return D (a.x,b.x) +d (A.Y,B.Y);} struct Kdt{int son[n][2],cnt;int x[n][2],y[n][2];void init (int a,point &p) {x[a][0]=x[a][1]=p.x;y[a][0]=y[a][1]= P.Y;} void update (int f) {int s;if (son[f][0]) {s=son[f][0];x[f][0]=min (x[f][0],x[s][0]); Y[f][0]=min (y[f][0],y[s][0]); X[f] [1]=max (X[f][1],x[s][1]); Y[f][1]=max (y[f][1],y[s][1]);} if (Son[f][1]) {s=son[f][1];x[f][0]=min (x[f][0],x[s][0]); Y[f][0]=min (y[f][0],y[s][0]); X[f][1]=max (x[f][1],x[s][1 ]); Y[f][1]=max (y[f][1],y[s][1]);}} int Maxdis (int a,point &p) {int Ret=0;ret+=max (d (x[a][0],p.x), D (x[a][1],p.x)), Ret+=max (d (y[a][0],p.y), D (Y[A][1],P.Y)); return ret;} int Mindis (int a,point &p) {int ret=0;if (p.x<x[a][0]) Ret+=x[a][0]-p.x;else if (x[a][1]<p.x) ret+=p.x-x[a][1] if (p.y<y[a][0]) Ret+=y[a][0]-p.y;else if (y[a][1]<p.y) ret+=p.y-y[a][1];return ret;} int NewNode (point &p) {init (++cnt,p); return CNT;} int build (int l,int r,int jd=0) {int mid=l+r>>1,t=0;judge=jd;nth_element (p+l,p+mid,p+r+1), if (L<mid) T=build ( L,MID-1,!JD); int Q=newnode (P[mid]); Son[q][0]=t;if (Mid<r) son[q][1]=build (MID+1,R,!JD); update (q); return q;} int maxdis,mindis;void querymin (int f,point &p) {int dis[3];D Is[2]=dis (p[f],p), if (dis[2]) mindis=min (mindis,dis[2 ]);D is[0]=dis[1]=inf;if (son[f][0]) Dis[0]=mindis (son[f][0],p), if (son[f][1]) Dis[1]=mindis (son[f][1],p); int t=Dis[ 0]>dis[1];if (Son[f][t]&&dis[t]<mindis) querymin (son[f][t],p); T^=1;if (Son[f][t]&&Dis[t] <mindis) querymin (son[f][t],p);} void Querymax (int f,point &p) {int dis[3];D Is[2]=dis (p[f],p), if (dis[2]) Maxdis=max (maxdis,dis[2]);D is[0]=dis[1]= 0;if (Son[f][0]) Dis[0]=maxdis (Son[f][0],p), if (son[f][1]) Dis[1]=maxdis (son[f][1],p); for (int i=2,t=dis[0]<dis[1];i--;t^=1) if ( Son[f][t]&&dis[t]>maxdis) Querymax (son[f][t],p);} int query (int root,int q) {Mindis=inf,maxdis=0;querymax (Root,p[q]), Querymin (Root,p[q]);//printf ("(%d,%d):%d%d\n", P [Q].x,p[q].y,mindis,maxdis); return Maxdis-mindis;}} Kdt;int Main () {//freopen ("test.in", "R", stdin), scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%d%d", &p[i]. X,&P[I].Y); int root=kdt.build (1,n); for (int i=1;i<=n;i++) ans=min (Ans,kdt.query (root,i));p rintf ("%d\n", ans) ; return 0;}
"BZOJ1941" "SDOI2010" Hide and Seek, kdtree "array" template,