Tyvj1461
来源:互联网 发布:一千个哈姆雷特 知乎 编辑:程序博客网 时间:2024/05/21 00:51
题目链接
分析:
KDtree最远点对
tip
新学的算法就要多加练习
注意:kdtree中的左右儿子不是*2,*2+1这么简单了
一定要调用t[bh].l,t[bh].r
最远距离的计算还是要注意:
这里写代码片#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define ll long longusing namespace std;const int N=100010;struct node{ ll mx[2],mn[2],d[2],l,r;};node t[N];int n,root,cmpd;ll x,y;ll an;int cmp(const node &a,const node &b){ return (a.d[cmpd]<b.d[cmpd])||((a.d[cmpd]==b.d[cmpd])&&(a.d[!cmpd]<b.d[!cmpd]));}ll max(ll a,ll b){if (a>b) return a;else return b;}void update(int bh){ int lc=t[bh].l; int rc=t[bh].r; if (lc) { t[bh].mn[0]=min(t[bh].mn[0],t[lc].mn[0]); t[bh].mn[1]=min(t[bh].mn[1],t[lc].mn[1]); t[bh].mx[0]=max(t[bh].mx[0],t[lc].mx[0]); t[bh].mx[1]=max(t[bh].mx[1],t[lc].mx[1]); } if (rc) { t[bh].mn[0]=min(t[bh].mn[0],t[rc].mn[0]); t[bh].mn[1]=min(t[bh].mn[1],t[rc].mn[1]); t[bh].mx[0]=max(t[bh].mx[0],t[rc].mx[0]); t[bh].mx[1]=max(t[bh].mx[1],t[rc].mx[1]); }}int build(int l,int r,int D){ cmpd=D; int mid=(l+r)>>1; nth_element(t+1+l,t+1+mid,t+1+r,cmp); t[mid].mn[0]=t[mid].mx[0]=t[mid].d[0]; t[mid].mn[1]=t[mid].mx[1]=t[mid].d[1]; if (l!=mid) t[mid].l=build(l,mid-1,!D); if (r!=mid) t[mid].r=build(mid+1,r,!D); update(mid); return mid; }ll sqr(ll x){return x*x;}ll dis(int p,ll x,ll y){ ll d=0; d=sqr(max(abs(t[p].mn[0]-x),abs(t[p].mx[0]-x))); d+=sqr(max(abs(t[p].mn[1]-y),abs(t[p].mx[1]-y))); return d;}void ask(int now){ ll d0,dl,dr; d0=(x-t[now].d[0])*(x-t[now].d[0])+(y-t[now].d[1])*(y-t[now].d[1]); if (t[now].l) dl=dis(t[now].l,x,y); else dl=0; if (t[now].r) dr=dis(t[now].r,x,y); else dr=0; if (d0>an) an=d0; if (dl>dr) { if (dl>an) ask(t[now].l); if (dr>an) ask(t[now].r); } else { if (dr>an) ask(t[now].r); if (dl>an) ask(t[now].l); }}int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld%lld",&t[i].d[0],&t[i].d[1]); root=build(1,n,0); ll ans=0; for (int i=1;i<=n;i++) { x=t[i].d[0]; y=t[i].d[1]; an=0; ask(root); if (an>ans) ans=an; } printf("%lld",ans); return 0;}
阅读全文
0 0