Cod sursa(job #3171430)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 18 noiembrie 2023 20:59:44
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 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;

    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;

        for(int i = Pow2Max - 1; i >= 0; i--){
            pow = (1 << i);

            if(b & pow){
                a = dp[i][a];
                b ^= pow;
            }
        }
        fout << a << '\n';
    }

    return 0;
}