Puzzle: http://www.cnblogs.com/zyfzyf/p/4105184.html
1#include <cstdio>2#include <algorithm>3#include <cmath>4 using namespacestd;5 #defineLson rt<<1,l,m6 #defineRson Rt<<1|1,m+1,r7 intnum,ch[ A],f,c;8InlinevoidRint&x) {9C=0; f=1;Ten for(;c<'0'|| C>'9'; C=getchar ())if(c=='-') f=-1; One for(x=0; c>='0'&&c<='9'; C=getchar ()) (x*=Ten) + = (c'0'); Ax*=F; - } -typedefLong Longll; the intn,m,w[1000001],now[1000001],b[1000001],fa[1000001]; -ll ans,maxv[4000001],delta[4000001]; - voidPushdown (intRT) - { + if(Delta[rt]) - { +delta[rt<<1]+=DELTA[RT]; delta[rt<<1|1]+=Delta[rt]; Amaxv[rt<<1]+=DELTA[RT]; maxv[rt<<1|1]+=Delta[rt]; atdelta[rt]=0; - } - } - voidUpdateintQlintQrintVintRtintLintR) - { - if(ql<=l&&r<=qr) in { -delta[rt]+=(ll) v; tomaxv[rt]+=(ll) v; + return; - } thePushdown (RT);intM=l+r>>1; * if(ql<=m) update (Ql,qr,v,lson); $ if(m<qr) update (Ql,qr,v,rson);Panax NotoginsengMaxv[rt]=max (maxv[rt<<1],maxv[rt<<1|1]); - } thell query (intQrintRtintLintR) + { A if(1<=L&&R<=QR)returnMaxv[rt]; the pushdown (RT); + intM=l+r>>1; ll res=0; - if(1<=M) res=Max (Res,query (Qr,lson)); $ if(M<QR) res=Max (Res,query (Qr,rson)); $ returnRes; - } - intMain () the { - R (n); R (m);Wuyi for(intI=1; i<=n;++i) R (B[i]); the for(intI=1; i<=m;++i) R (W[i]); - for(intI=1; i<=n;++i) Wu { -fa[i]=Now[b[i]]; Aboutnow[b[i]]=i; $ } - for(intI=1; i<=n;++i) - { -Update (fa[i]+1, I, (LL) W[b[i]],1,1, n); A if(Fa[i]) update (fa[fa[i]]+1, Fa[i], (LL) (-w[b[i]),1,1, n); +Ans=max (Ans,query (i,1,1, N)); the} printf ("%lld\n", ans); - return 0; $}
"Line segment Tree" bzoj3747 [Poi2015]kinoman