Pagini recente » Cod sursa (job #1002543) | Cod sursa (job #3176554) | Cod sursa (job #2043950) | Cod sursa (job #1796198) | Cod sursa (job #1043166)
#include <fstream>
#include <math.h>
using namespace std;
#define NMAX 250001
#define LOGNMAX 18
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int ancestors[NMAX][LOGNMAX],m,n,p,q;
int main()
{
int i,j;
fin>>n>>m;
for(i=1;i<=n;i++)
fin>>ancestors[i][0];
int height = 1;
while(pow(2,height) < n)
{
for(int i=1;i<=n;i++)
ancestors[i][height]=ancestors[ancestors[i][height-1]][0];
height++;
}
while(m--)
{
fin>>q>>p;
fout<<ancestors[q][p-1]<<"\n";
}
return 0;
}