1. 程式人生 > >[線段樹 均攤複雜度] BZOJ 2130 魔塔

[線段樹 均攤複雜度] BZOJ 2130 魔塔

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9'
);c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } const int N=100005; int F[N<<2]; int vm[N<<2],lm[N<<2],rm[N<<2]; int n,K[N],a[N],b[N],c[N],sa[N],sb[N],sc[N],pb[N],pc[N]; int f[N]; inline void upd(int x){ lm[x]=lm[x<<1]; rm[x]=rm[x<<1
|1]; vm[x]=max(vm[x<<1],vm[x<<1|1]); } inline void Build(int x,int l,int r){ F[x]=-1; if (l==r){ lm[x]=rm[x]=f[l],vm[x]=f[l]+sb[l]; return; } int mid=(l+r)>>1; Build(x<<1,l,mid); Build(x<<1|1,mid+1,r); upd(x); } inline void mark(int x,int r,int t){ F[x]=lm[x]=rm[x]=t; vm[x]=t+sb[r]; } inline
void Modify(int x,int l,int r,int ql,int qr,int t){ if (ql<=l && r<=qr){ if (lm[x]<=t) return; if (rm[x]>=t) { mark(x,r,t); return; } } int mid=(l+r)>>1; if (F[x]!=-1) mark(x<<1,mid,F[x]),mark(x<<1|1,r,F[x]),F[x]=-1; if (ql<=mid) Modify(x<<1,l,mid,ql,qr,t); if (qr>mid) Modify(x<<1|1,mid+1,r,ql,qr,t); upd(x); } int main(){ int T; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(T); while (T--){ read(n); for (int i=1;i<=n;i++) read(K[i]); for (int i=1;i<=n;i++) read(a[i]); for (int i=1;i<=n;i++) read(b[i]),pb[b[i]]=i; for (int i=1;i<=n;i++) read(c[i]),pc[c[i]]=i; for (int i=1;i<=n;i++) read(sa[i]),sa[i]+=sa[i-1]; for (int i=1;i<=n;i++) read(sb[i]),sb[i]+=sb[i-1]; for (int i=1;i<=n;i++) read(sc[i]),sc[i]+=sc[i-1]; f[0]=sc[n]; for (int i=1;i<=n;i++) if (K[b[i]]==1) f[i]=min(f[i-1],sc[pc[b[i]]-1]); else f[i]=f[i-1]; Build(1,0,n); int ans=vm[1]; for (int i=1;i<=n;i++){ if (K[a[i]]==1){ Modify(1,0,n,pb[a[i]],n,-1<<30); Modify(1,0,n,0,n,sc[pc[a[i]]-1]); }else{ Modify(1,0,n,pb[a[i]],n,sc[pc[a[i]]-1]); } ans=max(ans,sa[i]+vm[1]); } printf("%d\n",ans); } return 0; }