Description
给出平面上坐标非负的
Solution
依然是一道裸题,对这
时间复杂度
O(nlogn) 。
Code
//[Sdoi2010]Hide and Seek
#include <cstdio>
#include <algorithm>
using namespace std;
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
int const N=5e5+10;
int const INF=0x7FFFFFFF;
int n;
int ndCnt,rt,D,ch[N][2];
struct point{int c[2];} p0[N],v[N];
struct zone{int c1[2],c2[2];} zn[N];
bool cmpPt(point x,point y) {return x.c[D]<y.c[D];}
void create(int &p,point pt)
{
p=++ndCnt; v[p]=pt;
for(int k=0;k<2;k++) zn[p].c1[k]=zn[p].c2[k]=pt.c[k];
ch[p][0]=ch[p][1]=0;
}
void update(int p)
{
for(int k=0;k<2;k++)
{
zn[p].c1[k]=min(v[p].c[k],min(zn[ch[p][0]].c1[k],zn[ch[p][1]].c1[k]));
zn[p].c2[k]=max(v[p].c[k],max(zn[ch[p][0]].c2[k],zn[ch[p][1]].c2[k]));
}
}
void build(int &p,int L,int R)
{
int mid=L+R>>1;
nth_element(p0+L,p0+mid,p0+R+1,cmpPt);
create(p,p0[mid]); D^=1;
if(L<mid) build(ch[p][0],L,mid-1);
if(mid<R) build(ch[p][1],mid+1,R);
update(p);
}
int ans,r1,r2;
int dst1(point pt,int z0)
{
if(z0==0) return INF;
int res=0; zone z=zn[z0];
for(int k=0;k<2;k++)
if(pt.c[k]<z.c1[k]) res+=z.c1[k]-pt.c[k];
else if(z.c2[k]<pt.c[k]) res+=pt.c[k]-z.c2[k];
return res;
}
int dst2(point pt,int z0)
{
if(z0==0) return -1;
int res=0; zone z=zn[z0];
for(int k=0;k<2;k++)
if(pt.c[k]<=(z.c1[k]+z.c2[k])/2) res+=z.c2[k]-pt.c[k];
else res+=pt.c[k]-z.c1[k];
return res;
}
int query1(int p,point pt)
{
int d=0; for(int k=0;k<2;k++) d+=abs(pt.c[k]-v[p].c[k]); if(d) r1=min(r1,d);
int dL=dst1(pt,ch[p][0]),dR=dst1(pt,ch[p][1]);
if(dL<dR) {if(dL<r1) query1(ch[p][0],pt); if(dR<r1) query1(ch[p][1],pt);}
else {if(dR<r1) query1(ch[p][1],pt); if(dL<r1) query1(ch[p][0],pt);}
}
int query2(int p,point pt)
{
int d=0; for(int k=0;k<2;k++) d+=abs(pt.c[k]-v[p].c[k]); r2=max(r2,d);
int dL=dst2(pt,ch[p][0]),dR=dst2(pt,ch[p][1]);
if(dL>dR) {if(dL>r2) query2(ch[p][0],pt); if(dR>r2) query2(ch[p][1],pt);}
else {if(dR>r2) query2(ch[p][1],pt); if(dL>r2) query2(ch[p][0],pt);}
}
int main()
{
n=read();
for(int k=0;k<2;k++) zn[0].c1[k]=INF,zn[0].c2[k]=-INF;
for(int i=1;i<=n;i++) p0[i].c[0]=read(),p0[i].c[1]=read();
build(rt,1,n);
ans=INF;
for(int i=1;i<=n;i++)
{
r1=INF; query1(rt,p0[i]);
r2=0; query2(rt,p0[i]);
ans=min(ans,r2-r1);
}
printf("%d
",ans);
return 0;
}
P.S.
- 求最小距离时要忽略自己
- 求
dst2 的时候若矩形不存在返回负数