Cod sursa(job #108436)

Utilizator h_istvanHevele Istvan h_istvan Data 22 noiembrie 2007 18:33:37
Problema Datorii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.13 kb
program datorii;
var f,fout:Text;
    n,m,i,t,a,b,c:longint;
    v:array[0..15000] of longint;
function bin(x:longint):longint;
var h:longint;
begin
     h:=1;
     while(x mod (2*h) = 0) do h:=2*h;
     bin:=h;
end;

procedure hozzaad(index,ertek:longint);
var t:longint;
begin
     t:=index;
     while(t<=n) do
     begin
          v[t]:=v[t]+ertek;
          t:=t+bin(t);
     end;
end;

function ossz(index:longint):longint;
var t,o:longint;
begin
     o:=0;
     t:=index;
     while(t > 0) do
     begin
          o:=o+v[t];
          t:=t-bin(t);
     end;
     ossz:=o;
end;

function osszeg(k,v:longint):longint;
begin
     osszeg:=ossz(v)-ossz(k-1);
end;

begin
     assign(f,'datorii.in');
     reset(f);
     readln(f,n,m);
     for i:=1 to n do
     begin
          read(f,t);
          hozzaad(i,t);
     end;
     readln(f);
     assign(fout,'datorii.out');
     rewrite(fout);
     for i:=1 to m do
     begin
          readln(f,a,b,c);
          if(a=0) then hozzaad(b,(-1)*c)
                  else writeln(fout,osszeg(b,c));
     end;
     close(fout);
     close(f);
end.