# 【BZOJ2648】SJY摆棋子（KD-Tree）

### 代码

#include<bits/stdc++.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define swap(x,y) (x^=y,y^=x,x^=y)
#define abs(x) ((x)<0?-(x):(x))
#define INF 1e9
#define Inc(x,y) ((x+=(y))>=MOD&&(x-=MOD))
#define ten(x) (((x)<<3)+((x)<<1))
#define N 300000
using namespace std;
int d;
struct point
{
int s[2];
inline friend bool operator < (point x,point y) {return x.s[d]<y.s[d];}
point(int x=0,int y=0) {s[0]=x,s[1]=y;}
}s[N+5];
class FIO
{
private:
#define Fsize 100000
#define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
public:
FIO() {FinNow=FinEnd=Fin;}
inline void read(int &x) {x=0,f=1;while(!isdigit(ch=tc())) f=ch^'-'?1:-1;while(x=ten(x)+(ch&15),isdigit(ch=tc()));x*=f;}
inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
inline void write(int x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
inline void write_char(char x) {pc(x);}
inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
inline void end() {fwrite(Fout,1,FoutSize,stdout);}
}F;
class Class_TwoDTree//2D-Tree
{
private:
#define alpha 0.75
#define balance(x) (alpha*node[x].Size>=1.0*max(node[node[x].Son[0]].Size,node[node[x].Son[1]].Size))//判断子树是否平衡，借鉴了替罪羊树的思想
int rt,ans,tot,cnt,Void[(N<<1)+5];point data[(N<<1)+5];
struct Tree
{
int MaxX,MinX,MaxY,MinY,Size,Son[2];
point p;
Tree(int x=0,int y=0):p(x,y),MaxX(x),MinX(x),MaxY(y),MinY(y){Son[0]=Son[1]=Size=0;}
inline int get_dis(point w) {return abs(p.s[0]-w.s[0])+abs(p.s[1]-w.s[1]);}//求出某个点到当前点的距离
inline int calc(point w) {return max(MinX-w.s[0],0)+max(w.s[0]-MaxX,0)+max(MinY-w.s[1],0)+max(w.s[1]-MaxY,0);}//求出一个点到该子树构成矩形的距离
}node[(N<<1)+5];
inline void PushUp(int x)//上传信息
{
node[x].Size=node[node[x].Son[0]].Size+node[node[x].Son[1]].Size+1,
node[x].MaxX=max(node[x].p.s[0],max(node[node[x].Son[0]].MaxX,node[node[x].Son[1]].MaxX)),node[x].MinX=min(node[x].p.s[0],min(node[node[x].Son[0]].MinX,node[node[x].Son[1]].MinX)),
node[x].MaxY=max(node[x].p.s[1],max(node[node[x].Son[0]].MaxY,node[node[x].Son[1]].MaxY)),node[x].MinY=min(node[x].p.s[1],min(node[node[x].Son[0]].MinY,node[node[x].Son[1]].MinY));
}
inline int Build(int l,int r,int op)//建树
{
register int x=Void[tot--],mid=l+r>>1;
d=op,nth_element(data+l+1,data+mid+1,data+r+1),node[x]=Tree(data[mid].s[0],data[mid].s[1]),
l<mid&&(node[x].Son[0]=Build(l,mid-1,op^1)),r>mid&&(node[x].Son[1]=Build(mid+1,r,op^1));
return (void)(PushUp(x)),x;
}
inline void Traversal(int x) {if(x) Traversal(node[x].Son[0]),data[++cnt]=node[Void[++tot]=x].p,Traversal(node[x].Son[1]);}//中序遍历
inline void ReBuild(int &x,int op) {cnt=0,Traversal(x),x=Build(1,cnt,op);}//重构
inline void ins(int &x,point v,int op)//插入元素
{
if(!x) return (void)(node[x=Void[tot--]]=Tree(v.s[0],v.s[1]),node[x].Size=1);
v.s[op]<node[x].p.s[op]?ins(node[x].Son[0],v,op^1):ins(node[x].Son[1],v,op^1);
PushUp(x);
}
inline void check(int &x,point v,int op)//判断是否平衡
{
if(!x) return;if(!balance(x)) return ReBuild(x,op);
v.s[op]<node[x].p.s[op]?check(node[x].Son[0],v,op^1):check(node[x].Son[1],v,op^1);
}
inline void qry(int x,point v)//询问
{
register int res=node[x].get_dis(v),dis1=node[x].Son[0]?node[node[x].Son[0]].calc(v):INF,dis2=node[x].Son[1]?node[node[x].Son[1]].calc(v):INF;//res求出当前点的距离，dis1和dis2分别求出当前询问点到两个子树构成矩形的距离
ans=min(ans,res);//更新ans
if(dis1<dis2) dis1<ans&&(qry(node[x].Son[0],v),0),dis2<ans&&(qry(node[x].Son[1],v),0);//注意剪枝
else dis2<ans&&(qry(node[x].Son[1],v),0),dis1<ans&&(qry(node[x].Son[0],v),0);
}
public:
Class_TwoDTree() {node[0].MaxX=node[0].MaxY=-INF,node[0].MinX=node[0].MinY=INF;for(register int i=N<<1;i;--i) Void[++tot]=i;}//初始化
inline void Init(int n,point *num) {for(register int i=1;i<=n;++i) data[i]=num[i];rt=Build(1,n,0);}//建树
inline void Insert(point v) {ins(rt,v,0),check(rt,v,0);}//插入元素
inline int Query(point v) {return (void)(ans=INF,qry(rt,v)),ans;}//询问
}TwoDTree;
int main()
{
register int i,n,Q,op,x,y;