Pagini recente » Cod sursa (job #812568) | Cod sursa (job #2815084) | Monitorul de evaluare | Cod sursa (job #2056294) | Cod sursa (job #2275693)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int c[250001][18];
int n, m;
int solve(int x, int y)
{
while (y)
{
int j = 1, exp = 0;
while (j <= y)
{
exp += 1;
j *= 2;
}
exp -= 1;
j /= 2;
x = c[x][exp];
y -= j;
}
return x;
}
int main()
{
fin >> n >> m;
for (int i=1; i<=n; i++)
{
fin >> c[i][0];
}
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;
}