Cod sursa(job #2908398)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 3 iunie 2022 11:06:24
Problema Stramosi Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

using namespace std;

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

const int NMAX = 1e5;
int t[17][NMAX + 1], lg2[NMAX + 1], nivel[NMAX + 1], n, m, x, y;

void rmq(){

    for(int i = 1; i <= lg2[n]; i++)
        for(int j = 1; j <= n; j++)
            t[i][j] = t[i - 1][t[i - 1][j]];

}

int stramos(int x, int ord){

    int e = 0;
    while(ord){

        if(ord % 2)
            x = t[e][x];

        e++;
        ord >>= 1;

    }

    return x;
}


int main(){

    cin >> n >> m;

    lg2[0] = -1;
    for(int i = 1; i <= n; i++){

        cin >> t[0][i];
        lg2[i] = 1 + lg2[i >> 1];

    }

    rmq();


    for(int i = 0; i < m; i++){

        cin >> x >> y;
        cout << stramos(x, y) << "\n";


    }

    return 0;
}