Exercises
Kdtree naked question?
It is really a valued function to judge whether to enter the left and right sub-tree.
int Get (int k) { if(!k)return inf; int ret=0; For0 (i,1) Ret+=max (0, t[k].mi[i]-now[i]); For0 (i,1) Ret+=max (0, now[i]-t[k].mx[i]); return ret;}
Let's see this function.
This is, as I understand it, the smallest distance from now in the estimated subtree of k root
Then take a look at this according to the need not to go into the left and right sub-tree ...
Well, there's no reason ... How to guarantee the complexity of ...
Code:
1#include <cstdio>2#include <cstdlib>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <iostream>7#include <vector>8#include <map>9#include <Set>Ten#include <queue> One#include <string> A #defineINF 1000000000 - #defineMAXN 500000+5 - #defineMAXM 500+100 the #defineEPS 1e-10 - #definell Long Long - #definePA pair<int,int> - #defineFor0 (i,n) for (int i=0;i<= (n); i++) + #defineFor1 (i,n) for (int i=1;i<= (n); i++) - #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) + #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) A #defineFor4 (i,x) for (int i=head[x],y;i;i=e[i].next) at #defineMoD 1000000007 - using namespacestd; -InlineintRead () - { - intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} in while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} - returnx*F; to } + intN,m,cur,tot,rt,ans; - structRec the { * intd[2],mi[2],mx[2],l,r; $ int&operator[](inti) {returnd[i];}Panax Notoginseng}p[maxn],now,t[maxn<<1]; - BOOL operator< (rec A,rec b) {returna[cur]<b[cur];} theInlineintDis (rec a,rec b) {returnABS (a[0]-b[0]) +abs (a[1]-b[1]);} +InlinevoidPushup (intk) A { the intL=t[k].l,r=T[K].R; +For0 (I,1) - { $t[k].mi[i]=min (t[k].mi[i],min (t[l].mi[i],t[r].mi[i)); $t[k].mx[i]=Max (T[k].mx[i],max (t[l].mx[i],t[r].mx[i)); - } - } the intBuildintLintRintdir) - {WuyiCur=dir;intMid= (l+r) >>1; theNth_element (p+l,p+mid,p+r+1); -t[mid]=P[mid]; WuFor0 (I,1) t[mid].mi[i]=t[mid].mx[i]=T[mid][i]; - if(L<mid) T[mid].l=build (l,mid-1, dir^1); About if(mid<r) T[mid].r=build (mid+1, r,dir^1); $ Pushup (mid); - returnmid; - } - voidInsertint&k,intdir) A { + if(!k) the { -k=++tot;t[tot]=Now ; $For0 (I,1) t[tot].mi[i]=t[tot].mx[i]=T[tot][i]; the return; the } the if(Now[dir]<t[k][dir]) insert (t[k].l,dir^1); the ElseInsert (t[k].r,dir^1); - Pushup (k); in } theInlineint Get(intk) the { About if(!k)returninf; the intret=0; theFor0 (I,1) Ret+=max (0, t[k].mi[i]-now[i]); theFor0 (I,1) Ret+=max (0, now[i]-t[k].mx[i]); + returnret; - } the voidQueryintKintdir)Bayi { the intDl=Get(T[K].L), dr=Get(T[K].R), d0=dis (t[k],now); the if(D0<ans) ans=D0; - if(dl<DR) - { the if(dl<ans) query (t[k].l,dir^1); the if(dr<ans) query (t[k].r,dir^1); the}Else the { - if(dr<ans) query (t[k].r,dir^1); the if(dl<ans) query (t[k].l,dir^1); the } the }94 inta[ -]; theInlinevoidPrintintx) the { the if(!x) Putchar ('0');98 for(a[0]=0; x;x/=Ten) a[++a[0]]=x%Ten; AboutFor3 (i,a[0],1) Putchar ('0'+a[i]); -Putchar ('\ n');101 } 102 intMain ()103 {104Freopen ("Input.txt","R", stdin); theFreopen ("output.txt","W", stdout);106N=read (); M=read (); tot=N;107For0 (I,1) t[0].mi[i]=inf,t[0].mx[i]=-inf;108For1 (i,n) p[i][0]=read (), p[i][1]=read ();109Rt=build (1N0); the while(m--)111 { the intOp=read (); now[0]=read (); now[1]=read (); ans=inf;113 if(op==1) Insert (RT,0);ElseQuery (RT,0), print (ans); the } the return 0; the}
View Code
Added output optimization or not in the first edition, Good Mulberry Heart Qaq
2716: [Violet 3] Angel doll time limit:80 Sec Memory limit:128 MB
submit:569 solved:242
[Submit] [Status] Description
Input
Output
BZOJ2716: [Violet 3] Angel Doll