Cod sursa(job #2966430)

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

using namespace std;

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

const int N = 2500001;

int up[18][N + 1];

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++)
    {
        in >> up[0][i];
    }
    for(int i = 1; (1 << i) <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            up[i][j] = up[i - 1][up[i - 1][j]];
        }
    }
    for(int i = 1; i <= q; i++)
    {
        int x, y;
        in >> x >> y;
        out << jump(x, y) << '\n';
    }
    return 0;
}