Pagini recente » Rating Andrei Georgescu (Andrei592) | Cod sursa (job #1816364) | Cod sursa (job #739881) | Rating Stefan Ursache (StefanUrsache) | Cod sursa (job #2189416)
#include <fstream>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
int n,m,stra[20][250005],p,q,expo;
int main()
{
in>>n>>m;
for (int i=1; i<=n; i++)
in>>stra[0][i];
for (int i=1; i<=18; i++)
for (int j=1; j<=n; j++)
stra[i][j]=stra[i-1][stra[i-1][j]];
for (int i=1; i<=m; i++)
{
in>>q>>p;
expo=0;
while (p)
{
if (p & 1)
q=stra[expo][q];
expo++;
p/=2;
}
out<<q<<'\n';
}
return 0;
}