"Bzoj 2716" [Violet 3] Angel Doll (cdq+ tree-like array)

Source: Internet
Author: User
Tags stdin

Title Description

Ayu had received an Angel doll seven years ago, when she buried it in the ground as a time capsule. And seven years later today, Ayu forgot where she buried the Angel doll, so she decided to look for it with a vague memory.

We consider the town of Ayu life as a two-dimensional plane coordinate system, and Ayu will occasionally remember the possibility of burying an angel doll at a certain point (xmy), or Ayu will ask you how far she is from the nearest Angel doll if she is in (x, y).

Because Ayu only moves in the direction of the parallel axes, in this problem we define the distance between the two points as Dist (A, b) =| ax-bx|+| ay-by|. Where Ax represents the horizontal axis of point A, the rest is similar.

Input/output format

Input format:

The first line contains two integers n and M, and at the beginning, Ayu already knew that N points might be buried with an angel doll, and the next Ayu to operate m

The next n lines, two nonnegative integers per line (xi,yi), represent the coordinates of the initial n points.

Next M line, three nonnegative integers per line t,xi,yi.

If t=1, it means that Ayu also recalls a possible point where the doll is buried (xi,yi).

If t=2, then Ayu asks if she is at the point (Xi,yi), then in the point that has been recalled, how far from the point near her

Output format:

For each t=2 query, output the result of the query in a separate line.

Input/Output sample Input Sample # #:Copy
2 3 1 1 2 3 2 1 2 1 3 3 2 4 2
Sample # # of output:Copy
0 S
Description

N,m<=300 000

Xi,yi<=1 000 000

Exercises

First film, big Guy.

It is said that the problem is kd-tree, but I will only CDQ ... Also thought for half a day nothing to think out ...

Let's assume that all the points in the memory are at the bottom left of the query point, so the distance is as follows (A is the query point, B is the point of a memory)

$ $Dist (A, B) =|x_a-x_b|+|y_a-y_b|= (x_a+y_a)-(X_b+y_b) $$

Because $x_a+y_a$ is a fixed value for the same query point, you can find the minimum value for $dist (A, b) $ as long as you find the maximum value of $x_b+y_b$

The problem then translates to: For a query of $ (x, y) $, find $x_i<=x,y_i<=y$ and $i$ timestamp is less than the maximum $x_i+y_i$ of the current query, it is obvious that this is a three-dimensional partial order problem, can be solved by CDQ

However, the problem is that there is no guarantee that all points are below the query point, so we have to transform the coordinates of the other four-bearing points. In simple terms, rotate the whole picture and take the smallest of the four answers.

