[BZOJ3747] [POI2015]Kinoman

来源:互联网 发布:网络流行bgm 编辑:程序博客网 时间:2024/05/01 23:48

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=3747

题目大意

给定一个序列,对于任意区间的价值为区间内只出现过一次的权值和,询问最大权值和

题解

我们定义点i后与点i相同的位置为next[i]
我们考虑每个点的贡献[i,next[i]1],所以我们最开始将所有第一次出现的种类覆盖的贡献范围加进线段树,然后从左向右枚举左端点,取最大值
取过最值后,这个点就不再贡献了,将[i,next[i]1]减掉它的贡献,再对[next[i],next[next[i]]1]next[i]
注意next[i]next[next[i]]值为0的情况
速度倒第二-_-

const maxn=1000005;type nametype=record l,r:longint; val,tag:int64; end;var w:array[0..4*maxn]of nametype; x,y,z,v,pre,next:array[0..maxn]of longint; i,j,k:longint; n,m:longint; tt,ans:int64;function max(a,b:int64):int64;begin if a>b then exit(a) else exit(b);end;procedure build(a,l,r:longint);var mid:longint;begin w[a].l:=l; w[a].r:=r; w[a].val:=0; w[a].tag:=0; if l=r then exit; mid:=(l+r)>>1; build(a<<1,l,mid); build(a<<1+1,mid+1,r);end;procedure pushdown(a:longint);begin if w[a].l=w[a].r then begin w[a].tag:=0; exit; end; inc(w[a<<1].val,w[a].tag); inc(w[a<<1+1].val,w[a].tag); inc(w[a<<1].tag,w[a].tag); inc(w[a<<1+1].tag,w[a].tag); w[a].tag:=0;end;procedure update(a,l,r:longint;c:int64);var mid:longint;begin if l>r then exit; if w[a].tag<>0 then pushdown(a); if (l=w[a].l)and(r=w[a].r) then begin inc(w[a].val,c); inc(w[a].tag,c); exit; end; mid:=(w[a].l+w[a].r)>>1; if r<=mid then update(a<<1,l,r,c) else if l>mid then update(a<<1+1,l,r,c) else begin update(a<<1,l,mid,c); update(a<<1+1,mid+1,r,c); end; w[a].val:=max(w[a<<1].val,w[a<<1+1].val);end;function query(a,l,r:longint):int64;var mid:longint;begin if w[a].tag<>0 then pushdown(a); if (l=w[a].l)and(r=w[a].r) then exit(w[a].val); mid:=(w[a].l+w[a].r)>>1; if r<=mid then exit(query(a<<1,l,r)) else if l>mid then exit(query(a<<1+1,l,r)) else exit(max(query(a<<1,l,mid),query(a<<1+1,mid+1,r)));end;begin readln(n,m); for i:=1 to n do  begin   read(y[i]);   if pre[y[i]]=0 then v[i]:=1 else v[i]:=0;   next[pre[y[i]]]:=i;   pre[y[i]]:=i;  end; for i:=1 to m do  read(x[i]); build(1,1,n); for i:=1 to n do  if v[i]=1  then   if next[i]=0    then update(1,i,n,x[y[i]])    else update(1,i,next[i]-1,x[y[i]]); ans:=0; for i:=1 to n do  begin   ans:=max(ans,query(1,i,n));   if next[i]=0   then update(1,i,n,-x[y[i]])   else update(1,i,next[i]-1,-x[y[i]]);   if next[i]<>0   then    if next[next[i]]=0    then update(1,next[i],n,x[y[i]])    else update(1,next[i],next[next[i]]-1,x[y[i]]);  end; writeln(ans);end.
0 0
原创粉丝点击