Cod sursa(job #3164828)

Utilizator radu1331Mocan Radu radu1331 Data 4 noiembrie 2023 13:37:02
Problema Stramosi Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#pragma GCC optimize("O3")

int sm [ 300005 ];

inline int fastRead_int() 
{
    int num = 0;
    char c = getchar_unlocked();

    while ( true )
    {
        if ( c >= '0' && c <= '9' ) break;
        c = getchar_unlocked();
    }

    num = c - '0';

    while ( true ) 
    {
        c = getchar_unlocked();
        if ( c < '0' || c > '9') break;
        num = ( num << 3 ) + ( num << 1 ) + ( c - '0' );
    }

    return num;
}

int main ( int argc, char *argv[] )
{
    ( void )! freopen ( "stramosi.in", "r", stdin );
    ( void )! freopen ( "stramosi.out", "w", stdout );
    std::ios_base::sync_with_stdio ( false );
    std::cin.tie ( NULL );
    
    int n = fastRead_int();
    int m = fastRead_int();
    for ( int i = 1; i <= n; ++ i ) sm [ i ] = fastRead_int();
    while ( m -- )
    {
        int q = fastRead_int();
        int p = fastRead_int();
        int res = 0;
        res = sm [ q ];
        for ( int i = 0; i < p - 1; ++ i )
        {
            if ( res == 0 ) break;
            res = sm [ res ];
        }
        std::cout << res << '\n';
    }
    return 0;
}