Cod sursa(job #2977226)

Utilizator Summer05Cocut Alexandru Summer05 Data 11 februarie 2023 09:42:27
Problema Stramosi Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

#define ll long long

using namespace std;

const ll LOG = 18;

void solve()
{
    ll n, m;
    cin >> n >> m;
    vector<vector<ll>> t(n + 1, vector<ll>(LOG, 0));
    for (ll i = 1; i <= n; i++)
        cin >> t[i][0];
    /// t[i][j] = t[t[i][j - 1]][j - 1]
    for (ll j = 1; j <= LOG; j++)
        for (ll i = 1; i <= n; i++)
            t[i][j] = t[t[i][j - 1]][j - 1];
    function<void(ll, ll)> jump = [&](ll q, ll p)
    {
        for (ll j = LOG; j >= 0; j--)
            if ((p >> j) & 1)
                q = t[q][j];
        cout << q << '\n';
    };
    for (ll que = 1, q, p; que <= m; que++)
    {
        cin >> q >> p;
        jump(q, p);
    }
}

int main(void)
{
    freopen("stramosi.in", "r", stdin);
    freopen("stramosi.out", "w", stdout);

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}