BZOJ1099: [POI2007] Tree Drz

Source: Internet
Author: User

First 1 with I exchange, n with I exchange, I with i+1 exchangeable can $o (n) $ calculated.

Then just consider I and x Exchange (1<i,x<n and |i-x|>1).

Set

A[I]=H[I-1]

B[I]=H[I+1]

f[i]=|h[i-1]-h[i]|+|h[i+1]-h[i]|

C[i]=min (A[i],b[i])

D[i]=max (A[i],b[i])

Then the exchange I with x contributes to the answer as

-F[I]-F[X]

+|a[i]-h[x]|+|b[i]-h[x]|

+|h[i]-a[x]|+|h[i]-b[x]|

The second row is categorized as a discussion:

1.h[x]<=min (A[i],b[i])

A[i]+b[i]-h[x]*2

2.min (A[i],b[i]) <=h[x]<=max (A[i],b[i])

|a[i]-b[i]|

3.h[x]>=max (A[i],b[i])

H[x]*2-a[i]-b[i]

The third row is categorized as a discussion:

1.h[i]<=min (A[x],b[x])

A[x]+b[x]-h[i]*2

2.min (A[x],b[x]) <=h[i]<=max (A[x],b[x])

|a[x]-b[x]|

3.h[i]>=max (A[x],b[x])

H[I]*2-A[X]-B[X]

So in 9 kinds of situations to discuss, with the scan line + line tree to complete the inquiry. Time Complexity $o (n\log N) $.

