# BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

BZOJ2648: SJY摆棋子

BZOJ2716: [Violet 3]天使玩偶

BZOJ氪金无极限。。。

2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

1
2

## 题解Here!

$K-D\ Tree$一大缺点就是建好的树不能再动。

void pia(int rt,int num){
if(a[rt].lson)pia(a[rt].lson,num);
point[num+a[a[rt].lson].size+1]=a[rt].point;
recycle[++top]=rt;
if(a[rt].rson)pia(a[rt].rson,num+a[a[rt].lson].size+1);
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define MAXN 1000010
#define MAX (1LL<<30)
#define Alpha 0.75
using namespace std;
int n,m,root,ans,size=0;
int top=0,recycle[MAXN];
bool sort_flag=false;
struct Point{
int x,y;
friend bool operator <(const Point &p,const Point &q){
if(sort_flag)return p.y<q.y;
return p.x<q.x;
}
}point[MAXN],now;
struct Tree{
Point point;
int minx,miny,maxx,maxy,lson,rson,size;
}a[MAXN];
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
inline int get_dis(const Point &p,const Point &q){
return abs(p.x-q.x)+abs(p.y-q.y);
}
inline int newnode(const Point &p){
int rt;
if(top)rt=recycle[top--];
else rt=++size;
a[rt].point=p;
a[rt].maxx=a[rt].minx=p.x;
a[rt].maxy=a[rt].miny=p.y;
a[rt].lson=a[rt].rson=0;
a[rt].size=1;
return rt;
}
inline void pushup(int rt){
int lson=a[rt].lson,rson=a[rt].rson;
a[rt].size=a[lson].size+a[rson].size+1;
a[rt].maxx=max(a[rt].maxx,max(a[lson].maxx,a[rson].maxx));
a[rt].maxy=max(a[rt].maxy,max(a[lson].maxy,a[rson].maxy));
a[rt].minx=min(a[rt].minx,min(a[lson].minx,a[rson].minx));
a[rt].miny=min(a[rt].miny,min(a[lson].miny,a[rson].miny));
}
void buildtree(int l,int r,int &rt,int flag){
int mid=l+r>>1;
sort_flag=flag;
nth_element(point+l,point+mid,point+r+1);
rt=newnode(point[mid]);
if(l<mid)buildtree(l,mid-1,a[rt].lson,flag^1);
if(mid<r)buildtree(mid+1,r,a[rt].rson,flag^1);
pushup(rt);
}
void pia(int rt,int num){
if(a[rt].lson)pia(a[rt].lson,num);
point[num+a[a[rt].lson].size+1]=a[rt].point;
recycle[++top]=rt;
if(a[rt].rson)pia(a[rt].rson,num+a[a[rt].lson].size+1);
}
void check(int &rt,int flag){
if(Alpha*a[rt].size<max(a[a[rt].lson].size,a[a[rt].rson].size)){
pia(rt,0);
buildtree(1,a[rt].size,rt,flag);
}
}
void insert(int &rt,int flag){
if(!rt){
rt=newnode(now);
return;
}
sort_flag=flag;
if(a[rt].point<now)insert(a[rt].rson,flag^1);
else insert(a[rt].lson,flag^1);
pushup(rt);
check(rt,flag);
}
inline int max_dis(int rt){
int x,y;
x=max(now.x-a[rt].maxx,0)+max(a[rt].minx-now.x,0);
y=max(now.y-a[rt].maxy,0)+max(a[rt].miny-now.y,0);
return x+y;
}
void query(int rt){
int dis=get_dis(a[rt].point,now),ldis=MAX,rdis=MAX;
ans=min(ans,dis);
if(a[rt].lson)ldis=max_dis(a[rt].lson);
if(a[rt].rson)rdis=max_dis(a[rt].rson);
if(ldis<rdis){
if(ldis<ans)query(a[rt].lson);
if(rdis<ans)query(a[rt].rson);
}
else{
if(rdis<ans)query(a[rt].rson);
if(ldis<ans)query(a[rt].lson);
}
}
void work(){
int f;
while(m--){
if(f==1)insert(root,0);
else{
ans=MAX;
query(root);
printf("%d\n",ans);
}
}
}
void init(){
a[0].maxx=a[0].maxy=-MAX;
a[0].minx=a[0].miny=MAX;