program pro;
var
aa:array[0..100000]of longint;
tree:array[0..200000]of record a,b,co,l,r,max:longint; end;
n,m,a,b,c,tot,i,j:longint;
order:char;
function max(x,y:longint):longint;
begin
if x>y then exit(x)
else exit(y);
end;
procedure maketree(l,r:longint);
var
now,mid:longint;
begin
inc(tot); now:=tot;
tree[tot].a:=l; tree[tot].b:=r;
if l=r then
begin
tree[now].max:=aa[l];
exit;
end;
mid:=(l+r)div 2;
tree[now].l:=tot+1;
maketree(l,mid);
tree[now].r:=tot+1;
maketree(mid+1,r);
tree[now].max:=max(tree[tree[now].l].max,tree[tree[now].r].max);
end;
procedure plus(x:longint);
begin
with tree[x] do
begin
inc(tree[l].co,co);
inc(tree[r].co,co);
inc(tree[l].max,co);
inc(tree[r].max,co);
co:=0;
end;
end;
procedure gx(x:longint);
begin
tree[x].max:=max(tree[tree[x].l].max,tree[tree[x].r].max);
end;
procedure add(num,l,r,c:longint);
var
mid,now:longint;
begin
if tree[num].co<>0 then plus(num);
if (l<=tree[num].a)and(r>=tree[num].b) then
begin
inc(tree[num].co,c);
inc(tree[num].max,c);
exit;
end;
mid:=(tree[num].a+tree[num].b)div 2;
if (l<=mid) then add(tree[num].l,l,r,c);
if (r>mid) then add(tree[num].r,l,r,c);
gx(num);
end;
function find(num,l,r:longint):longint;
var
mid,ans:longint;
begin
if tree[num].co<>0 then plus(num);
if (l<=tree[num].a)and(r>=tree[num].b) then exit(tree[num].max);
mid:=(tree[num].a+tree[num].b)div 2;
ans:=-maxlongint;
if l<=mid then ans:=find(tree[num].l,l,r);
if r>mid then ans:=max(ans,find(tree[num].r,l,r));
exit(ans);
end;
begin
readln(n,m);
tot:=0;
maketree(1,n);
for i:=1 to m do
begin
read(order);
if order='A' then
begin
readln(a,b,c);
add(1,a,b,c);
end;
if order='Q' then
begin
readln(a,b);
writeln(find(1,a,b));
end;
end;
end.
线段树求区间最大值
最新推荐文章于 2024-07-04 22:10:47 发布