Pagini recente » Cod sursa (job #1280244) | Cod sursa (job #921498) | Cod sursa (job #514724) | Cod sursa (job #2945183) | Cod sursa (job #1045656)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int str[20][250001];
int stramos(int nod, int poz)
{
int maxi=17;
while(poz>0)
{
if((1<<maxi)<=poz)
{
nod=str[maxi][nod];
poz=poz-(1<<maxi);
}
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];
i=1;
while((1<<i)<=n)
{
for(j=1;j<=n;j++)
str[i][j]=str[i-1][str[i-1][j]];
i++;
}
for(i=1;i<=m;i++)
{
f>>p>>q;
g<<stramos(p,q)<<'\n';
}
f.close();
g.close();
return 0;
}