Pagini recente » Cod sursa (job #3312706) | Cod sursa (job #3327468) | Cod sursa (job #3317518) | Cod sursa (job #3317526) | Cod sursa (job #3337644)
//e binary lifting penal
#include<bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
const int MAXN=25e4;
const int MAXLOG=20;
int up[MAXN+1][MAXLOG+1];
int main() {
int n,q;
fin>>n>>q;
for(int i=1; i<=n; i++) {
fin>>up[i][0];
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=MAXLOG; j++) {
up[i][j]=up[up[i][j-1]][j-1];
}
}
while(q--) {
int nd,k;
fin>>nd>>k;
for(int i=MAXLOG; i>=0; i--) {
if(k&(1<<i)){
nd=up[nd][i];
}
}
fout<<nd<<'\n';
}
return 0;
}