Cod sursa(job #739355)
Utilizator | Data | 22 aprilie 2012 20:08:53 | |
---|---|---|---|
Problema | Arbori indexati binar | Scor | 100 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 2.85 kb |
Program arbori_indexati_binar;
type natural=0..75;
var fi, fo : text;
i,n,m,aux,nr,a1,b1,pozitie,operatie : longint;
a,q : array[-1..100100] of longint;
k : array[-1..100100] of natural;
Function suma(a,b:longint):longint;
var s1,s2:longword;
begin
s2:=0; s1:=0;
while b>0 do begin
s2:=s2+q[b];
b:=b-(1 shl k[b]);
end;
a:=a-1;
while a>0 do begin
s1:=s1+q[a];
a:=a-(1 shl k[a]);
end;
suma:=s2-s1;
end;
Procedure searching(y:longint);
var left,right,mijl,s : longint;
begin
left:=1; right:=n;
while (left<=right) and (pozitie=-1) do begin
mijl:=(left+right) div 2;
s:=suma(1,mijl);
if s>y then right:=mijl-1
else if s<y then left:=mijl+1
else pozitie:=mijl;
end;
end;
Procedure Adaugare(a,b:longint);
begin
while a<=n do begin
q[a]:=q[a]+b;
a:=a+(1 shl k[a]);
end;
end;
begin
assign(fi,'aib.in'); reset(fi);
assign(fo,'aib.out'); rewrite(fo);
readln(fi,n,m); a[0]:=0; k[0]:=0;
for i:=1 to n do begin
read(fi,a[i]);
a[i]:=a[i]+a[i-1];
nr:=0; aux:=i;
while (aux mod 2 = 0) do begin
nr:=nr+1;
aux:=aux div 2;
end;
k[i]:=nr;
q[i]:=a[i]-a[i-(1 shl k[i])];
end;
for i:=1 to m do begin
read(fi,operatie);
if operatie=0 then begin
readln(fi,a1,b1);
adaugare(a1,b1);
end
else if operatie=1 then begin
readln(fi,a1,b1);
writeln(fo,suma(a1,b1));
end
else begin
readln(fi,b1);
pozitie:=-1;
searching(b1);
writeln(fo,pozitie);
end;
end;
close(fi); close(fo);
end.