1941: [Sdoi2010]hide and Seek time limit:16 Sec Memory limit:162 MB
submit:830 solved:455
[Submit] [Status] [Discuss] 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.
Sample Input4
0 0
1 0
0 1
1 1
Sample Output1
HINT
For 30% of the data, n<=1000 for 100% of the data, n<=500000,0<=x,y<=10^8 ensure that the data does not focus on ensuring n>=2
Source
SDOI2010 Second Round day 1
Solution
Kdtree template problem ... Add all points to kdtree, then enumerate each point, find the furthest nearest, and update the answer
It is important to note that the calculation is the furthest and the nearest two are written separately, and the nearest point cannot be computed to its own
Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineINF 0x7fffffff#defineMAXN 500010intN,d,ans;structpointnode{intL,r;intd[2],maxx[2],minn[2]; Pointnode (intx=0,inty=0) {l=r=0; d[0]=x,d[1]=y;} BOOL operator< (ConstPointnode & A)Const{returnd[d]<a.d[d];}} P[MAXN];intDis (pointnode a,pointnode B) {returnABS (a.d[1]-b.d[1]) +abs (a.d[0]-b.d[0]);}structkdtreenode{Pointnode TREE[MAXN<<1],point; intrt,ansmax,ansmin; voidUpdate (intNow ) { for(intI=0; i<=1; i++) {Tree[now].minn[i]=tree[now].maxx[i]=Tree[now].d[i]; if(TREE[NOW].L) tree[now].minn[i]=min (Tree[tree[now].l].minn[i],tree[now].minn[i]),
tree[now].maxx[i]=Max (tree[tree[now].l].maxx[i],tree[now].maxx[i]); if(TREE[NOW].R) tree[now].minn[i]=min (Tree[tree[now].r].minn[i],tree[now].minn[i]),
tree[now].maxx[i]=Max (tree[tree[now].r].maxx[i],tree[now].maxx[i]); } } intBuildtree (intLintRintdd) {intMid= (l+r) >>1; D=DD; Nth_element (p+l,p+mid,p+r+1); Tree[mid]=P[mid]; for(intI=0; i<=1; i++) tree[mid].minn[i]=tree[mid].maxx[i]=Tree[mid].d[i]; if(L<mid) Tree[mid].l=buildtree (l,mid-1, dd^1); if(R>mid) Tree[mid].r=buildtree (mid+1, r,dd^1); Update (mid); returnmid; } intDismax (intNow ) { if(!now)return-inf; intRe=0; for(intI=0; i<=1; i++) Re+=max (ABS (Tree[now].maxx[i]-point.d[i]), ABS (tree[now].minn[i]-point.d[i])); returnre; } intDismin (intNow ) { if(!now)returninf; intRe=0; for(intI=0; i<=1; i++) Re+=max (0, tree[now].minn[i]-Point.d[i]); for(intI=0; i<=1; i++) Re+=max (0, point.d[i]-Tree[now].maxx[i]); returnre; } voidGetmax (intNow ) { if(!now)return; intdl,dr,d0; D0=dis (tree[now],point); Ansmax=Max (D0,ansmax); if(TREE[NOW].L) dl=Dismax (TREE[NOW].L); if(TREE[NOW].R) dr=Dismax (TREE[NOW].R); if(dl>DR) { if(dl>Ansmax) Getmax (TREE[NOW].L); if(dr>Ansmax) Getmax (TREE[NOW].R); } Else { if(dr>Ansmax) Getmax (TREE[NOW].R); if(dl>Ansmax) Getmax (TREE[NOW].L); } } voidGetmin (intNow ) { if(!now)return; intdl,dr,d0; D0=dis (tree[now],point); if(D0) ansmin=min (ansmin,d0); if(TREE[NOW].L) dl=dismin (TREE[NOW].L); if(TREE[NOW].R) dr=dismin (TREE[NOW].R); if(dl<DR) { if(dl<ansmin) Getmin (TREE[NOW].L); if(dr<ansmin) Getmin (TREE[NOW].R); } Else { if(dr<ansmin) Getmin (TREE[NOW].R); if(dl<ansmin) Getmin (TREE[NOW].L); } } intQuerymax (Pointnode P) {point=p; ansmax=-inf; Getmax (RT);returnAnsmax;} intQuerymin (Pointnode P) {point=p; ansmin=inf; Getmin (RT);returnansmin;}} Kdtree;intMain () {n=read (); for(intX,y,i=1; i<=n; i++) X=read (), Y=read (), p[i].d[0]=x,p[i].d[1]=y; for(intI=0; i<=1; i++) p[0].maxx[i]=-inf,p[0].minn[i]=inf; Kdtree.rt=kdtree.buildtree (1N1); Ans=inf; for(intI=1; i<=n; i++) { intMinn=kdtree.querymin (P[i]), maxx=Kdtree.querymax (P[i]); Ans=min (ans,maxx-Minn); } printf ("%d\n", ans); return 0;}
"BZOJ-1941" Hide and Seek Kd-tree