"Bzoj" 2648:sjy pendulum piece & 2716: [Violet 3] Angel Doll (Kdtree)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2716

http://www.lydsy.com/JudgeOnline/problem.php?id=2648

Double the experience problem ...

Kdtree naked Question bar ..... Today I learned the next kdtree ... The feeling is quite simple ....

In fact, it is the geometry of the division to build, and the special is that the X and y-axis in turn to split .... This can provide a good quality for easy searching ...

(at first the brain complements a treap instead of kdtree ...) Obviously I have a brain residue .... Half of the writing found that it was not rotatable ....

(then the brain complements ....) If the goods and grandchildren (son's son) can rotate ... Because the node of the goods is similar to the red-black tree ... But Konjac Konjac I'm too weak to ever write a red-black tree ...

Very good information (concept of brain repair): http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html

Some reference code (though not exactly as written): http://blog.csdn.net/jiangshibiao/article/details/34144829

Tell me how to find it.

First, this is Manhattan distance. Different from Euclid's distance ... If Euclid had been so much simpler.

We need to maintain the extremum (that is, four vertices that can surround a rectangle of all the subtrees including its own point):

If the point you are looking for is inside a rectangle, then go to find.

If the rectangle is outside, then calculate the distance from the Manhattan to find the point to the rectangle, judging if it is less than the current optimal solution, and if so, go to search.

(maybe even better, because at first my idea wasn't the same as this one.) I think.. Recursively enters one of the two subtrees trees (sorted by x or Y instead of the extremum), and then determines whether the optimal solution can cross the split line of the current root from a point in the left subtree .... If it is, enter ...

And then there's no more ...

Left the pit: Euclid did not write ... The delete operation has not been written (feel the right to Sparta ...). Want to write a balance tree (obsessive-Compulsive Disorder series

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=2000005, oo=~0u>>1;struct node *null;struct node {node *c[2];int mx[2], mn[2], P[2];void upd (node *x) {if (x==null) Return;mx[0]=max (mx[0], x->mx[0]); Mx[1]=max (mx[1) , x->mx[1]); Mn[0]=min (Mn[0], x->mn[0]); Mn[1]=min (Mn[1], x->mn[1]);} int Getdis (node *x) {int Ret=0;ret+=max (x->p[0]-mx[0], 0); Ret+=max (X->p[1]-mx[1], 0); Ret+=max (Mn[0]-x->p[0] , 0); Ret+=max (Mn[1]-x->p[1], 0); return ret;} void init (int x, int y) {p[0]=mx[0]=mn[0]=x;p[1]=mx[1]=mn[1]=y;c[0]=c[1]=null;} int dis (node *x) {return abs (P[0]-x->p[0]) +abs (p[1]-x->p[1]);}} *root, T[n], *tcnt=t;node *newnode (int x=0, int y=0) {tcnt->init (x, y); return tcnt++;} void Init () {null=newnode (); null->c[0]=null->c[1]=null; Root=null;} void Insert (node *&x, node *y, bool f) {if (x==null) {x=y; return;} BOOL D=x->p[f]<y->p[f];x->upd (y); Insert (X->c[d], y,!f);} void ins (int x, int y) {insert (root, NewNode (x, y), 0);} void Ask (node *x, node *y, int &ans) {ans=min (ans, X->dis (y)); int d[2];d [0]=x->c[0]==null?oo:x->c[0]-> GetDis (y);d [1]=x->c[1]==null?oo:x->c[1]->getdis (y); bool f=d[0]>d[1]; if (D[f]==oo) Return;ask (x->c[f], y, ans); if (D[!f]<ans) ask (X->c[!f], y, ans);} int ask (int x, int y) {int ret=oo; static node r;r.p[0]=x; R.p[1]=y;ask (Root, &r, ret); return ret;} int main () {init (); int n=getint (), M=getint (), Rep (i, n) {int x=getint (), Y=getint (), Ins (x, y); while (m--) {int t=getint (), X=getint (), Y=getint (), if (t==2) printf ("%d\n", ask (x, y)); Else ins (x, y);} return 0;}

  

Description This day, Sjy seems bored. at home. On a chessboard, there are n black pieces. Every time he either put a black piece on the board, or put a white piece, if it is a white piece, he will find the nearest black piece from the white piece. The distance here is Manhattan distance (|x1-x2|+|y1-y2|). Now give the n<=500000 an initial piece. and m<=500000 operations. For each white piece, the distance from the black piece that is closest to the white piece is output. There may be multiple pieces in the same lattice. Input first row two number N m after M row, 3 number per line T x y if T=1 then lay down a black pawn if t=2 then lay down a white pawn output for each t=2 outputs a minimum distance of Sample Input2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

Sample Output
1
2

HINT



Kdtree can.

Source

Acknowledgement Sun Jiayu

Bzoj 2648:sjy pendulum piece & 2716: [Violet 3] Angel Doll (Kdtree)

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.