Cod sursa(job #3311986)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 25 septembrie 2025 11:02:24
Problema Stramosi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("stramosi.in");
ofstream fout("stramosi.out");
int n, q, x, y, i, j, tata[18][102];

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> n >> q;
    for(i = 1; i <=  n; i++) fin >> tata[0][i];
    for(i = 1; i <= 17; i++) {
        for(j = 1; j <= n; j++) {
            tata[i][j] = tata[i - 1][tata[i - 1][j]];
        }
    }

    while(q--) {
        fin >> x >> y;
        for(i = 17; i >= 0; i--) {
            if(y >> i & 1) x = tata[i][x];
        }

        fout << x << "\n";
    }

    return 0;
}