Pagini recente » Cod sursa (job #1958255) | Cod sursa (job #2431395) | Cod sursa (job #624989) | Cod sursa (job #2153275) | Cod sursa (job #1943649)
#include <cstdio>
const int NMAX = 250000;
const int LOG = 17;
int n, m;
int t[1 + NMAX][1 + LOG];
void build()
{
for(int i = 1; i <= LOG ; i++)
{
for(int nod = 1; nod <= n; nod++)
{
t[nod][i] = t[t[nod][i - 1]][i - 1];
}
}
}
int answer(int nod, int dist)
{
int val = 1 << LOG;
int count = LOG;
while(val != 0)
{
//printf("%d %d %d %d %d\n", val, dist, count,t[nod][count],val&dist);
if((val&dist)!=0)
{
//printf("*\n");
nod = t[nod][count];
}
val >>= 1;
count--;
}
return nod;
}
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", &t[i][0]);
}
build();
for(int i=1; i<=m; i++)
{
int q, p;
scanf("%d %d", &q, &p);
printf("%d\n", answer(q, p));
}
return 0;
}