Pagini recente » Cod sursa (job #2074933) | Cod sursa (job #337175) | Cod sursa (job #2902992) | FMI No Stress 4 | Cod sursa (job #1501846)
#include <cstdio>
#include <algorithm>
#include <vector>
#define DIM 250000
using namespace std;
int NthFather[20][DIM], N, M, X, Y;
int main ()
{
freopen ("stramosi.in" ,"r", stdin );
freopen ("stramosi.out","w", stdout);
scanf ("%d %d", &N, &M);
for (int i = 1; i <= N; i ++)
scanf ("%d", &NthFather[0][i]);
for (int i = 1; i <= 19; i ++)
for (int j = 1; j <= N; j ++)
NthFather[i][j] = NthFather[i-1][NthFather[i-1][j]];
for (int i = 1; i <= M; i ++)
{
scanf ("%d %d", &X, &Y);
for (int j = 30; Y; j --)
if ((1<<j) <= Y)
{
Y -= (1<<j);
X = NthFather[j][X];
}
printf ("%d\n", X);
}
fclose (stdin );
fclose (stdout);
return 0;
}