Cod sursa(job #215909)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 21 octombrie 2008 18:45:39
Problema Datorii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.14 kb
var a:array[1..40000] of longint;
    f,g:text;
    suma,op,i,n,m,dat,ziua,x,y:longint;

procedure update(nod,st,dr:longint);
 var mij:longint;
 begin
  if nod<=n shl 1 then
   if st=dr then
    inc(a[nod],dat)
   else begin
    mij:=(st+dr) shr 1;
    if ziua<=mij then
     update(nod shl 1,st,mij)
    else
     update(nod shl 1+1,mij+1,dr);
    a[nod]:=a[nod shl 1]+a[nod shl 1+1];
   end;
 end;

procedure query(nod,st,dr:longint);
 var mij:longint;
 begin
  if nod<=n shl 1 then
   if (x<=st) and (y>=dr) then
    suma:=suma+a[nod]
   else
    if st<dr then begin
     mij:=(st+dr) shr 1;
     if x<=mij then
      query(nod shl 1,st,mij);
     if y>mij then
      query(nod shl 1+1,mij+1,dr);
    end;
 end;

begin
 assign(f,'datorii.in'); reset(f);
 assign(g,'datorii.out'); rewrite(g);
 read(f,n,m);
 for ziua:=1 to n do begin
  read(f,dat);
  update(1,1,n);
 end;
 for i:=1 to m do begin
  read(f,op);
  if op=1 then begin
   read(f,x,y);
   suma:=0;
   query(1,1,n);
   writeln(g,suma);
  end
  else begin
   read(f,ziua,dat);
   dat:=-dat;
   update(1,1,n);
  end;
 end;
 close(f); close(g);
end.