Cod sursa(job #268519)

Utilizator botaMihai Botezatu Catalin bota Data 1 martie 2009 12:57:58
Problema Stramosi Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.05 kb
program stramosi;   
var a,b:array[1..250500]of longint;   
    i,j,n,m,x,y,stramos:longint;   
    fin,fout:text;   
  
  
begin  
assign(fin,'stramosi.in'); reset(fin);   
assign(fout,'stramosi.out'); rewrite(fout);   
  
readln(fin,n,m);   
for i:=1 to n do read(fin,a[i]);   
  
for j:=1 to n do begin  
    stramos:=j;   
    for i:=1 to 3000 do  
        if stramos = 0 then break   
        else stramos:=a[stramos];   
    b[j]:=stramos;   
end;   
  
for j:=1 to m do begin  
    readln(fin,x,y);   
    stramos:=x;   
    while y<>0 do  
          if stramos= 0 then break   
          else  
          if y >= 3000 then begin  
                         stramos:=b[stramos];   
                         y:=y-3000;   
                         end  
          else begin  
               stramos:=a[stramos];   
               dec(y);   
          end;   
    {for i:=1 to y do  
        if stramos = 0 then break  
        else stramos:=a[stramos];}  
    writeln(fout,stramos);   
end;   
  
close(fin); close(fout);   
end.