Cod sursa(job #35582)

Utilizator floringh06Florin Ghesu floringh06 Data 22 martie 2007 10:53:02
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.87 kb
var fi,fo:text;
    j,i,n,m,st,vl,a,b,ct:longint;
    s:array[0..19,0..250000] of longint;


  procedure work;
   var i,j,p,k:longint;
    begin
     for i:=1 to ct do
       for j:=1 to n do
         s[i,j]:=s[i-1,s[i-1,j]];
    end;




begin
 assign(fi,'stramosi.in'); reset(fi);
 assign(fo,'stramosi.out'); rewrite(fo);
 readln(fi,n,m);
 for i:=1 to n do
  begin
   read(fi,s[0,i]);
  end;
 st:=1;
 while st<=n do
  begin
   st:=st shl 1;
   inc(ct);
  end;
 work;
{ for i:=1 to ct do
  begin
   for j:=1 to n do
    write(fo,s[i,j]:3);
   writeln(fo);
  end;}
 for i:=1 to m do
  begin
   readln(fi,a,b);
   vl:=a;
  while b>0 do
  begin
   st:=1;
   ct:=0;
   while st<=b do
   begin
    st:=st shl 1;
    inc(ct);
   end;
   dec(ct);
   st:=st shr 1;
   b:=b-st;
   vl:=s[ct,vl];
  end;
 writeln(fo,vl);
  end;
close(fo);
end.