Pagini recente » Cod sursa (job #2540360) | Cod sursa (job #2259913) | Cod sursa (job #863734) | Cod sursa (job #2640479) | Cod sursa (job #3259492)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
const int Nmax=250;
int stramos[(int)floor(log2(Nmax))+2][Nmax];///s[i][j]= al 2^i stramos al lui j
int n;
void initStramosi()
{
for(int i=1; i<=n; i++)
fin>>stramos[0][i];
int maxStramos=(int)floor(log2(n));
for(int i=1; i<=maxStramos; i++)
{
for(int j=1; j<=n; j++)
{
stramos[i][j]=stramos[i-1][stramos[i-1][j]];
}
}
}
int getStramos(int p, int q)
{
int pas2=1;
while((1<<pas2)<=p)
pas2++;
pas2--;
int rez=stramos[pas2][q];
pas2--;
while(pas2>=0)
{
if((1<<pas2)&p)
rez=stramos[pas2][rez];
pas2--;
}
return rez;
}
int main()
{
int m;
fin>>n>>m;
initStramosi();
int x,y;
for(int q=1; q<=m; q++)
{
fin>>x>>y;
fout<<getStramos(y, x)<<'\n';
}
return 0;
}