Pagini recente » Cod sursa (job #1714469) | Cod sursa (job #1919128) | Cod sursa (job #2469940) | Cod sursa (job #287613) | Cod sursa (job #667147)
Cod sursa(job #667147)
#include <fstream>
#include <algorithm>
using namespace std;
int N,M,mat[20][250001];
int X,Y,t=1,d;
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];
}
while(t<=N){
t<<=1;d++;
}
for(int i=1;i<=d;++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;
}