Pagini recente » Cod sursa (job #895707) | Cod sursa (job #2025078) | Cod sursa (job #2801863) | Rating Totolici Alexandru (agabi21) | Cod sursa (job #13936)
Cod sursa(job #13936)
program p1;
var mat:array[0..250001,0..20] of 0..250001;
a,c,v:array[0..250001] of 0..250001;
n,m,i,j,p,q,x,y:longint;
f,g:text;
procedure parc(vf:longint);
var k:0..250001;
begin
for k:=1 to n do
if a[k]=vf then
begin
c[k]:=c[vf]+1;
v[c[k]]:=k;
i:=1;
j:=0;
repeat
j:=j+1;
mat[k,j]:=v[c[k]-i];
i:=i+(i xor (i-1)) and i;
until c[k]-i<1;
mat[k,0]:=i div 2;
parc(k);
end;
end;
begin
assign(f,'stramosi.in');
reset(f);
readln(f,n,m);
x:=1;
for i:=1 to n do
begin
read(f,a[i]);
if a[i]=0 then
begin
mat[0,x]:=i;
x:=x+1;
end;
end;
for y:=1 to x-1 do
begin
c[mat[0,y]]:=1;
v[1]:=mat[0,y];
parc(mat[0,y]);
end;
readln(f);
assign(g,'stramosi.out');
rewrite(g);
for i:=1 to m do
begin
readln(f,q,p);
j:=0;
repeat
j:=j+1;
mat[0,j]:=p mod 2;
p:=p div 2;
until p<=1;
if p<>0 then
begin
j:=j+1;
mat[0,j]:=p;
end;
p:=1 shl (j-1);
if p>mat[q,0] then q:=0
else
begin
q:=mat[q,j];
while j>1 do
begin
repeat
j:=j-1;
until mat[0,j]=1;
p:=1 shl (j-1);
if p>mat[q,0] then j:=0
else q:=mat[q,j];
end;
end;
writeln(g,q);
end;
close(g);
close(f);
end.