Pagini recente » Cod sursa (job #3221747) | Cod sursa (job #2858573) | Cod sursa (job #1929818) | Cod sursa (job #1457149) | Cod sursa (job #2286527)
#include <fstream>
#include <cstring>
#include <algorithm>
# define mod 1000000
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int i,n,m,nod,poz,j,t[250001][18],aux;
int main(){
fin>>n>>m;
for(i=1;i<=n;i++)
fin>>t[i][0];
for(j=1;(1<<j)<=n;i++)
for(i=1;i<=n;i++)
t[i][j]=t[t[i][j-1]][j-1];
for(i=1;i<=m;i++)
{
fin>>nod>>poz;
for(j=0;(1<<j)<=poz;j++)if(poz&(1<<j)) nod=t[nod][j];
fout<<nod<<'\n';
}
return 0;
}