Pagini recente » Cod sursa (job #263923) | Cod sursa (job #1334502) | Cod sursa (job #1317237) | Cod sursa (job #2980749) | Cod sursa (job #667122)
Cod sursa(job #667122)
#include <fstream>
#include <algorithm>
using namespace std;
int N,M,mat[22][300001];
int lg[300001],X,Y;
inline int caut(int X,int Y)
{
int p=0;
while(Y){
if(Y&1==1) X=mat[p][X];
p++; Y>>=1;
}
return X;
}
int main()
{
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
fin>>N>>M;
for(int i=1;i<=N;++i)
{
fin>>mat[0][i];
}
for(int i=2;i<=N;++i)
lg[i]=lg[i>>1]+1;
for(int i=1;(1<<i)<=N;++i)
{
for(int j=1;j<=N;++j)
{
mat[i][j]=mat[i-1][mat[i-1][j]];
}
}
for(;M>0;--M)
{
fin>>X>>Y;
fout<<caut(X,Y)<<'\n';
}
fout.close();
return 0;
}