Pagini recente » Cod sursa (job #1717611) | Cod sursa (job #3216195) | Cod sursa (job #2475065) | Cod sursa (job #22737) | Cod sursa (job #2720141)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
const int NMAX = 300001;
const int KMAX = 30;
int n, m, dp[KMAX][NMAX];
int main() {
f >> n >> m;
for(int i = 1;i <= n;++i)
f >> dp[0][i];
for(int i = 1;i < KMAX;++i)
for(int j = 1;j <= n;++j)
dp[i][j] = dp[i - 1][ dp[i - 1][j] ];
for(;m--;) {
int x, y;
f >> x >> y;
for(int exp{};y;y >>= 1, exp++) {
if(y & 1)
x = dp[exp][x];
}
g << x << '\n';
}
return 0;
}