Pagini recente » Profil | Cod sursa (job #1703464) | Cod sursa (job #201937) | Cod sursa (job #717828) | Cod sursa (job #201834)
Cod sursa(job #201834)
program stramosi_010;
var a:array[0..250001,0..18] 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);
for i:=1 to n do read(a[i,0]);
for j:=1 to 17 do
for i:=1 to n do a[i,j]:=a[a[i,j-1],j-1];
for l:=1 to m do begin
readln(u,v);
for w:=0 to 17 do begin
if (v and (1 shl w))>0 then u:=a[u,w];
end;
writeln(u);
end;
close(output); close(input);
END.