So do four times CDQ (just saw this sentence almost didn't scare ...) )

And then there's a couple of optimizations to learn from the big guy

1. Remove the point that must not be in the lower left before each CDQ

2. Every time a rebuild is a hassle, just keep an original image and turn it around.

I feel that my understanding of CDQ is too shallow ...

1 //Minamoto2#include <cstdio>3#include <iostream>4#include <algorithm>5#include <cstring>6 using namespacestd;7 #defineGetc () (p1==p2&& (p2= (p1=buf) +fread (Buf,1,1<<21,stdin), P1==P2)? eof:*p1++)8 Charbuf[1<< +],*p1=buf,*p2=buf;9template<classT>inlineBOOLCmax (T&a,ConstT&AMP;B) {returnA<b?a=b,1:0;}Tentemplate<classT>inlineBOOLCmin (T&a,ConstT&AMP;B) {returnA>b?a=b,1:0;} OneInlineintRead () { A     #defineNum ch-' 0 ' -     CharChBOOLflag=0;intRes; -      while(!isdigit (ch=getc ())) the(ch=='-') && (flag=true); -      for(Res=num;isdigit (Ch=getc ()); res=res*Ten+num); -(flag) && (res=-res); -     #undefNum +     returnRes; - } + Charsr[1<< +],z[ -];intc=-1, Z; AInlinevoidOt () {fwrite (SR),1, c+1, stdout), c=-1;} atInlinevoidPrintintx) { -     if(c>1<< -) Ot ();if(x<0) sr[++c]= $, x=-x; -      while(z[++z]=x%Ten+ -, x/=Ten); -      while(Sr[++c]=z[z],--z); sr[++c]='\ n'; - } - Const intn=1e6+5, inf=0x3f3f3f3f; in intN,m,lx,ly,rx,ry,c[n],ans[n]; -InlinevoidClearintx) { to      for(inti=x;i<=ly;i+=i&-i) +     if(C[i]) c[i]=0;Else  Break; - } theInlinevoidAddintXintval) { *      for(inti=x;i<=ly;i+=i&-i) $ Cmax (c[i],val);Panax Notoginseng } -InlineintQueryintx) { the     intres=0; +      for(inti=x;i;i-=i&-i) A Cmax (Res,c[i]); the     returnRes; + } - structnode{ $     intX,y,t;BOOLF; $ node () {} -NodeConst int&x,Const int&y,Const int&t,Const int&f): - x (x), Y (y), T (T), F (f) {} theInlineBOOL operator< (ConstNode &b)Const -{returnx<b.x| | (x==b.x&&y<b.y);}Wuyi }a[n],p[n],q[n]; the voidCDQ (intLintR) { -     if(L==R)return; Wu     intMid= (l+r) >>1; -CDQ (L,mid), CDQ (mid+1, R); About     intj=l; $      for(intI=mid+1; i<=r;++i) -     if(!p[i].f) { -          while(j<=mid&&p[j].x<=p[i].x) {if(P[J].F) Add (p[j].y,p[j].x+p[j].y); + +J;} -         inttmp=query (P[I].Y); A         if(TMP) tmp=p[i].x+p[i].y-tmp,cmin (ans[p[i].t],tmp); +     } the      for(inti=l;i<j;++i) -     if(P[I].F) Clear (P[I].Y); $Merge (p+l,p+mid+1, p+mid+1, p+r+1, q+l); thememcpy (P+l,q+l,sizeof(node) * (r-l+1)); the } the voidCheck () { therx=ry=m=0; -      for(intI=1; i<=n;++i) in     if(!p[i].f) Cmax (rx,p[i].x), Cmax (RY,P[I].Y); the      for(intI=1; i<=n;++i) the     if(P[i].x<=rx&&p[i].y<=ry) q[++m]=P[i]; Aboutmemcpy (p+1, q+1,sizeof(node) *m); the } the intMain () { the     //freopen ("testdata.in", "R", stdin); +N=read (), m=read (); -      for(intI=1; i<=n;++i) { the         intX=read () +1, Y=read () +1;BayiP[i]= (node) {x,y,i,true}; the Cmax (lx,x), Cmax (ly,y); the     } -      while(m--){ -         intK=read (), X=read () +1, Y=read () +1; the++n,p[n]=node (x,y,n,k&1); the Cmax (lx,x), Cmax (ly,y); the     } thememcpy (A +1, p+1,sizeof(node) *n); -memset (Ans,inf,sizeof(ans)); theCheck (), CDQ (1, m); the      for(intI=1; i<=n;++i) thep[i]=a[i],p[i].x=lx-p[i].x+1;94Check (), CDQ (1, m); the      for(intI=1; i<=n;++i) thep[i]=a[i],p[i].y=ly-p[i].y+1; theCheck (), CDQ (1, m);98      for(intI=1; i<=n;++i) Aboutp[i]=a[i],p[i].y=ly-p[i].y+1, p[i].x=lx-p[i].x+1; -Check (), CDQ (1, m);101      for(intI=1; i<=n;++i)102     if(!a[i].f) print (ans[i]);103 Ot ();104     return 0; the}

"Bzoj 2716" [Violet 3] Angel Doll (cdq+ tree-like array)

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.