Pagini recente » Cod sursa (job #92078) | Cod sursa (job #2678029) | Cod sursa (job #3356434) | Borderou de evaluare (job #1750314) | Cod sursa (job #3311987)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, q, x, y, i, j, tata[18][200002];
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
fin >> n >> q;
for(i = 1; i <= n; i++) fin >> tata[0][i];
for(i = 1; i <= 17; i++) {
for(j = 1; j <= n; j++) {
tata[i][j] = tata[i - 1][tata[i - 1][j]];
}
}
while(q--) {
fin >> x >> y;
for(i = 17; i >= 0; i--) {
if(y >> i & 1) x = tata[i][x];
}
fout << x << "\n";
}
return 0;
}