Pagini recente » Cod sursa (job #1884369) | Cod sursa (job #286217) | Cod sursa (job #2979718) | Cod sursa (job #1781084) | Cod sursa (job #2649533)
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int N, str[250100][20], lim;
inline void rmqish()
{
for(int exp=1; exp<=lim; ++exp)
for(int j=1; j<=N; ++j)
str[j][exp]=str[str[j][exp-1]][exp-1];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int M,x,Q,P;
cin>>N>>M;
lim=log2(N);
for(int i=1; i<=N; ++i) {
cin>>x;
str[i][0]=x;
}
rmqish();
while(M--){
cin>>Q>>P;
if(P>N) {
cout<<0<<'\n';
continue;
}
x=0;
while(P) {
if(P&1)
Q=str[Q][x];
P>>=1; ++x;
}
cout<<Q<<'\n';
}
return 0;
}