Pagini recente » Cod sursa (job #75914) | Cod sursa (job #2872013) | Cod sursa (job #3354561) | Cod sursa (job #3246004) | 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;
}