Cod sursa(job #2966426)

Utilizator pifaDumitru Andrei Denis pifa Data 17 ianuarie 2023 16:56:16
Problema Stramosi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 2500001;

int up[N + 1][18];

int n, q;

int jump(int x, int y)
{
    int nivel = 0;
    while(y > 0)
    {
        if(y & 1)
        {
            x = up[nivel][x];
        }
        nivel++;
        y >>= 1;
    }
    return x;
}

int main()
{
    in >> n >> q;
    for(int i = 1; i <= n; i++)
    {
        int x;
        in >> x;
        up[0][i] = x;
    }
    for(int i = 1; (i << i) <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            up[i][j] = up[i - 1][up[i - 1][j]];
        }
    }
    while(q--)
    {
        int a, b;
        in >> a >> b;
        out << jump(a, b) << '\n';
    }
    return 0;
}