1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cmath>5#include <cstdlib>6 #defineM 20000097 #defineINF 10000000008 using namespacestd;9 structATen { One intmx[2],mn[2],d[2],l,r; A }a[m],b; - intN,n,m,root,ans; - BOOLCMP (A a1,a A2) the { - returna1.d[n]<A2.d[n]; - } - voidUpdata (intk) + { -A l1=a[a[k].l],r1=A[A[K].R]; + for(intI=0;i<2; i++) A { at if(A[K].L) -A[k].mn[i]=min (A[k].mn[i],l1.mn[i]), a[k].mx[i]=Max (a[k].mx[i],l1.mx[i]); - if(A[K].R) -A[k].mx[i]=max (A[k].mx[i],r1.mx[i]), a[k].mn[i]=min (a[k].mn[i],r1.mn[i]); - } - return; in } - intJianintL1,intR1,intNow ) to { + intMid= (L1+R1) >>1; -n=Now ; theNth_element (a+l1,a+mid,a+r1+1, CMP); * for(intI=0;i<2; i++) $a[mid].mn[i]=a[mid].mx[i]=A[mid].d[i];Panax Notoginseng if(l1<mid) -A[mid].l=jian (l1,mid-1, now^1); the Else +A[mid].l=0; A if(r1>mid) theA[mid].r=jian (mid+1, r1,now^1); + Else -A[mid].r=0; $ Updata (mid); $ returnmid; - } - voidJiaintXintnow,a b) the { - if(b.d[now]<A[x].d[now])Wuyi { the if(A[X].L) -Jia (a[x].l,now^1, b); Wu Else - { Abouta[x].l=++N; $a[n]=b; - for(intI=0;i<2; i++) -a[n].mn[i]=a[n].mx[i]=A[n].d[i]; - } A } + Else the { - if(A[X].R) $Jia (a[x].r,now^1, b); the Else the { thea[x].r=++N; thea[n]=b; - for(intI=0;i<2; i++) ina[n].mn[i]=a[n].mx[i]=A[n].d[i]; the } the } About updata (x); the return; the } the int Get(intk,a p) + { - inttmp=0; the for(intI=0;i<2; i++)BayiTmp+=max (0, a[k].mn[i]-p.d[i]); the for(intI=0;i<2; i++) theTmp+=max (0, p.d[i]-a[k].mx[i]); - returntmp; - } the voidXunintx,a b) the { the intDl=inf,dr=inf; theAns=min (Ans,abs (a[x].d[0]-b.d[0]) +abs (a[x].d[1]-b.d[1])); - if(A[X].L) theDl=Get(a[x].l,b); the if(A[X].R) theDr=Get(a[x].r,b);94 if(dl<DR) the { the if(dl<ans) the Xun (a[x].l,b);98 if(dr<ans) About Xun (a[x].r,b); - }101 Else102 {103 if(dr<ans)104 Xun (a[x].r,b); the if(dl<ans)106 Xun (a[x].l,b);107 }108 return;109 } the intMain ()111 { thescanf"%d%d",&n,&m);113 for(intI=1; i<=n;i++) thescanf"%d%d", &a[i].d[0],&a[i].d[1]); theRoot=jian (1N0); the for(intI=1; i<=m;i++)117 {118 inta1,a2,a3;119scanf"%d%d%d",&a1,&a2,&A3); - A B;121b.d[0]=A2;122b.d[1]=A3;123B.l=b.r=0;124 if(a1==1) theJia (Root,0, b);126 Else127 { -ans=inf;129 Xun (root,b); theprintf"%d\n", ans);131 } the }133 return 0;134}
Kdtree
Bzoj 2648:sjy Pendulum Pawn