Pagini recente » Cod sursa (job #598312) | Cod sursa (job #474914) | Cod sursa (job #1616512) | Cod sursa (job #999119) | Cod sursa (job #2275688)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int a[250001], c[250001][18];
int n, m;
int solve(int x, int y)
{
int pos = x;
while (y)
{
int j = 1, exp = 0;
while (j <= y)
{
exp += 1;
j *= 2;
}
exp -= 1;
j /= 2;
pos = c[pos][exp];
y -= j;
}
return pos;
}
int main()
{
fin >> n >> m;
for (int i=1; i<=n; i++)
{
fin >> a[i];
c[i][0] = a[i];
}
for (int i=1; i<=n; i++)
{
for (int j=1; j<18; j++)
{
c[i][j] = c[c[i][j-1]][j-1];
}
}
// for (int i=1; i<=n; i++)
// {
// for (int j=0; j<18; j++)
// {
// cout << c[i][j] << " ";
// }
// cout << "\n";
// }
int x, y;
for (int i=1; i<=m; i++)
{
fin >> x >> y;
fout << solve(x,y) << endl;
}
return 0;
}