Pagini recente » Cod sursa (job #36001) | Cod sursa (job #557928) | Cod sursa (job #2596786) | Cod sursa (job #1778955)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
const int maxn=250001;
int stramosi[20][maxn];
int N,M;
int main()
{
int i,j,x,y;
f>>N>>M;
for (i=1;i<=N;i++)
f>>stramosi[0][i];
for (i=1;i<=19;i++)
for (j=1;j<=N;j++)
stramosi[i][j]=stramosi[i-1][stramosi[i-1][j]];
for (i=1;i<=M;i++)
{
f>>x>>y;
j=0;
while(y)
{
if ((y&1)==1)
{
x=stramosi[j][x];
}
j++;
y=y>>1;
}
g<<x<<'\n';
}
return 0;
}