Bzoj 2648/2716 (sjy piece-kd_tree) [Template:kd_tree]

Source: Internet
Author: User

2648:sjy piece Time limit: Sec Memory Limit: MB
Submit: 1180 Solved: 391
[

id=2648 "style=" color:blue; Text-decoration:none ">submit][status][discuss]

DescriptionThis 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, assuming it is a white pawn. He will find out the recent black pieces from this 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 short black piece of the white piece is output. There may be multiple pieces in the same lattice.

 Inputfirst row two number N Mafter M line. 3 numbers per line T X ySuppose T=1 then put down a black pawnSuppose t=2 then put down a white pawnOutputfor each t=2 output 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 could have

Source

Search=%e9%b8%a3%e8%b0%a2%20%e5%ad%99%e5%98%89%e8%a3%95 "style=" color:blue; Text-decoration:none "> Acknowledgements Sun Jiayu



Hint already has kd-tree, then Baidu a bit

Considering a pile of points on a plane, first find the point of the median of the horizontal axis. Remove. To cut a knife, the remaining points are divided into 2 and a half

Then cut it vertically and cut it sideways ....

Transferred from: http://www.cnblogs.com/slysky/archive/2011/11/08/2241247.html


#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (1000000000) #define F (100000007) #define MAXN (500000+10) #define MAXM (500000+10) typedef Long Long Ll;ll mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} int N,m;int Cmp_d=0;class nodE{public:int x[2];int L,r,minv[2],maxv[2];node () {}node (int a,int b) {MEM (x) l=r=0; x[0]=a,x[1]=b; Rep (i,2) minv[i]=maxv[i]=x[i];} int& operator[] (int i) {return x[i];}; int Dis (node A,node b) {int ans=0; Rep (i,2) ans+=abs (A.x[i]-b.x[i]); return ans;} int Dis2 (node P,node a)//point P and Square area a Euclidean distance {int ans=0; Rep (i,2) {if (P.x[i]<a.minv[i]) Ans+=a.minv[i]-p.x[i];elseif (P.x[i]>a.maxv[i]) ans+=p.x[i]-a.maxv[i];} return ans;} int CMP (node A,node b) {return a[cmp_d]<b[cmp_d];} Class Kd_tree{public:node a[maxn*3]; Kd_tree () {}void mem () {}void update (node& o) {if (O.L) {node P=A[O.L]; Rep (i,2) o.minv[i]=min (O.minv[i],p.minv[i]); Rep (i,2) O.maxv[i]=max (O.maxv[i],p.maxv[i]);} if (O.R) {node P=A[O.R]; Rep (i,2) o.minv[i]=min (O.minv[i],p.minv[i]); Rep (i,2) O.maxv[i]=max (O.maxv[i],p.maxv[i]);}} int build (int l,int r,int nowd) {int m= (l+r) >>1;cmp_d=nowd;nth_element (A+L+1,A+M+1,A+R+1,CMP); if (l^m) a[m].l= Build (L,m-1,nowd^1), if (r^m) a[m].r=build (m+1,r,nowd^1); update (a[m]); return m;} int root;void _build(int l,int r,int nowd) {Root=build (l,r,nowd);} void Insert (int o,int k,int nowd) {int p=a[o].x[nowd];int p2=a[k].x[nowd];if (p2<=p) {if (A[O].L) insert (a[o].l,k,nowd ^1); else a[o].l=k;} Else{if (A[O].R) Insert (a[o].r,k,nowd^1); else a[o].r=k;} Update (A[o]);} void _insert (int k,int nowd) {int P=root;insert (root,k,nowd);} Node _p;int _ans;void ask_min_dis (int o) {if (o==0) return;_ans=min (_ans,dis (a[o],_p)); int ans1=a[o].l?

Dis2 (_P,A[A[O].L]): inf;//point P to a random distance within the range of the minimum value int ANS2=A[O].R? Dis2 (_P,A[A[O].R]): Inf;if (ANS1<ANS2) {if (Ans1<_ans) Ask_min_dis (A[O].L), if (Ans2<_ans) Ask_min_dis (A[O].R );} else {if (Ans2<_ans) Ask_min_dis (A[O].R), if (Ans1<_ans) Ask_min_dis (A[O].L);}} int _ask (node p) {_p=p;_ans=inf;ask_min_dis (root); return _ans;}} S;int Main () {//freopen ("bzoj2648.in", "R", stdin)//freopen ("Bzoj2648.out", "w", stdout);cin>>n>>m; for (i,n) {int x,y;scanf ("%d%d", &x,&y); S.a[i]=node (x, y);} S.a[++n]=node (Inf,inf); S._build (1,n,0); for (i,m) {int p,x,y;scanf ("%d%d%d", &p,&x,&y), if (p==1) {s.a[++n]=node (x, y); S._insert (n,0);} else {printf ("%d\n", S._ask (x, y));}} return 0;}






Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Bzoj 2648/2716 (sjy piece-kd_tree) [Template:kd_tree]

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.