Cod sursa(job #2719977)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 10 martie 2021 14:30:20
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <fstream>
#define f in
#define g out

using namespace std;
ifstream in ( "stramosi.in" );
ofstream out( "stramosi.out" );
int d[300000][25];
int n, i, j, q, x, y, p, nod, stramos;

int main() {
    f>>n>>q;
    for ( i=1; i <= n; i++ ){
        f>>x;
        d[i][0] = x;
    }
    
    for ( i = 1; i <= n; i++ )
        for ( j = 1; j <= 20; j++ )
            d[i][j] = d[d[i][j-1]][j-1];
    
    for ( ; q--; ){
        f>>nod>>stramos;
        p = 0;
        while ( stramos ) {
            if ( stramos % 2 )
                nod = d[nod][p];
            p++;
            stramos /= 2;
        }
        g<<nod<<"\n";
    }
    return 0;
}