Pagini recente » Cod sursa (job #3356752) | Cod sursa (job #1783548) | Borderou de evaluare (job #3179389) | Cod sursa (job #2015709) | Cod sursa (job #3318293)
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3", "unroll-loops", "fast-math", "no-stack-protector", "omit-frame-pointer")
#pragma GCC target("avx2", "popcnt", "tune=native")
const int maxn=250005;
const int LOG=20;
int up[maxn][LOG];
signed main() {
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int n,q;
cin>>n>>q;
up[0][0]=0;
for(int i=1;i<=n;i++) {
cin>>up[i][0];
}
for (int i=1;i<=n;i++) {
for (int j=1;j<LOG;j++) {
up[i][j]=up[up[i][j-1]][j-1];
}
}
for (int i=1;i<=q;i++) {
int nod,h;
cin>>nod>>h;
int nodcur=nod;
for (int j=0;j<LOG;j++) {
if ((1<<j)&h)nodcur=up[nodcur][j];
}
cout<<nodcur<<'\n';
}
return 0;
}