[BZOJ4094] [Usaco2013 Dec]Optimal Milking

传送门

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

题目大意

给定n台机器,相邻两台不能同时使用,对于机器的产量可以修改,询问m天最大产量和

题解

对于线段树维护一下4个权值
1.左端点选,右端点选
2.左端点不选,右端点选
3.左端点选,右端点不选
4.左端点不选,右端点不选

var
 w:array[0..160005,1..6]of int64;
 i,j,k:longint;
 n,m,a,b:longint;
 ans:int64;
function max(a,b:Longint):longint;
begin
 if a>b then exit(a) else exit(b);
end;

procedure pushup(a:longint);
var b,c:longint;
begin
 b:=a<<1; c:=b+1;
 w[a,3]:=max(max(w[b,5]+w[c,3],w[b,3]+w[c,4]),w[b,3]+w[c,3]);
 w[a,4]:=max(max(w[b,4]+w[c,3],w[b,4]+w[c,4]),w[b,6]+w[c,3]);
 w[a,5]:=max(max(w[b,3]+w[c,5],w[b,5]+w[c,5]),w[b,3]+w[c,6]);
 w[a,6]:=max(max(w[b,4]+w[c,5],w[b,6]+w[c,5]),w[b,4]+w[c,6]);
end;

procedure build(a,l,r:longint);
var mid:longint;
begin
 w[a,1]:=l; w[a,2]:=r;
 if l=r then begin
  w[a,3]:=0; readln(w[a,6]); w[a,5]:=0; w[a,4]:=0; exit;
 end;
 mid:=(l+r)>>1;
 build(a<<1,l,mid); build(a<<1+1,mid+1,r);
 pushup(a);
end;

procedure update(a,b,c:longint);
var mid:longint;
begin
 if w[a,1]=w[a,2] then begin
  w[a,3]:=0; w[a,6]:=c; w[a,5]:=0; w[a,4]:=0; exit;
 end;
 mid:=(w[a,1]+w[a,2])>>1;
 if b<=mid then update(a<<1,b,c) else update(a<<1+1,b,c);
 pushup(a);
end;

begin
 readln(n,m);
 build(1,1,n);
 ans:=0;
 for i:=1 to m do
  begin
   readln(a,b);
   update(1,a,b);
   inc(ans,max(max(max(w[1,3],w[1,4]),w[1,5]),w[1,6]));
  end;
 writeln(ans);
end.

你可能感兴趣的:([BZOJ4094] [Usaco2013 Dec]Optimal Milking)