Cod sursa(job #2965893)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 16 ianuarie 2023 15:41:31
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>

using namespace std;

const int Nmax = 250001;
const int Pow2Max = 20;

int dp[Pow2Max][Nmax];

int main(){
    ifstream fin("stramosi.in");
    ofstream fout("stramosi.out");

    int n, m, a, b, pow, sol;

    fin >> n >> m;

    for(int j = 1; j <= n; j++){
        fin >> dp[0][j];
    }

    for(int i = 1; i < Pow2Max; i++){
        for(int j = 1; j <= n; j++){
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
        }
    }

    for(int h = 0; h < m; h++){
        fin >> a >> b;

        while(b){
            sol = -1;
            for(int i = 0; i < Pow2Max; i++){
                pow = (1 << i);

                if(b & pow){
                    sol = i;
                }
            }

            a = dp[sol][a];
            b -= (1 << sol);
        }

        fout << a << '\n';

    }

    return 0;
}