Pagini recente » Cod sursa (job #781394) | Cod sursa (job #2480223) | Cod sursa (job #643818) | Cod sursa (job #713509) | Cod sursa (job #14265)
Cod sursa(job #14265)
program p1;
var mat:array[0..250001,0..20] of 0..250001;
a,b,c,v:array[0..250001] of 250001;
n,m,i,j,p,q,x,y:0..250001;
f,g:text;
procedure parc(vf:integer);
var k:integer;
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);
assign(g,'stramosi.out');
rewrite(g);
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);
for y:=1 to m do
begin
readln(f,q,p);
x:=0;
j:=0;
repeat
j:=j+1;
b[j]:=p mod 2;
if b[j]=1 then
begin
x:=x+1;
c[x]:=1 shl (j-1);
mat[0,x]:=j;
end;
p:=p div 2;
until p<=1;
if p=1 then
begin
j:=j+1;
b[j]:=p;
x:=x+1;
c[x]:=1 shl (j-1);
mat[0,x]:=j;
end;
x:=x+1;
if mat[q,0]=0 then q:=0
else
while x>1 do
begin
x:=x-1;
if mat[q,0]<c[x] then
begin
i:=x;
q:=0;
end
else q:=mat[q,mat[0,x]];
end;
writeln(g,q);
end;
close(g);
close(f);
end.