Pagini recente » Cod sursa (job #117) | Cod sursa (job #359151) | Cod sursa (job #2257764) | Cod sursa (job #1520845) | Cod sursa (job #2024920)
#include <bits/stdc++.h>
using namespace std;
int dp[20][250001];
void Build_Dp(int n)
{
for (int j = 1; j<=19; ++j)
for (int i = 1; i<=n; ++i)
dp[j][i] = dp[j-1][dp[j-1][i]];
}
void Query (int& x, int count)
{
for (int i = 19; i>=0; --i)
if (count >= (1<<i))
{
count -= (1<<i);
x = dp[i][x];
}
}
int main(int argc, char const *argv[])
{
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
int n, m;
fin >> n >> m;
for (int i = 1; i<=n; ++i)
fin >> dp[0][i];
Build_Dp(n);
for (int i = 1; i<=m; ++i)
{
int x, count;
fin >> x >> count;
Query(x, count);
fout << x << '\n';
}
return 0;
}