Pagini recente » Cod sursa (job #602754) | Cod sursa (job #1334863) | Cod sursa (job #1658438) | Cod sursa (job #1070810) | Cod sursa (job #2401297)
#include <fstream>
using namespace std;
ifstream fin( "stramosi.in" );
ofstream fout( "stramosi.out" );
const int NMAX = 250005;
int N, T;
int mat[NMAX][20];
void Read()
{
fin >> N >> T;
for( int i = 1; i <= N; ++i )
fin >> mat[i][0];
}
int BiggestPow( int val )
{
if( val == 0 ) return -1;
int ans = 0;
while( true )
{
int aux = 1 << ( ans + 1 );
if( aux > val ) return ans;
else ++ans;
}
}
void Do()
{
int aux;
for( int j = 1; j <= 19; ++j )
for( int i = 1; i <= N; ++i )
{
aux = mat[i][j - 1];
if( aux > 0 ) mat[i][j] = mat[aux][j - 1];
}
int nod, D;
for( int i = 1; i <= T; ++i )
{
fin >> nod >> D;
aux = BiggestPow( D );
while( D > 0 )
{
nod = mat[nod][aux];
D -= ( 1 << aux );
aux = BiggestPow( D );
}
fout << nod << '\n';
}
}
int main()
{
Read();
Do();
return 0;
}