Pagini recente » Borderou de evaluare (job #1347354) | Cod sursa (job #3304490) | Cod sursa (job #3321778) | Cod sursa (job #3350277) | Cod sursa (job #3322730)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n,m,tt[250001];
int mat[250001][30];
void citire()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
fin>>mat[0][i];
}
void matr()
{
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]];
}
}
}
void solve()
{
for(int i=1;i<=m;i++)
{ int p,q;
fin>>q>>p;
while(p)
{
int l=log2(p);
int z=(1<<l);
q=mat[l][q];
p-=z;
}
fout<<q;fout<<endl;
}
}
int main()
{
citire();
matr();
solve();
return 0;
}