Pagini recente » Cod sursa (job #1501707) | Cod sursa (job #2216601) | Cod sursa (job #281985) | Cod sursa (job #716183) | Cod sursa (job #2696427)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("stramosi.in");
ofstream out("stramosi.out");
const int POW = 16;
const int MAXN = 250000;
int mat[ POW + 1 ][ MAXN + 1 ];
int main()
{
bool ok;
int n, m, i, j, pow, x, ind, nr_stramos, putere;
in>>n>>m;
i = 0;
for( j = 1; j <= n; j++ ){
in>>mat[i][j];
}
pow = 1;
putere = 1;
while( putere * 2 <= n ){
putere = putere * 2;
for( j = 1; j <= n; j++ ){
mat[pow][j] = mat[pow - 1][ mat[ pow - 1 ][j]];
}
pow++;
}
for( i = 0; i < m; i++ ){
in>>x>>ind;
ok = 0;
for( pow = POW; pow >= 0; pow-- ){
if( 1 << pow <= ind ){
x = mat[pow][x];
ind = ind - ( 1 << pow );
}
}
out<<x<<'\n';
}
return 0;
}