Pagini recente » Cod sursa (job #47641) | Cod sursa (job #275818) | Cod sursa (job #779230) | Cod sursa (job #2260170) | Cod sursa (job #1045646)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int str[20][250001];
int stramos(int nod, int poz)
{
int maxi;
while(poz>0)
{
maxi=log2(poz);
nod=str[maxi][nod];
poz=poz-(1<<maxi);
}
return nod;
}
int main()
{
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int n,m,i,j,p,q;
f>>n>>m;
for(i=1;i<=n;i++)
f>>str[0][i];
for(i=1;i<=19;i++)
for(j=1;j<=n;j++)
str[i][j]=str[i-1][str[i-1][j]];
for(i=1;i<=m;i++)
{
f>>p>>q;
g<<stramos(p,q)<<'\n';
}
f.close();
g.close();
return 0;
}