Cod sursa(job #2642873)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 17 august 2020 14:27:16
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX = 250005;
const int PMAX = 19;
int dp[PMAX][NMAX], n, q, x, k, b;

int main()
{
    f >> n >> q;
    for(int i = 1; i <= n; i++) {
        f >> dp[0][i];
    }
    for(int i = 1; (1 << i) <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dp[i][j] = dp[i - 1][dp[i - 1][j]];
        }
    }
    while(q--) {
        f >> x >> k;
        b = 0;
        while(k) {
            if(k % 2) {
                x = dp[b][x];
            }
            b++;
            k /= 2;
        }
        g << x << '\n';
    }
    return 0;
}