#include <cstdio> #include <algorithm>using namespace std;typedef long long ll;const int n=50010,inf=~0u> >1;int n,m,i,now,h[n],a[n],b[n],c[n],d[n],f[n],loc[n],v[131073],ans[n];ll H[N],B[N],sum;struct Q{int x,l,r,z,t; Q () {}q (int _x,int _l,int _r,int _z,int _t) {x=_x,l=_l,r=_r,z=_z,t=_t;}} q[n*3];inline bool Cmp1 (Q a,q b) {return a.x==b.x?a.t<b.t:a.x<b.x;} inline bool Cmp2 (Q a,q b) {return a.x==b.x?a.t<b.t:a.x>b.x;}  inline int Getl (ll x) {x=x*n;  int l=1,r=n,mid,t;  while (l<=r) if (b[mid= (l+r) >>1]>=x) r= (t=mid) -1;else l=mid+1; return t;}  inline int Getr (ll x) {x=x*n+n-1;  int l=1,r=n,mid,t;  while (l<=r) if (b[mid= (l+r) >>1]<=x) l= (t=mid) +1;else r=mid-1; return t;}  inline int Getx (ll x) {int l=1,r=n,mid,t;  while (l<=r) if (b[mid= (l+r) >>1]>=x) r= (t=mid) -1;else l=mid+1; return t;} inline int abs (int x) {return x>0?x:-x;} inline void up (Int&a,int b) {if (a>b) a=b;} inline void Read (int&a) {char c;while (!) ( ((C=getchar ()) >= ' 0 ') &&(c<= ' 9 ')), a=c-' 0 ', while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*=10) +=c-' 0 ';}  void build (int x,int A,int b) {v[x]=inf;  if (a==b) return;  int mid= (A+B) >>1; Build (X<<1,a,mid), build (X&LT;&LT;1|1,MID+1,B);}  void change (int x,int a,int b,int c,int p) {if (a==b) {V[x]=p;return;}  int mid= (A+B) >>1;  C<=mid?change (x<<1,a,mid,c,p): Change (X&LT;&LT;1|1,MID+1,B,C,P); V[x]=min (v[x<<1],v[x<<1|1]);} void Ask (int x,int a,int b,int c,int D) {if (c>d| |  V[x]>=now) return;  if (C<=a&&b<=d) {up (now,v[x]); return;}  int mid= (A+B) >>1;  if (C<=mid) ask (X&LT;&LT;1,A,MID,C,D); if (D>mid) ask (X<<1|1,mid+1,b,c,d);}  inline void query (int l,int r,int p) {int x=loc[p-1],y=loc[p+1];  if (x>y) swap (x, y); if (y<l| |    X>r) {ask (1,1,n,l,r);  Return    } if (l<=x&&y<=r) {ask (1,1,n,l,x-1);    Ask (1,1,n,x+1,y-1);    Ask (1,1,n,y+1,r);  Return    } if (l<=x) {ask (1,1,n,l,x-1);    Ask (1,1,n,x+1,r);  Return } ASK (1,1,n,l,y-1); Ask (1,1,n,y+1,r);}    void S11 () {for (m=0,i=2;i<n;i++) {q[++m]=q (c[i],loc[i],0,a[i]+b[i]-h[i]*2-f[i],0);  Q[++m]=q (H[i],getr (C[i]), 0,a[i]+b[i]-h[i]*2-f[i],i);    } for (sort (Q+1,Q+M+1,CMP2), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (1,q[i].l,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S12 () {for (m=0,i=2;i<n;i++) {q[++m]=q (C[i],loc[i],0,abs (A[i]-b[i])-h[i]*2-f[i],0);    Q[++m]=q (D[i],loc[i],0,0,n);  Q[++m]=q (H[i],getr (C[i]), 0,a[i]+b[i]-f[i],i);    } for (sort (Q+1,Q+M+1,CMP1), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);    else if (q[i].t==n) change (1,1,n,q[i].l,inf);      else{now=inf,query (1,q[i].l,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S13 () {for (m=0,i=2;i<n;i++) {q[++m]=q (d[i],loc[i],0,-h[i]*2-a[i]-b[i]-f[i],0);  Q[++m]=q (H[i],getr (C[i]), 0,a[i]+b[i]+h[i]*2-f[i],i); } for (sort (Q+1,Q+M+1,CMP1), build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (1,q[i].l,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S21 () {for (m=0,i=2;i<n;i++) {q[++m]=q (c[i],loc[i],0,a[i]+b[i]-f[i],0);  Q[++m]=q (H[i],getl (C[i]), GETR (D[i]), ABS (A[i]-b[i])-h[i]*2-f[i],i);    } for (sort (Q+1,Q+M+1,CMP2), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (q[i].l,q[i].r,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S22 () {for (m=0,i=2;i<n;i++) {q[++m]=q (C[i],loc[i],0,abs (A[i]-b[i])-f[i],0);    Q[++m]=q (D[i],loc[i],0,0,n);  Q[++m]=q (H[i],getl (C[i]), GETR (D[i]), ABS (A[i]-b[i])-f[i],i);    } for (sort (Q+1,Q+M+1,CMP1), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);    else if (q[i].t==n) change (1,1,n,q[i].l,inf);      else{now=inf,query (q[i].l,q[i].r,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now); }}}void S23 () {for (m=0,i=2;i<n;i++){q[++m]=q (d[i],loc[i],0,-a[i]-b[i]-f[i],0);  Q[++m]=q (H[i],getl (C[i]), GETR (D[i]), ABS (A[i]-b[i]) +h[i]*2-f[i],i);    } for (sort (Q+1,Q+M+1,CMP1), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (q[i].l,q[i].r,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S31 () {for (m=0,i=2;i<n;i++) {q[++m]=q (c[i],loc[i],0,a[i]+b[i]+h[i]*2-f[i],0);  Q[++m]=q (H[i],getl (D[i]), 0,-a[i]-b[i]-h[i]*2-f[i],i);    } for (sort (Q+1,Q+M+1,CMP2), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (q[i].l,n,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S32 () {for (m=0,i=2;i<n;i++) {q[++m]=q (C[i],loc[i],0,abs (A[i]-b[i]) +h[i]*2-f[i],0);    Q[++m]=q (D[i],loc[i],0,0,n);  Q[++m]=q (H[i],getl (D[i]), 0,-a[i]-b[i]-f[i],i);    } for (sort (Q+1,Q+M+1,CMP1), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z); else if (q[i].t==n) Change (1,1,n,q[i].l,iNF);      else{now=inf,query (q[i].l,n,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);    }}}void S33 () {for (m=0,i=2;i<n;i++) {q[++m]=q (d[i],loc[i],0,h[i]*2-a[i]-b[i]-f[i],0);  Q[++m]=q (H[i],getl (D[i]), 0,h[i]*2-a[i]-b[i]-f[i],i);    } for (sort (Q+1,Q+M+1,CMP1), Build (1,1,n), i=1;i<=m;i++) {if (!q[i].t) change (1,1,N,Q[I].L,Q[I].Z);      else{now=inf,query (q[i].l,n,q[i].t);    if (Now<inf) up (Ans[q[i].t],q[i].z+now);  }}}int Main () {for (read (n), i=1;i<=n;i++) read (H[i]);  for (i=1;i<n;i++) Sum+=abs (h[i]-h[i+1]);    if (n>2) {f[1]=abs (h[1]-h[2]), F[n]=abs (h[n]-h[n-1]);      for (i=2;i<n;i++) {a[i]=h[i-1],b[i]=h[i+1];      C[i]=min (A[i],b[i]), D[i]=max (A[i],b[i]);    F[i]=abs (H[i]-h[i-1]) +abs (h[i]-h[i+1]);      } for (i=3;i<n;i++) {Up (Ans[1],abs (h[i]-h[2]) +abs (h[1]-h[i-1]) +abs (h[1]-h[i+1])-f[1]-f[i]);    Up (Ans[i],abs (h[i]-h[2]) +abs (h[1]-h[i-1]) +abs (h[1]-h[i+1)-f[1]-f[i]); } for (i=2;i<n-1;i++) {Up (Ans[n],abs (h[i]-h[n-1]) +ABS (H[n]-h[i-1]) +abs (h[n]-h[i+1])-f[n]-f[i]);    Up (Ans[i],abs (h[i]-h[n-1]) +abs (h[n]-h[i-1]) +abs (h[n]-h[i+1)-f[n]-f[i]);    } Up (Ans[1],abs (h[1]-h[n-1]) +abs (h[n]-h[2])-f[1]-f[n]);    Up (Ans[n],abs (h[1]-h[n-1]) +abs (h[n]-h[2])-f[1]-f[n]); if (n>2) {for (i=2;i<n-1;i++) {Up (Ans[i],abs (h[i-1]-h[i+1]) +abs (h[i]-h[i+1]) +abs (H[i+2]-h[i])-f[i]-f[i+1]+        ABS (h[i]-h[i+1]));      Up (Ans[i+1],abs (h[i-1]-h[i+1]) +abs (h[i]-h[i+1]) +abs (H[i+2]-h[i])-f[i]-f[i+1]+abs (h[i]-h[i+1]));      } Up (Ans[1],abs (h[1]-h[3])-abs (h[2]-h[3]));      Up (Ans[2],abs (h[1]-h[3])-abs (h[2]-h[3]));      Up (Ans[n],abs (h[n]-h[n-2])-abs (h[n-1]-h[n-2]));    Up (Ans[n-1],abs (h[n]-h[n-2])-abs (h[n-1]-h[n-2]));      } if (n>4) {for (i=1;i<=n;i++) b[i]=h[i]= (LL) h[i]*n+i-1;      Sort (b+1,b+n+1);      for (i=1;i<=n;i++) Loc[i]=getx (H[i]);    S11 (), S12 (), S13 (), S21 (), S22 (), S23 (), S31 (), S32 (), S33 ();  }} for (i=1;i<=n;i++) printf ("%lld\n", Sum+ans[i]); return 0;}

  

BZOJ1099: [POI2007] Tree Drz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.