# 【BZOJ2648】SJY摆棋子

Description

Input

Output

Sample Input

2 3

1 1

2 3

2 1 2

1 3 3

2 4 2

Sample Output

1

2

HINT

kdtree可以过

Source

kdtree裸做.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define GET (ch>='0'&&ch<='9')
#define Dnum 2
#define MAXN 500010
#define MAXINT 0x7fffffff
using namespace std;
inline void in(int &x)
{
char ch=getchar();x=0;int flag=1;
while (!GET)    flag=ch=='-'?-1:1,ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();x*=flag;
}
bool cmp_d;
int n,Q,root,ans;
struct KDtree
{
int d[Dnum],ch[2],minn[Dnum],maxn[Dnum];
inline bool operator < (const KDtree &a)const   {   return d[cmp_d]<a.d[cmp_d]; }
inline void init()  {   for (int i=0;i<Dnum;++i)    minn[i]=maxn[i]=d[i];   }
}tree[MAXN<<1],qnode;
inline void push_up(int rt)
{
for (int i=0;i<2;++i)
if (tree[rt].ch[i])
{
int x=tree[rt].ch[i];
for (int j=0;j<Dnum;++j)
tree[rt].minn[j]=min(tree[rt].minn[j],tree[x].minn[j]),
tree[rt].maxn[j]=max(tree[rt].maxn[j],tree[x].maxn[j]);
}
}
int rebuild(int l=1,int r=n,bool d=0)
{
cmp_d=d;int mid=(l+r)>>1;nth_element(tree+l,tree+mid,tree+r+1);
tree[mid].init();
if (l!=mid) tree[mid].ch[0]=rebuild(l,mid-1,d^1);
if (r!=mid) tree[mid].ch[1]=rebuild(mid+1,r,d^1);
return push_up(mid),mid;
}
{
int ret=0;
for (int i=0;i<Dnum;++i)    ret+=max(0,tree[rt].minn[i]-p.d[i]),ret+=max(0,p.d[i]-tree[rt].maxn[i]);
return ret;
}
void insert(int rt=root,bool d=0)
{
bool flag=(tree[n].d[d]>tree[rt].d[d]);
if (tree[rt].ch[flag])  insert(tree[rt].ch[flag],d^1);  else    tree[rt].ch[flag]=n;
push_up(rt);
}
inline int dis(const KDtree a,const KDtree b)   {   return abs(a.d[0]-b.d[0])+abs(a.d[1]-b.d[1]);   }
void query(int rt=root,bool d=0)
{
int Dis,disl=MAXINT,disr=MAXINT;
Dis=dis(tree[rt],qnode);ans=min(ans,Dis);
if (disl<disr)
{
if (disl<ans)   query(tree[rt].ch[0],d^1);
if (disr<ans)   query(tree[rt].ch[1],d^1);
}
else
{
if (disr<ans)   query(tree[rt].ch[1],d^1);
if (disl<ans)   query(tree[rt].ch[0],d^1);
}
}
int main()
{
in(n);in(Q);int opt;
for (int i=1;i<=n;i++)  in(tree[i].d[0]),in(tree[i].d[1]);
for (root=rebuild();Q;Q--)
{
in(opt);ans=MAXINT;
if (opt==1) in(tree[++n].d[0]),in(tree[n].d[1]),tree[n].init(),insert();
else    in(qnode.d[0]),in(qnode.d[1]),query(),printf("%d\n",ans);
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：【BZOJ2648】SJY摆棋子 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)