Pagini recente » Cod sursa (job #2045263) | Denis S | Diferente pentru home intre reviziile 762 si 763 | Profil M@2Te4i | Cod sursa (job #201832)
Cod sursa(job #201832)
program stramosi_010;
var a:array[0..300000,0..22] of longint;
n,m:longint;
i,j,r,l,u,v,w:longint;
BEGIN
assign(input,'stramosi.in'); reset(input);
assign(output,'stramosi.out'); rewrite(output);
readln(n,m); r:=trunc(ln(n)/ln(2));
for i:=1 to n do read(a[i,0]);
for j:=1 to r do begin
for i:=1 to n do begin
a[i,j]:=a[a[i,j-1],j-1];
end;
end;
for l:=1 to m do begin
readln(u,v);
for w:=0 to r do begin
if (v and (1 shl w))>0 then u:=a[u,w];
end;
writeln(u);
end;
close(output); close(input);
END.