Pagini recente » Cod sursa (job #2262293) | Cod sursa (job #378897) | Cod sursa (job #1235251) | Cod sursa (job #1252812) | Cod sursa (job #549756)
Cod sursa(job #549756)
var i,j,n,m,c,x,y:longint;
f1,f2:text;
a,v:array[1..120000]of longint;
procedure init;
var i,j:longint;
begin
for i:=1 to n do
for j:=i-(i xor (i and (i-1)))+1 to i do
v[i]:=v[i]+a[j];
end;
procedure schimba(x,y:longint);
var i:longint;
begin
i:=x;
while i<n do
begin
v[i]:=v[i]+y;
i:=i+(i xor (i and (i-1)));
end;
end;
function suma(p:longint):int64;
begin
suma:=0;
while p>0 do
begin
suma:=suma+v[p];
p:=p-(p xor (p and (p-1)));
end;
end;
function poz(p:longint):longint;
var i,s:longint;
begin
s:=0;
i:=1;
while (v[i*2]<=p)and(i*2<=n) do i:=i*2;
s:=v[i];
while (s<p)and(i<n) do
begin
inc(i);
s:=s+a[i];
end;
if s=p then poz:=i
else poz:=-1;
end;
begin
assign(f1,'aib.in');
reset(f1);
assign(f2,'aib.out');
rewrite(f2);
readln(f1,n,m);
for i:=1 to n do
read(f1,a[i]);
readln(f1);
init;
for i:=1 to m do
begin
read(f1,c);
if c=0 then
begin
readln(f1,x,y);
schimba(x,y);
end;
if c=1 then
begin
readln(f1,x,y);
writeln(f2,suma(y)-suma(x-1));
end;
if c=2 then
begin
readln(f1,x);
writeln(f2,poz(x));
end;
end;
close(f1);
close(f2);
end.