线段树+树状数组模板

线段树

题目为区间改变与求最大值

var
    a,tree,add:array[1..1000000]of longint;
    i,n,m,x,y,z,c:longint;
function max(a,b:longint):longint;
begin
    if a>b then exit(a) else exit(b);
end;

function build(v,i,j:longint):longint;
begin
    if i=j then
    begin
        tree[v]:=a[i];
        build:=a[i];
    end
    else
    begin
        tree[v]:=max(build(v*2,i,(i+j) div 2),build(v*2+1,((i+j) div 2)+1,j));
        build:=tree[v];
    end;
end;

procedure insert(v,i,j,x,y,c:longint);
begin
    if (i=x)and(y=j) then
    begin
        inc(add[v],c);
        inc(tree[v],c)
    end
    else
    begin
        inc(add[v*2],add[v]);
        inc(tree[v*2],add[v]);
        inc(add[v*2+1],add[v]);
        inc(tree[v*2+1],add[v]);
        add[v]:=0;
        if y<=(i+j)div 2 then insert(v*2,i,(i+j)div 2,x,y,c)
        else if x>(i+j)div 2 then insert(v*2+1,((i+j)div 2)+1,j,x,y,c)
             else
             begin
                 insert(v*2,i,(i+j)div 2,x,(i+j)div 2,c);
                 insert(v*2+1,((i+j)div 2)+1,j,((i+j)div 2)+1,y,c);
             end;
        tree[v]:=max(tree[v*2],tree[v*2+1]);
    end;
end;

function find(v,i,j,x,y:longint):longint;
begin
    if (x=i)and(y=j) then
    begin
        find:=tree[v];
    end
    else
    begin
        inc(add[v*2],add[v]);
        inc(tree[v*2],add[v]);
        inc(add[v*2+1],add[v]);
        inc(tree[v*2+1],add[v]);
        add[v]:=0;
        if y<=(i+j)div 2 then find:=find(v*2,i,(i+j)div 2,x,y)
        else if x>((i+j)div 2) then find:=find(v*2+1,((i+j)div 2)+1,j,x,y)
             else
             begin
                 find:=max(find(v*2,i,(i+j)div 2,x,(i+j)div 2),find(v*2+1,((i+j)div 2)+1,j,((i+j)div 2)+1,y));
             end;
    end;
end;

begin
    assign(input,'xianduanshu.in'); reset(input);
    assign(output,'xianduanshu.out'); rewrite(output);
    read(n);
    for i:=1 to n do read(a[i]);
    tree[1]:=build(1,1,n);
    read(m);
    for i:=1 to m do
    begin
        read(x,y,z);
        if x=1 then begin read(c); insert(1,1,n,y,z,c); end;
        if x=2 then writeln(find(1,1,n,y,z));
    end;
    close(input); close(output);
end.

树状数组

题目为单点加值与区间求和

var
    ch:char;
    i,n,m,x,y:longint;
    a,c:array[1..100000] of longint;
function lowbit(a:longint):longint;
begin
    lowbit:=a and (-a);
end;

procedure insert(i,v:longint);
begin
    while i<=n do
    begin
        c[i]:=c[i]+v;
        i:=i+lowbit(i);
    end;
end;

function getsum(i:longint):longint;
var
    sum:longint;
begin
    sum:=0;
    while i>0 do
    begin
        sum:=sum+c[i];
        i:=i-lowbit(i);
    end;
    getsum:=sum;
end;

begin
    assign(input,'shuzhuangshuzu.in'); reset(input);
    assign(output,'shuzhuangshuzu.out'); rewrite(output);
    read(n);
    for i:=1 to n do begin read(a[i]); insert(i,a[i]); end;
    readln(m);
    for i:=1 to m do
    begin
        readln(ch,x,y);
        case ch of
        'C':begin
                insert(x,y-a[x]);
                a[x]:=y;
            end;
        'Q':writeln(getsum(y)-getsum(x-1));
        end;
    end;
    close(input); close(output);
end.

你可能感兴趣的:(线段树,树状数组)