Cod sursa(job #1535054)

Utilizator gedicaAlpaca Gedit gedica Data 24 noiembrie 2015 11:39:55
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream in( "stramosi.in" );
ofstream out( "stramosi.out" );

int d[20][250001];

int main()
{
    int N, M;
    in >> N >> M;

    for( int i= 1; i<=N; ++i )
    {
        in >> d[0][i];
    }

    for( int i= 1; i<=17; ++i )
    {
        for( int j= 1; j<=N; ++j )
        {
            d[i][j]= d[i-1][d[i-1][j]];
        }
    }

    while( M )
    {
        --M;
        int x, y;
        in >> x >> y;
        int aux= 0;

        for( int aux= 0; y ; ++aux )
        while( aux<=y )
        {
            if( y&(1<<aux) )
            {
                x= d[aux][x];
                y-= (1<<aux);
            }
            aux++;
        }
        out << x << '\n';
    }

    return 0;
}