Pagini recente » Cod sursa (job #2649707) | Cod sursa (job #632637) | Cod sursa (job #3132151) | Cod sursa (job #1589162) | Cod sursa (job #2649534)
#include <fstream>
#include <cmath>
using namespace std;
ifstream cin("stramosi.in");
ofstream cout("stramosi.out");
int N, str[250100][18], lim;
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>>str[i][0];
for(int exp=1; exp<=lim; ++exp)
for(int j=1; j<=N; ++j)
str[j][exp]=str[str[j][exp-1]][exp-1];
while(M--){
cin>>Q>>P;
x=0;
while(P) {
if(P&1)
Q=str[Q][x];
P>>=1; ++x;
}
cout<<Q<<'\n';
}
return 0;
}