Pagini recente » Cod sursa (job #419939) | Cod sursa (job #1633087) | Cod sursa (job #2711434) | Cod sursa (job #1026207) | Cod sursa (job #2696468)
#include <bits/stdc++.h>
using namespace std;
#define x1 "scmax.in"
#define x2 "scmax.out"
ifstream in("stramosi.in");
ofstream out("stramosi.out");
#define NMAX 250000
#define POW2 17
int mat[POW2 + 1][NMAX + 1];
int main() {
int n, q, i, j, x, y;
in >> n >> q;
for(i = 1; i <= n; i++)
in >> mat[0][i];
for(i = 1; i <= POW2; i++)
for(int j = 1; j <= n; j++)
mat[i][j] = mat[i - 1][mat[i - 1][j]];
while(q--) {
in >> x >> y;
for(j = POW2; j >= 0; j--) {
if(y >= 1 << j) {
x = mat[j][x];
y -= (1 << j);
}
}
out << x << "\n";
}
return 0;
}