题解:
学之前以为这个东西挺难的
看了之后发现好像挺简单的
思路上挺简单的,代码上基本和线段树差不多
hdu-2966
代码:
#include <bits/stdc++.h> using namespace std; #define IL inline #define rint register ll #define rep(i,h,t) for(rint i=h;i<=t;i++) #define dep(i,t,h) for(rint i=t;i>=h;i--) #define mid ((h+t)/2) #define ll long long #define me(x) memset(x,0,sizeof(x)) char ss[1<<24],*A=ss,*B=ss; char gc() { return A==B&&(B=(A=ss)+fread(ss,1,1<<24,stdin),A==B)?EOF:*A++; } template<class T> void read(T &x) { rint f=1,c; while (c=gc(),c<48||c>57) if (c=='-') f=-1; x=(c^48); while (c=gc(),c>47&&c<58) x=(x<<3)+(x<<1)+(c^48); x*=f; } const ll INF=1e18; const ll N=2e5+10; ll n,id[N],cmp_d,ans; ll x,y; void umax(ll &x,ll y) { if (x<y) x=y; } void umin(ll &x,ll y) { if (x>y) x=y; } struct re { ll d[2],f; }; bool cmp(re x,re y) { return(x.d[cmp_d]<y.d[cmp_d]); } re p[N]; struct kd { ll ls[N],rs[N],Mx[N],Nx[N],My[N],Ny[N]; void updata(ll x) { if (ls[x]) { umax(Mx[x],Mx[ls[x]]); umax(My[x],My[ls[x]]); umin(Nx[x],Nx[ls[x]]); umin(Ny[x],Ny[ls[x]]); } if (rs[x]) { umax(Mx[x],Mx[rs[x]]); umax(My[x],My[rs[x]]); umin(Nx[x],Nx[rs[x]]); umin(Ny[x],Ny[rs[x]]); } } ll build(ll h,ll t,ll o) { cmp_d=o; nth_element(p+h,p+mid,p+t+1,cmp); int x=mid; id[p[x].f]=x; Mx[x]=Nx[x]=p[x].d[0]; My[x]=Ny[x]=p[x].d[1]; if (h!=x) ls[x]=build(h,mid-1,o^1); else ls[x]=0; //多组数据 if (t!=x) rs[x]=build(mid+1,t,o^1); else rs[x]=0; updata(x); return x; } ll dis(ll x1,ll y1,ll x2,ll y2) { ll x=x1-x2,y=y1-y2; return x*x+y*y; } ll dis(ll k,ll x,ll y) { ll xx=0,yy=0; if (Mx[k]<x) xx=x-Mx[k]; if (x<Nx[k]) xx=Nx[k]-x; if (My[k]<y) yy=y-My[k]; if (y<Ny[k]) yy=Ny[k]-y; return (xx*xx+yy*yy); } void query(ll k,ll x,ll y) { ll dl=INF,dr=INF,d=dis(p[k].d[0],p[k].d[1],x,y); if (d) ans=min(ans,d); if (ls[k]) dl=dis(ls[k],x,y); if (rs[k]) dr=dis(rs[k],x,y); if (dl<dr) { if (dl<ans) query(ls[k],x,y); if (dr<ans) query(rs[k],x,y); } else { if (dr<ans) query(rs[k],x,y); if (dl<ans) query(ls[k],x,y); } } }K; int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); ll T; read(T); while (T--) { // K.clear(); read(n); rep(i,1,n) read(p[i].d[0]),read(p[i].d[1]),p[i].f=i; ll rt=K.build(1,n,0); rep(i,1,n) { ans=INF; x=p[id[i]].d[0],y=p[id[i]].d[1]; K.query(rt,x,y); // cout<<ans<<endl; printf("%lld ",ans); } } return 0; }
BZOJ2648
BZOJ3053