Cod sursa(job #2727141)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 21 martie 2021 15:16:15
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <iostream>
#include <fstream>

const int NMAX = 25e4;
const int LOG = 17;

int t[1 + NMAX][1 + LOG];

int main() {
  std::ifstream in("stramosi.in");
  std::ofstream out("stramosi.out");

  int n, q;
  in >> n >> q;

  for (int i = 1; i <= n; ++i)
    in >> t[i][0];

  for (int i = 1; i <= n; ++i)
    for (int e = 1; e <= LOG; ++e)
      t[i][e] = t[t[i][e - 1]][e - 1];

  while (q--) {
    int a, k;
    in >> a >> k;

    int e = 0;
    while (k) {
      if (k & 1)
        a = t[a][e];

      ++e;
      k >>= 1;
    }

    out << a << '\n';
  }


  return 0;
}