Cod sursa(job #197471)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 4 iulie 2008 12:48:59
Problema Datorii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.32 kb
var v:array[1..40010]of longint;
    n,i,j,k,m,o,a,b,c,l,s:longint;
    f1,f2:text;
procedure plata(p,r:longint);
var q:longint;
begin
   v[l]:=v[l]-c;
   q:=(p+r)div 2;
   if p<r then begin if b<=q then begin l:=l*2;
                                        plata(p,q);
                                  end
                             else begin l:=l*2+1;
                                        plata(q+1,r);
                                  end;
               end;
end;
procedure cauta(p,r,w:longint);
var q:longint;
begin
   q:=(p+r)div 2;
   if(b<=p)and(r<=c)then s:=s+v[w]
                    else begin if b<=q then cauta(p,q,w*2);
                               if c>q then cauta(q+1,r,w*2+1);
                         end;
end;
begin
   assign(f1,'datorii.in');
   reset(f1);
   assign(f2,'datorii.out');
   rewrite(f2);
   readln(f1,n,m);
   o:=1;
   while o<n do
   o:=o*2;
   for i:=1 to n do
   read(f1,v[i+o-1]);
   for i:=o*2-1 downto 2 do
   v[i div 2]:=v[i div 2]+v[i];
   for i:=1 to m do
   begin
   read(f1,a,b,c);
   if a=0 then begin l:=1;
                     plata(1,o);
               end
          else begin l:=1;
                     s:=0;
                     cauta(1,o,l);
                     writeln(f2,s);
               end;
   end;
   close(f1);
   close(f2);
end.