Cod sursa(job #528977)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 3 februarie 2011 23:05:24
Problema Datorii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.43 kb
var     a,v,b:array[1..15000] of longint;
        c:array[0..15] of longint;
        n:integer;
        m,i,j,x,y:longint;
        f,t:text;
        s:string;
        poz:integer;

function suma(p:longint):longint;
var     t:longint;
begin
  suma:=0;
  if p>0 then
  begin
  t:=c[b[p]];
  while p-t>=0 do
    begin
      suma:=suma+v[p];
      dec(p,t);
      if p>0 then t:=c[b[p]];
    end;
  end;
end;

procedure modifica(x,y:longint);
var t:longint;
begin
  t:=c[b[x]];
  v[x]:=v[x]-y;
  while x+t<=n do
    begin
      inc(x,t);
      t:=c[b[x]];
      v[x]:=v[x]-y;
    end;
end;

begin
  assign(f,'datorii.out');
  rewrite(f);
  assign(t,'datorii.in');
  reset(t);
  readln(t,n,m);
      while poz<n do
        begin
          inc(poz);
          readln(t,s);
          for i:=1 to length(s) do
          if s[i]=' ' then inc(poz) else a[poz]:=a[poz]*10+ord(s[i])-ord('0');
        end;


  for i:=1 to n div 2 do
    if i<n div 2 then if b[i]=0 then
      begin
        j:=i;
          while j<=n do
            begin
              b[j*2]:=b[j]+1;
              j:=j*2;
            end;
      end;

  c[0]:=1;
  for i:=1 to 10 do
    c[i]:=c[i-1]*2;

  for i:=1 to n do
    v[i]:=a[i]+suma(i-1)-suma(i-c[b[i]]);

  for i:=1 to m do
    begin
      readln(t,j,x,y);
      if j=1 then writeln(f,suma(y)-suma(x-1))
      else modifica(x,y);
    end;
  close(t);
  close(f);
end.