Pagini recente » Cod sursa (job #2323405) | Cod sursa (job #1156859) | Cod sursa (job #2839450) | Cod sursa (job #754291) | Cod sursa (job #30058)
Cod sursa(job #30058)
#include <stdio.h>
int mat[250000][19];
int main()
{
freopen("stramosi.in","r",stdin);
freopen("stramosi.out","w",stdout);
int i, j, temp, cnt, n, m, p, q;
scanf("%d%d", &n, &m);
for(i = 1; i <= n; ++i)
{
scanf("%d", &mat[i][0]);
}
for(j = 1; j <= 18; ++j)
{
for(i = 1; i <= n; ++i)
{
mat[i][j] = mat[mat[i][j - 1]][j - 1];
}
}
for(i = 1; i <= m; ++i)
{
scanf("%d%d", &q, &p);
temp = 1 << 18;
cnt = 18;
while(temp)
{
if(temp & p)
{
q = mat[q][cnt];
}
temp >>= 1;
--cnt;
}
printf("%d\n", q);
}
return 0;
}