Pagini recente » Cod sursa (job #692127) | Cod sursa (job #614663) | Diferente pentru implica-te/arhiva-educationala intre reviziile 223 si 46 | Cod sursa (job #418422) | Cod sursa (job #969017)
Cod sursa(job #969017)
var a:array[0..19,0..250000]of longint;
buf1,buf2:array[1..1 shl 16]of char;
n,m,i,j,p,q:longint;
begin
assign(input,'stramosi.in'); reset(input); settextbuf(input,buf1);
readln(n,m);
for i:=1 to n do read(a[0,i]);
for i:=1 to 19 do for j:=1 to n do a[i,j]:=a[i-1,a[i-1,j]];
assign(output,'stramosi.out'); rewrite(output); settextbuf(output,buf2);
for i:=1 to m do
begin
readln(p,q);
j:=0;
while q<>0 do
begin
if (q and 1)=1 then p:=a[j,p];
inc(j);
q:=q shr 1;
end;
writeln(p);
end;
close(output);
end.