Cod sursa(job #3123737)

Utilizator retrogradLucian Bicsi retrograd Data 25 aprilie 2023 10:25:52
Problema Stramosi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <bits/stdc++.h>

using namespace std;

int main() {
  ifstream cin("stramosi.in");
  ofstream cout("stramosi.out");

  int n, m; cin >> n >> m;
  vector<int> par(n + 1);
  for (int i = 1; i <= n; ++i) 
    cin >> par[i];
  vector<int> qv(m), qk(m);
  for (int i = 0; i < m; ++i) 
    cin >> qv[i] >> qk[i]; 
  
  for (int d = 0; d < 18; ++d) {
    for (int i = 0; i < m; ++i)
      if (qk[i] & (1 << d))
        qv[i] = par[qv[i]];
    for (int i = n; i >= 1; --i)
      par[i] = par[par[i]];
  }
  for (int i = 0; i < m; ++i) 
    cout << qv[i] << "\n";
  return 0;
}