Pagini recente » Cod sursa (job #2681332) | Cod sursa (job #2214939) | Cod sursa (job #660038) | Cod sursa (job #2423036) | Cod sursa (job #18002)
Cod sursa(job #18002)
program p1;
type tip=0..250001;
var mat:array[0..17,0..250001] of tip;
gr:array[0..250001] of tip;
c:array[0..21] of tip;
n,m,i,j,p,q,x,y:0..300001;
f,g:text;
begin
assign(f,'stramosi.in');
reset(f);
assign(g,'stramosi.out');
rewrite(g);
read(f,n,m);
for i:=1 to n do
begin
read(f,mat[1,i]);
j:=1;
p:=0;
repeat
j:=j+1;
if mat[j-1,i]=0 then p:=1
else
begin
q:=mat[j-1,mat[j-1,i]];
if q=0 then p:=1
else mat[j,i]:=q;
end;
until p=1;
mat[0,i]:=1 shl (j-2);
end;
for y:=1 to m do
begin
readln(f,q,p);
x:=0;
j:=1;
i:=1;
if (i and p)=i then
begin
x:=x+1;
c[x]:=i;
gr[x]:=j;
end;
repeat
i:=i+i;
j:=j+1;
if (i and p)=i then
begin
x:=x+1;
c[x]:=i;
gr[x]:=j;
end;
until i>=p;
x:=x+1;
if mat[0,q]=0 then q:=0
else
while x>1 do
begin
x:=x-1;
if mat[0,q]<c[x] then
begin
i:=x;
q:=0;
end
else q:=mat[gr[x],q];
end;
writeln(g,q);
end;
close(f);
close(g);
end.