[BZOJ3212] Pku3468 A Simple Problem with Integers

传送门

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

题目大意

区间修改,区间查询和

题解

水题
树状数组版本 POJ 954MS

const
    maxn=200000;
type
    data=array[0..maxn]of int64;
var
    x,y,z,c,d:data;
    i,j,k,l:longint;
    n,m,t:longint;
    a,b,val,ans:int64;
    cha:char;
procedure update(var a:data;pos,val:int64);
begin
    while pos<=n do
        begin
            a[pos]:=a[pos]+val;
            inc(pos,pos and (-pos));
        end;
end;

function query(var a:data;pos:int64):int64;
var sum:int64;
begin
    sum:=0;
    while pos>0 do
        begin
            sum:=sum+a[pos];
            dec(pos,pos and (-pos));
        end;
    exit(sum);
end;

begin
    readln(n,m); x[0]:=0;
    for i:=1 to n do
        begin
            read(x[i]);
            y[i]:=x[i]-x[i-1];
            z[i]:=y[i]*i;
            update(c,i,y[i]);
            update(d,i,z[i]);
        end;
    readln;
    for i:=1 to m do
        begin
            read(cha);
            if cha='Q'
            then
                begin
                    readln(a,b);
                    ans:=((b+1)*query(c,b)-query(d,b))-((a)*query(c,a-1)-query(d,a-1));
                    writeln(ans);
                end
            else
                begin
                    readln(a,b,val);
                    update(c,a,val); update(c,b+1,-val);
                    y[a]:=y[a]+val; y[b+1]:=y[b+1]-val;
                    update(d,a,y[a]*a-z[a]); update(d,b+1,y[b+1]*(b+1)-z[b+1]);
                    z[a]:=y[a]*a; z[b+1]:=y[b+1]*(b+1);
                end;
        end;
end.

线段树版本 POJ 2172MS

const
 maxn=100005;
type
 aa=record
  l,r:longint; val,tag:int64;
 end;
var
 w:array[0..8*maxn]of aa;
 i,j,k:longint;
 n,m:longint;
 a,b,c:longint;
 ch:char;
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 begin read(w[a].val); exit; end;
 mid:=(l+r)>>1;
 build(a<<1,l,mid); build(a<<1+1,mid+1,r);
 w[a].val:=w[a<<1].val+w[a<<1+1].val;
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*(w[a<<1].r-w[a<<1].l+1));
 inc(w[a<<1+1].val,w[a].tag*(w[a<<1+1].r-w[a<<1+1].l+1));
 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,c:longint);
var mid:longint;
begin
 if w[a].tag<>0 then pushdown(a);
 if (l=w[a].l)and(w[a].r=r) then begin inc(w[a].val,c*(w[a].r-w[a].l+1)); 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:=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(w[a].r=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(query(a<<1,l,mid)+query(a<<1+1,mid+1,r));
end;

begin
 readln(n,m);
 build(1,1,n); readln;
 for i:=1 to m do
  begin
   read(ch);
   case ch of
   'Q':begin readln(a,b); writeln(query(1,a,b)); end;
   'C':begin readln(a,b,c); update(1,a,b,c); end;
   end;
  end;
end.

你可能感兴趣的:([BZOJ3212] Pku3468 A Simple Problem with Integers)