Pagini recente » Cod sursa (job #831581) | Cod sursa (job #1422963) | Cod sursa (job #1089416) | Cod sursa (job #2365842) | Cod sursa (job #2791533)
#include <bits/stdc++.h>
#define NMAX 250005
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int t[19][NMAX];
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int n, m, i, j, x, y, z;
fin >> n >> m;
for(i = 1; i <= n; i++) fin >> t[0][i];
for(i = 1; i <= 18; i++)
for(j = 1; j <= n; j++)
t[i][j] = t[i-1][t[i-1][j]];
while(m--)
{
fin >> x >> y;
while(y)
{
z = log2(y);
y -= (1<<z);
x = t[z][x];
}
fout << x << '\n';
}
return 0;
}