1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include<cstring> #include<algorithm> using namespace std; int (int x,int y) {return x<y?x:y;} int mymax(int x,int y) {return x>y?x:y;} int myabs(int x) {return x>0?x:-x;}
const int MAXN=510000; const int INF=0x3f3f3f3f;
struct nod { int d[2]; int son[2]; int mi[2],mx[2]; nod() { son[0]=son[1]=0; } }p[MAXN]; int root;
int D; bool cmp(nod a,nod b) { return (a.d[D]<b.d[D]) or (a.d[D]==b.d[D] and a.d[1-D]<b.d[1-D]); } void update(int f,int x) { p[f].mi[0]=mymin(p[f].mi[0],p[x].mi[0]);p[f].mx[0]=mymax(p[f].mx[0],p[x].mx[0]); p[f].mi[1]=mymin(p[f].mi[1],p[x].mi[1]);p[f].mx[1]=mymax(p[f].mx[1],p[x].mx[1]); } int getdis(int t,int x,int y,int op) { if(op==0) { int ans=0; if(x<p[t].mi[0]) ans+=p[t].mi[0]-x; if(p[t].mx[0]<x) ans+=x-p[t].mx[0]; if(p[t].mx[1]<y) ans+=y-p[t].mx[1]; if(y<p[t].mi[1]) ans+=p[t].mi[1]-y; return ans; } else { return mymax(myabs(x-p[t].mx[0]),myabs(x-p[t].mi[0]))+ mymax(myabs(y-p[t].mx[1]),myabs(y-p[t].mi[1])); } }
int build(int l,int r,int d) { if(l>r) return 0; int mid=(l+r)/2;D=d; nth_element(p+l,p+mid,p+r+1,cmp); p[mid].mi[0]=p[mid].mx[0]=p[mid].d[0]; p[mid].mi[1]=p[mid].mx[1]=p[mid].d[1]; if(l<=mid-1) p[mid].son[0]=build(l,mid-1,1-d),update(mid,p[mid].son[0]); if(mid+1<=r) p[mid].son[1]=build(mid+1,r,1-d),update(mid,p[mid].son[1]); return mid; } int ans; void ask(int now,int x,int y,int op) { int dis=myabs(p[now].d[0]-x)+myabs(p[now].d[1]-y); if(dis!=0) { if(op==0) ans=mymin(ans,dis); else ans=mymax(ans,dis); } int f[2],lc=p[now].son[0],rc=p[now].son[1]; if(op==0) { f[0]=lc?getdis(lc,x,y,op):INF; f[1]=rc?getdis(rc,x,y,op):INF; bool tmp=(f[1]<=f[0]); if(f[tmp]<ans) ask(p[now].son[tmp],x,y,op); if(f[1-tmp]<ans) ask(p[now].son[1-tmp],x,y,op); } else { f[0]=lc?getdis(lc,x,y,op):0; f[1]=rc?getdis(rc,x,y,op):0; bool tmp=(f[1]>=f[0]); if(f[tmp]>ans) ask(p[now].son[tmp],x,y,op); if(f[1-tmp]>ans) ask(p[now].son[1-tmp],x,y,op); } }
int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].d[0],&p[i].d[1]); root=build(1,n,0); int mi=INF; for(int i=1;i<=n;i++) { ans=0; ask(root,p[i].d[0],p[i].d[1],1); int t=ans;ans=INF; ask(root,p[i].d[0],p[i].d[1],0); mi=mymin(mi,t-ans); } printf("%d",mi); }
|
近期评论