"Kd-tree" bzoj2716 [Violet 3] Angel Doll

Source: Internet
Author: User

#include <cstdio> #include <cmath> #include <algorithm>using namespace std; #define N 500001#define INF    2147483647#define KD 2//èêýint qp[kd],disn;int n,root;bool dn;struct node{int MINN[KD],MAXX[KD],P[KD];    int ch[2];      void Init () {for (int i=0;i<kd;++i) minn[i]=maxx[i]=p[i];        } int Dis () {int res=0;            for (int i=0;i<kd;++i) {Res+=max (0,minn[i]-qp[i]);          Res+=max (0,qp[i]-maxx[i]);      } return res;          }}t[n<<1];void Update (int rt) {for (Int. i=0;i<2;++i) if (T[rt].ch[i]) for (int j=0;j<kd;++j)            {t[rt].minn[j]=min (t[rt].minn[j],t[t[rt].ch[i]].minn[j]);          T[rt].maxx[j]=max (T[rt].maxx[j],t[t[rt].ch[i]].maxx[j]); }}int Abs (const int &x) {return x<0? (-X): x;} int Dis (int a[],int b[]) {return Abs (a[0]-b[0]) +abs (a[1]-b[1]);} BOOL operator < (const node &a,const node &b) {return a.p[dn]!=b. p[dn]? A.P[DN]&LT;B.P[DN]: a.p[dn^1]<b.p[dn^1];}    int buildtree (int l=1,int r=n,bool d=0) {dn=d;    int m= (l+r>>1);    Nth_element (t+l,t+m,t+r+1); T[M].    Init ();    if (l!=m) T[m].ch[0]=buildtree (l,m-1,d^1);    if (m!=r) T[m].ch[1]=buildtree (m+1,r,d^1);    Update (m); return m;}    void Query (int rt=root) {disn=min (Disn,dis (T[RT].P,QP));    int dd[2]; for (int i=0;i<2;i++) if (T[rt].ch[i]) dd[i]=t[t[rt].ch[i]].      Dis ();    else Dd[i]=inf;    BOOL F= (dd[0]<=dd[1]);    if (DD[!F]&LT;DISN) Query (t[rt].ch[!f]); if (DD[F]&LT;DISN) Query (T[rt].ch[f]);}    void Insert (int rt=root,bool d=0) {bool f= (t[n].p[d]>t[rt].p[d]);    if (T[rt].ch[f]) Insert (t[rt].ch[f],d^1);    else t[rt].ch[f]=n; Update (RT);}    int Q;int Main () {//Freopen ("Bzoj2716.in", "R", stdin);//Freopen ("Bzoj2716.out", "w", stdout);    int op,x,y;    scanf ("%d%d", &n,&q);    for (int i=1;i<=n;++i) scanf ("%d%d", &t[i].p[0],&t[i].p[1]);    Root= (1+n>>1); BuildtRee ();        for (int i=1;i<=q;++i) {scanf ("%d", &op);            if (op==1) {++n;            scanf ("%d%d", &t[n].p[0],&t[n].p[1]); T[n].            Init ();          Insert ();            } else {scanf ("%d%d", &qp[0],&qp[1]);            Disn=inf;            Query ();          printf ("%d\n", DISN); }} return 0;}

"Kd-tree" bzoj2716 [Violet 3] Angel doll

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.