比赛 20110723 评测结果 AAWWWWWT
题目名称 儿童节快乐 最终得分 25
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-23 11:53:58
显示代码纯文本
var
  n,total,q,x,y,z,i,p:longint;
  ch:char;
  g,left,right,lefts,rights,sum,fa,ans:array[0..1000000]of longint;

procedure build(t,l,r:longint);
var
   mid:longint;
begin
  left[t]:=l;
  right[t]:=r;
  mid:=(left[t]+right[t]) div 2;
  if l=r then
  begin
    ans[t]:=t;
    exit;
  end;

  inc(total);
  lefts[t]:=total;
  fa[total]:=t;
  build(total,l,mid);

  inc(total);
  rights[t]:=total;
  fa[total]:=t;
  build(total,mid+1,r);
end;

procedure add(t,delta:longint);
begin
    g[t]:=g[t]+delta;
    sum[t]:=sum[t]+delta;
end;

procedure putdown(t:longint);
begin
      add(lefts[t],g[t]);
      add(rights[t],g[t]);
      g[t]:=0;
end;

procedure change(t,l,r:longint);
var
  mid:longint;
begin
  if (l=left[t])and(r=right[t]) then
  begin
    add(t,z);
    exit;
  end;
  putdown(t);
  mid:=(left[t]+right[t]) div 2;
  if r<=mid then change(lefts[t],l,r)
  else if l>mid then change(rights[t],l,r)
  else begin
	  change(lefts[t],l,mid);
	  change(rights[t],mid+1,r);
  end;
  if sum[lefts[t]]>=sum[rights[t]] then
  begin
    sum[t]:=sum[lefts[t]];
    ans[t]:=ans[lefts[t]];
  end
  else begin
    sum[t]:=sum[rights[t]];
    ans[t]:=ans[rights[t]];
  end;
end;

function ask(t,l,r:longint):longint;
var
  mid,nowl,nowr:longint;
begin
  if (l=left[t])and(r=right[t]) then exit(ans[t]);
  putdown(t);
  mid:=(left[t]+right[t]) div 2;
  if r<=mid then ask:=ask(lefts[t],l,r)
  else if l>mid then ask:=ask(rights[t],l,r)
  else begin
	nowl:=ask(lefts[t],l,mid);
	nowr:=ask(rights[t],mid+1,r);
    if sum[nowl]>=sum[nowr] then ask:=nowl
    else ask:=nowr;
  end;
  if sum[lefts[t]]>=sum[rights[t]] then
  begin
    sum[t]:=sum[lefts[t]];
    ans[t]:=ans[lefts[t]];
  end
  else begin
    sum[t]:=sum[rights[t]];
    ans[t]:=ans[rights[t]];
  end;
end;

procedure up(t:longint);
var
  k:longint;
begin
  k:=fa[t];
  if sum[lefts[k]]>=sum[rights[k]] then
  begin
    sum[k]:=sum[lefts[k]];
    ans[k]:=ans[lefts[k]];
  end
  else begin
    sum[k]:=sum[rights[k]];
    ans[k]:=ans[rights[k]];
  end;
end;

begin
  assign(input,'happya.in'); reset(input);
  assign(output,'happya.out'); rewrite(output);
  readln(n,q);
  total:=1;
  build(1,1,n);
  for i:=1 to q do
  begin
    read(ch);
    if ch='I' then
    begin
      readln(x,y,z);
      change(1,x,y);
    end
    else begin
      readln(x,y);
      p:=ask(1,x,y);
      writeln(sum[p]);
      sum[p]:=0;
      up(p);
    end;
  end;
  close(input);
  close(output);
end.