Cod sursa(job #2695506)

Utilizator Fantastic_Mantudor voicu Fantastic_Man Data 13 ianuarie 2021 15:17:02
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>

using namespace std;
const int NMAX = 25e4;
const int LOGMAX = 17;
ifstream fin ( "stramosi.in" );
ofstream fout ( "stramosi.out" );
int a[LOGMAX + 1][NMAX + 1];
int main() {
    int n, Q, q, p, j;
    fin >> n >> Q;
    for ( int i = 1; i <= n; i++ )
        fin >> a[0][i];
    for ( int i = 1; ( 1 << i ) <= n; i++ )
        for ( int j = 1; j <= n; j++ )
            a[i][j] = a[i - 1][a[i - 1][j]];
    while ( Q-- ) {
        fin >> q >> p;
        j = 0;
        while ( p ) {
            if ( p & 1 )
                q = a[j][q];
            j++;
            p /= 2;
        }
        fout << q << '\n';
    }

    return 0;
}