比赛 20110723 评测结果 AAWWWWWW
题目名称 儿童节快乐 最终得分 25
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-07-23 10:06:11
显示代码纯文本
program happya;
const
  maxn=500000;
type
  point=^node;
  node=record
    l,r,num,s:longint;
    left,right,fa:point;
  end;
var
  tree:array[0..maxn] of node;
  n,m,i,a,b,c,tot:longint;
  root,nill,t0:point;
  ch:char;

function max(a,b:longint):longint;
begin
  if a>b then max:=a else max:=b;
end;

procedure maketree(var t:point;l,r:longint);
var
  mid:longint;
begin
  tot:=tot+1;
  t:=@tree[tot];
  t^.l:=l;
  t^.r:=r;
  t^.num:=0;
  t^.s:=0;
  t^.left:=nill;
  t^.right:=nill;
  t^.fa:=nill;
  if l<r then
  begin
    mid:=(l+r) div 2;
    maketree(t^.left,l,mid);
    t^.left^.fa:=t;
    maketree(t^.right,mid+1,r);
    t^.right^.fa:=t;
  end;
end;

procedure pushdown(t:point);
begin
  if t^.s>0 then
  begin
    inc(t^.left^.s,t^.s);
    inc(t^.left^.num,t^.s);
    inc(t^.right^.s,t^.s);
    inc(t^.right^.num,t^.s);
    t^.s:=0;
  end;
end;

procedure inserts(t:point;a,b,c:longint);
var
  mid:longint;
begin
  pushdown(t);
  if (a<=t^.l) and (b>=t^.r) then
  begin
    inc(t^.s,c);
    inc(t^.num,c);
  end
  else
  begin
    mid:=(t^.l+t^.r) div 2;
    if a<=mid then inserts(t^.left,a,b,c);
    if b>=mid+1 then inserts(t^.right,a,b,c);
    t^.num:=max(t^.left^.num,t^.right^.num);
  end;
end;  

function find(t:point;a,b:longint):point;
var
  mid,temp:longint;
  tt,t1,t2:point;
begin
  pushdown(t);
  if (a<=t^.l) and (b>=t^.r) then
  begin
    find:=t;
  end
  else
  begin
    mid:=(t^.l+t^.r) div 2;
    temp:=-100;
    tt:=nill;
    if a<=mid then
    begin
      t1:=find(t^.left,a,b);
      temp:=t1^.num;
      tt:=t1;
    end;      
    if b>=mid+1 then 
    begin
      t2:=find(t^.right,a,b);
      if t2^.num>temp then
      begin
        temp:=t2^.num;
        tt:=t2;
      end;
    end;
    find:=tt;
  end;
end;

procedure deletes(t:point);
begin
  pushdown(t);
  if t^.l=t^.r then
  begin
    t^.num:=0;
  end
  else
  begin
    if t^.left^.num=t^.num 
      then deletes(t^.left)
      else deletes(t^.right);
    t^.num:=max(t^.left^.num,t^.right^.num);
  end;
end;

procedure delete0(t:point);
var
  tt:point;
begin
  tt:=t^.fa;
  while tt<>nill do
  begin
    tt^.num:=max(tt^.left^.num,tt^.right^.num);
    tt:=tt^.fa;
  end;
end;

begin
  assign(input,'happya.in');
  reset(input);
  assign(output,'happya.out');
  rewrite(output);
  
  readln(n,m);
  tot:=0;
  nill:=@tree[0];
  maketree(root,1,n);
  for i:=1 to m do
  begin
    read(ch);
    if ch='I' then
    begin
      readln(a,b,c);
      inserts(root,a,b,c);
    end
    else
    begin
      readln(a,b);
      t0:=find(root,a,b);
      writeln(t0^.num);
      deletes(t0);
      delete0(t0);
    end;
  end;
  
  close(input);
  close(output);
end.