# 【SDOI2010】【BZOJ1941】Hide and Seek

Description

Input

Output

Sample Input

4

0 0

1 0

0 1

1 1

Sample Output

1

HINT

Source

SDOI2010 第二轮Day 1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define Dnum 2
#define MAXN 500010
#define MAXINT 0x3f3f3f3f
#define GET (ch>='0'&&ch<='9')
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;
}
int n,root,ans=MAXINT,tmp;
bool cmp_d;
int x[MAXN],y[MAXN];
struct KDtree
{
int ch[2],d[Dnum],minn[Dnum],maxn[Dnum];
inline void init()  {   for (int i=0;i<Dnum;++i)    minn[i]=maxn[i]=d[i];   }
inline bool operator < (const KDtree& a)const   {   return d[cmp_d]<a.d[cmp_d]; }
}tree[MAXN],qnode;
inline void push_up(int rt)
{
for (int x=0,i=0;i<2;++i)
if ((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(abs(qnode.d[i]-tree[rt].maxn[i]),abs(qnode.d[i]-tree[rt].minn[i]));
return ret;
}
{
int ret=0;
for (int i=0;i<Dnum;++i)    ret+=max(tree[rt].minn[i]-qnode.d[i],0),ret+=max(qnode.d[i]-tree[rt].maxn[i],0);
return ret;
}
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_max(int rt=root)
{
tmp=max(tmp,dis(tree[rt],qnode));
if (disl>disr)
{
if (disl>tmp)   query_max(l);
if (disr>tmp)   query_max(r);
}
else
{
if (disr>tmp)   query_max(r);
if (disl>tmp)   query_max(l);
}
}
void query_min(int rt=root)
{
int Dis=dis(tree[rt],qnode);if (Dis)    tmp=min(tmp,Dis);
if (disl<disr)
{
if (disl<tmp)   query_min(l);
if (disr<tmp)   query_min(r);
}
else
{
if (disr<tmp)   query_min(r);
if (disl<tmp)   query_min(l);
}
}
int query(bool d,int x,int y)
{
qnode.d[0]=x;qnode.d[1]=y;
if (d)  tmp=-MAXINT,query_max();
else    tmp=MAXINT,query_min();
return tmp;
}
int main()
{
in(n);ans=MAXINT;
for (int i=1;i<=n;i++)  in(x[i]),in(y[i]),tree[i].d[0]=x[i],tree[i].d[1]=y[i];
root=rebuild();
for (int i=1;i<=n;i++)
{
int minn=query(0,x[i],y[i]),maxn=query(1,x[i],y[i]);
ans=min(ans,maxn-minn);
}
printf("%d\n",ans);
}

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

举报原因： 您举报文章：【SDOI2010】【BZOJ1941】Hide and Seek 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)