Cod sursa(job #2890488)

Utilizator AlexMariMarinescu Alexandru AlexMari Data 15 aprilie 2022 17:58:56
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("stramosi.in");
ofstream fout("stramosi.out");

int n, q, dp[35][200005];

int main()
{
    fin >> n >> q;
    for (int i = 1; i <= n; i++)
        fin >> dp[0][i];

    for (int pow_2 = 1; pow_2 <= 30; pow_2++)
        for (int i = 1; i <= n; i++)
            dp[pow_2][i] = dp[pow_2 - 1][dp[pow_2 - 1][i]];
    while (q--) {
        int nod, nr_stramos;
        fin >> nod >> nr_stramos;
        int curr_nod = nod, put_2 = 0;
        while (nr_stramos) {
            if (nr_stramos % 2 == 1)
                curr_nod = dp[put_2][curr_nod];
            nr_stramos /= 2;
            put_2++;
        }
        fout << curr_nod << '\n';
    }
    return 0;
}