题目大意:给定平面上一堆点,支持插入点和查询一个点的最近点离它的曼哈顿距离。
题解:KD-Tree裸题,第一次写KD-Tree,总的来说这是一种玄学时间复杂度的东西,为什么叫KD-Tree呢,因为它可以支持K维平面上的查询,为了让每个区域尽可能的均衡,我们采用每次变更比较维度的方法,就比如说我们在一个二维平面上横着切一刀再竖着切一刀,让平面的每个区间尽可能的点数相同,然后每一个点代表的就是一个K维空间,如果查询的点在这个区间之内的话就进去查,否则的话就计算一下最小的可能距离,如果这都比答案小就不进去,针对两个区间都不包含点的情况我们先走估价较小的一个再走估价较大的一个,为了防止插入以后造成的不平衡情况我们采取替罪羊式重建(好像并没有快多少)。
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double ALPHA=0.7;
const int INF=2147483647;
struct point
{
int x,y;
int siz;
point(){}
point(int _,int __):x(_),y(__){}
};
bool cmp0(point a,point b)
{
return a.xbool cmp1(point a,point b)
{
return a.yinline int Min(int a,int b) {return ainline int Max(int a,int b) {return ainline point Min(point a,point b){return point(Min(a.x,b.x),Min(a.y,b.y));}
inline point Max(point a,point b){return point(Max(a.x,b.x),Max(a.y,b.y));}
struct kd_tree
{
kd_tree *ls,*rs;
point p;
point ma,mi;
int siz;
bool cmp;
void *operator new (size_t,point _,bool lei)
{
static kd_tree *C,*mempool;
if(C==mempool)
mempool=(C=new kd_tree[1<<15])+(1<<15);
C->p=C->ma=C->mi=_;
C->cmp=lei;
C->ls=C->rs=0x0;
C->siz=1;
return C++;
}
void push_up()
{
siz=1;
if(ls)
{
siz+=ls->siz;
ma=Max(ma,ls->ma);
mi=Min(mi,ls->mi);
}
if(rs)
{
siz+=rs->siz;
ma=Max(ma,rs->ma);
mi=Min(mi,rs->mi);
}
}
bool check()
{
if(ls && ls->siz>siz*ALPHA) return true;
if(rs && rs->siz>siz*ALPHA) return true;
return false;
}
}*root;
inline int Abs(int a){return a<0?-a:a;}
inline int get_dis(point a,point b)
{
return Abs(a.x-b.x)+Abs(a.y-b.y);
}
inline int get_min_dis(point a,point ma,point mi)
{
int re=0;
if(a.xif(a.x>ma.x) re+=a.x-ma.x;
if(a.yif(a.y>ma.y) re+=a.y-ma.y;
return re;
}
point a[2000000];
kd_tree *make_tree(int l,int r,bool lei)
{
int mid=l+r>>1;
if(l>r) return 0x0;
if(l==r) return new (a[mid],lei) kd_tree;
nth_element(a+l,a+mid,a+r+1,lei?cmp1:cmp0);
kd_tree *re=new (a[mid],lei) kd_tree;
re->ls=make_tree(l,mid-1,lei^1);
re->rs=make_tree(mid+1,r,lei^1);
re->push_up();
return re;
}
kd_tree **my_stack[1000000];
int top=0;
void Insert(kd_tree *&o,point p,int lei)
{
if(!o)
{
o=new (p,lei) kd_tree;
return;
}
bool pd=lei?cmp1(p,o->p):cmp0(p,o->p);
if(pd) Insert(o->ls,p,lei^1);
else Insert(o->rs,p,lei^1);
o->push_up();
if(o->check()) my_stack[++top]=&o;
}
int tot=0;
void Delete_tree(kd_tree *&o)
{
if(o->ls) Delete_tree(o->ls);
a[++tot]=o->p;
if(o->rs) Delete_tree(o->rs);
}
void recomposition(kd_tree *&o)
{
top=0;
tot=0;
bool lei=o->cmp;
Delete_tree(o);
o=make_tree(1,tot,lei);
}
int ans=2147483647;
void query(kd_tree *o,point p)
{
ans=Min(ans,get_dis(p,o->p));
int minn1=o->ls?get_min_dis(p,o->ls->ma,o->ls->mi):INF;
int minn2=o->rs?get_min_dis(p,o->rs->ma,o->rs->mi):INF;
if(minn1if(o->ls && minn1ls,p);
if(o->rs && minn2rs,p);
}
else
{
if(o->rs && minn2rs,p);
if(o->ls && minn1ls,p);
}
}
int main()
{
//freopen("a.in","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
root=make_tree(1,n,0);
for(int i=1;i<=m;i++)
{
int lei,x,y;
scanf("%d%d%d",&lei,&x,&y);
if(lei==1)
{
top=0;
Insert(root,point(x,y),root->cmp);
if(top) recomposition(*my_stack[top]);
}
else
{
ans=2147483647;
query(root,point(x,y));
printf("%d\n",ans);
}
}
return 0;
}