The main topic: N points on a given plane, defining distances to Manhattan distances, supports the following operations:
1. Insert a point
2. Query the distance from the nearest point of a point
Hint said Kdtree "can", then do not write KDT can write what = =
My CDQ, but t fell. = =
Remember, KDT is not necessarily a constant problem when the Tle event occurs. = = (this is not the same as MO team 233
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 500500# Define INF 0x3f3f3f3fusing namespace std;struct point{int x,y;friend istream& operator >> (IStream &_,point &p) {scanf ("%d%d", &p.x,&p.y); return _;} Friend int Distance (const point &p1,const point &p2) {return abs (p1.x-p2.x) + ABS (P1.Y-P2.Y);}} Points[m];int n,m;bool compare_x (const point &p1,const point &p2) {return p1.x < p2.x;} BOOL Compare_y (const point &p1,const point &p2) {return p1.y < p2.y;} namespace K_dimensional_tree{struct ABCD{ABCD *ls,*rs; Point P;int X1,y1,x2,y2;abcd () {}ABCD (const point &_):p (_) {ls=rs=0x0;x1=x2=p.x;y1=y2=p.y;} void Push_up (ABCD *x) {x1=min (x1,x->x1); Y1=min (y1,x->y1); X2=max (x2,x->x2); Y2=max (y2,x->y2);} int min_distance (const point &p) {int re=0;if (p.x<x1) re+=x1-p.x;if (p.x>x2) re+=p.x-x2;if (p.y<y1) re+= Y1-p.y;if (p.y>y2) Re+=p.y-y2;return re;} /*int max_distance (const Point &p) {int Re=0;re+=max (p.x-x1,x2-p.x); Re+=max (P.Y-Y1,Y2-P.Y); return re;} */}mempool[m],*c=mempool,*root;void Build_tree (ABCD *&x,int l,int r,bool flag) {if (l>r) return; int mid=l+r> >1;nth_element (Points+l,points+mid,points+r+1,flag? compare_y:compare_x); x=new ABCD (Points[mid]); Build_tree (x->ls,l,mid-1,flag^1); Build_tree (x->rs,mid+1,r,flag^1), if (X->ls) x->push_up (X->ls), if (X->rs) x->push_up (X->RS);} void Insert (ABCD *&x,const point &p,bool flag) {if (!x) {x=new ABCD (p); return;} if (flag? compare_y:compare_x) (p,x->p)) {Insert (x->ls,p,flag^1); x->push_up (X->ls);} Else{insert (x->rs,p,flag^1); x->push_up (X->rs);}} void Get_min (ABCD *x,const point &p,int &ans) {ans=min (Ans,distance (x->p,p)); int l_dis=x->ls?x->ls- >min_distance (P): Inf;int r_dis=x->rs?x->rs->min_distance (P): Inf;if (L_dis<r_dis) {if (X->ls & & L_dis<ans) Get_min (X->ls,p,ans), if (X->rs && R_dis<ans) get_min (X->rs,p,ans);} Else{if (X->rs && R_dis<ans) get_min (X->rs,p,ans); if (X->ls && L_dis<ans) get_min (X- >ls,p,ans);}}} int main () {using namespace K_dimensional_tree;int i,o; Point P;cin>>n>>m;for (i=1;i<=n;i++) cin>>points[i]; Build_tree (root,1,n,0); for (i=1;i<=m;i++) {scanf ("%d", &o), Cin>>p;if (o==1) Insert (root,p,0); else{int Ans=inf; Get_min (Root,p,ans);p rintf ("%d\n", ans);}} return 0;}
Bzoj 2648 sjy pendulum piece K-dimensional-tree