Cod sursa(job #562502)

Utilizator andrei31Andrei Datcu andrei31 Data 23 martie 2011 10:35:28
Problema Datorii Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1 kb
var t:array[1..1 shl 15] of longint;
    buf1,buf2:array[1..200000] of char;

procedure update(nod,l,r,p,v:longint);
var m:longint;
begin
m:=(l+r) shr 1;
t[nod]:=t[nod]-v;
if l<r then
 if p<=m then update(nod shl 1,l,m,p,v)
  else update(nod shl 1+1,m+1,r,p,v);
end;

function query(nod,l,r,a,b:longint):longint;
var tt,m:longint;
begin
tt:=0;
if (a<=l) and (r<=b) then tt:=t[nod]
 else
   begin
   m:=(l+r) shr 1;
   if a<=m then tt:=tt+query(nod shl 1,l,m,a,b);
   if m<b then tt:=tt+query(nod shl 1+1,m+1,r,a,b);
   end;
query:=tt;
end;

procedure citire;
var i,n,m,x,y,op:longword;
begin
assign(input,'datorii.in');settextbuf(input,buf1);reset(input);
readln(n,m);
for i:=1 to n do
 begin
 read(x);
 update(1,1,n,i,-x);
 end;
assign(output,'datorii.out');settextbuf(output,buf2);rewrite(output);
for i:=1 to m do
 begin
 readln(op,x,y);
 if op=0 then
   update(1,1,n,x,y)
    else writeln(query(1,1,n,x,y));
 end;
close(input);close(output);
end;

begin
citire;
end.