Cod sursa(job #1800560)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 7 noiembrie 2016 21:27:53
Problema Stramosi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<fstream>
#define NMax 250000
#define LOG 18
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

int N,Q;
int str[LOG + 5][NMax + 5];

void Read()
{
    fin>>N>>Q;

    for(int i = 1 ; i <= N ; ++i)
        fin>>str[0][i];
}

void Precalculate()
{
    for(int i = 1 ; i <= LOG ; i++)
        for(int j = 1 ; j <= N ; j++)
            str[i][j] = str[i-1][str[i-1][j]];
}

void Answer()
{
    while(Q--)
    {
        int x,y;    fin>>x>>y;

        for(int i = LOG ; i >= 0 ; i--)
            if( y & (1 << i) )
                x = str[i][x];

        fout<<x<<"\n";
    }
}

int main()
{
    Read();
    Precalculate();
    Answer();

    fin.close();
    fout.close();
    return 0;
}