bzoj 1941 Hide and Seek

题目大意:

n个点,求每个点到其最远点距离-到其最近点距离(除自己之外)的最小值

思路:

对于估计函数的理解还不够深刻

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #include<vector>
  9 #include<map>
 10 #include<set>
 11 #define ll long long
 12 #define inf 2139062143
 13 #define MAXN 500100
 14 #define MOD 998244353
 15 #define rep(i,s,t) for(register int i=(s),i##__end=(t);i<=i##__end;++i)
 16 #define dwn(i,s,t) for(register int i=(s),i##__end=(t);i>=i##__end;--i)
 17 #define ren(x) for(register int i=fst[x];i;i=nxt[i])
 18 #define pb(i,x) vec[i].push_back(x)
 19 #define pls(a,b) (a+b)%MOD
 20 #define mns(a,b) (a-b+MOD)%MOD
 21 #define mul(a,b) (1LL*(a)*(b))%MOD
 22 using namespace std;
 23 inline int read()
 24 {
 25     int x=0,f=1;char ch=getchar();
 26     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
 27     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
 28     return x*f;
 29 }
 30 int rt,n,Dim,ret1,ret2,ans;
 31 struct node{int mx[2],mn[2],d[2],l,r;}tr[MAXN],g;
 32 bool operator < (const node a,const node b) {return a.d[Dim]<b.d[Dim];}
 33 struct kd_tree
 34 {
 35     void upd(int k)
 36     {
 37         node gl=tr[tr[k].l],gr=tr[tr[k].r];
 38         rep(i,0,1)
 39         {
 40             if(tr[k].l) tr[k].mn[i]=min(tr[k].mn[i],gl.mn[i]),tr[k].mx[i]=max(tr[k].mx[i],gl.mx[i]);
 41             if(tr[k].r) tr[k].mn[i]=min(tr[k].mn[i],gr.mn[i]),tr[k].mx[i]=max(tr[k].mx[i],gr.mx[i]);
 42         }
 43     }
 44     int build(int l,int r,int D)
 45     {
 46         int mid=l+r>>1;Dim=D;nth_element(tr+l,tr+mid,tr+r+1);
 47         rep(i,0,1) tr[mid].mn[i]=tr[mid].mx[i]=tr[mid].d[i];
 48         if(l<mid) tr[mid].l=build(l,mid-1,D^1);
 49         if(mid<r) tr[mid].r=build(mid+1,r,D^1);
 50         upd(mid);return mid;
 51     }
 52     int Get1(int k,int res=0)
 53     {
 54         rep(i,0,1) res+=max(abs(g.d[i]-tr[k].mn[i]),abs(g.d[i]-tr[k].mx[i]));
 55         return res;
 56     }
 57     int Get2(int k,int res=0)
 58     {
 59         rep(i,0,1) res+=max(0,tr[k].mn[i]-g.d[i]);
 60         rep(i,0,1) res+=max(0,g.d[i]-tr[k].mx[i]);return res;
 61     }
 62     int dis(int k,int res=0) {rep(i,0,1) res+=abs(tr[k].d[i]-g.d[i]);return res;}
 63     void query(int k)
 64     {
 65         int dl1=0,dr1=0,dl2=inf,dr2=inf;
 66         ret1=max(ret1,dis(k));if(dis(k)) ret2=min(ret2,dis(k));
 67         if(tr[k].l) dl1=Get1(tr[k].l),dl2=Get2(tr[k].l);
 68         if(tr[k].r) dr1=Get1(tr[k].r),dr2=Get2(tr[k].r);
 69         if(dl1>dr1)
 70         {
 71             if(dl1>ret1)
 72             {
 73                 query(tr[k].l);
 74                 if(dr1>ret1||dr2<ret2) query(tr[k].r);
 75             }else
 76             {
 77                 if(dr2<ret2) query(tr[k].r);
 78                 if(dl2<ret2) query(tr[k].l);
 79             }
 80         }else
 81         {
 82             if(dr1>ret1)
 83             {
 84                 query(tr[k].r);
 85                 if(dl1>ret1||dl2<ret2) query(tr[k].l);
 86             }else
 87             {
 88                 if(dl2<ret2) query(tr[k].l);
 89                 if(dr2<ret2) query(tr[k].r);
 90             }
 91         }
 92     }
 93 }kd;
 94 int main()
 95 {
 96     n=read();rep(i,1,n) tr[i].d[0]=read(),tr[i].d[1]=read();
 97     rt=kd.build(1,n,0),ans=inf;
 98     rep(i,1,n)
 99     {
100         g=tr[i],ret1=0,ret2=inf;kd.query(rt);
101         ans=min(ans,ret1-ret2);
102     }
103     printf("%d\n",ans);
104 }
View Code

 

posted @ 2019-02-27 18:17  jack_yyc  阅读(154)  评论(0编辑  收藏  举报