Pagini recente » Cod sursa (job #2473194) | Cod sursa (job #2939999) | Cod sursa (job #37479) | Cod sursa (job #3209905) | Cod sursa (job #2715576)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
const int NMAX = 250000;
const int LGMAX = 18;
int N, M;
int anc[LGMAX + 2][NMAX + 2];
int main() {
cin >> N >> M;
for(int i = 1; i <= N; i++) {
cin >> anc[0][i];
}
for(int e = 1; e <= LGMAX; e++) {
for(int j = 1; j <= N; j++) {
anc[e][j] = anc[e - 1][anc[e - 1][j]];
}
}
for(int i = 1; i <= M; i++) {
int Q, P;
cin >> Q >> P;
int res = Q;
for(int step = LGMAX; step >= 0; step--) {
if(P >= (1 << step)) {
res = anc[step][res];
P -= (1 << step);
}
}
cout << res << '\n';
}
return 0;
}