Pagini recente » Cod sursa (job #1476346) | Cod sursa (job #3272032) | Cod sursa (job #1213463) | Cod sursa (job #2857608) | Cod sursa (job #2919137)
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
ifstream fin ("stramosi.in");
ofstream fout ("stramosi.out");
const int MAX_N = 250005;
int n, q, nod, pw2, step;
int ancestor[19][MAX_N];
int main (){
ios_base::sync_with_stdio(false);
fin.tie(nullptr), fout.tie(nullptr);
fin>>n>>q;
for(int j=1; j<=n; j++)
fin>>ancestor[0][j];
for(int i=1; (1 << i)<=n; i++)
for(int j=1; j<=n; j++)
ancestor[i][j] = ancestor[i-1][ancestor[i-1][j]];
while(q--){
fin>>nod>>step;
pw2 = 0;
while(step){
if(step & 1)
nod = ancestor[pw2][nod];
pw2++;
step >>= 1;
}
fout<<nod<<"\n";
}
return 0;
}