板子题,只是感觉KD-Tree写起来很爽就先找了道题写写,发现现在的手速越来越慢了的说
真没什么好说的,暴枚选那个点做起点,然后求到一个点的最远最近点即可
注意一个细节:判断一个点到矩形的距离最小值是要考虑仔细,某一维是否有贡献要看这个点是否被包含在最大最小的区间内(刚开始naive了)
#include
#include
#include
#define RI register int
#define CI const int&
using namespace std;
const int N=100005,INF=2e9;
int D;
struct point
{
int d[2];
friend inline bool operator < (const point& A,const point& B)
{
return A.d[D]==B.d[D]?A.d[D^1]>1; D=d; nth_element(a+l+1,a+mid+1,a+r+1);
P(now)=Mi(now)=Mx(now)=a[mid];
if (l!=mid) build(lc(now),l,mid-1,d^1),pushup(now,lc(now));
if (r!=mid) build(rc(now),mid+1,r,d^1),pushup(now,rc(now));
}
inline void querymin(CI now)
{
if (!now) return; if (getmin(now)>mi) return;
int tp=dist(s,P(now)); if (tp) mi=min(mi,tp);
int ml=getmin(lc(now)),mr=getmin(rc(now));
if (mlmr) querymax(lc(now)),querymax(rc(now));
else querymax(rc(now)),querymax(lc(now));
}
#undef lc
#undef rc
#undef P
#undef Mi
#undef Mx
}KD;
int main()
{
RI i; for (scanf("%d",&n),i=1;i<=n;++i) scanf("%d%d",&a[i].d[0],&a[i].d[1]);
for (KD.build(rt),i=1;i<=n;++i)
s=(point){a[i].d[0],a[i].d[1]},mi=INF,mx=-INF,KD.querymin(rt),KD.querymax(rt),ans=min(ans,mx-mi);
return printf("%d",ans),0;
}