Skip to content

Instantly share code, notes, and snippets.

@theortsac
Last active July 2, 2023 18:09
Show Gist options
  • Save theortsac/b72937ec70f8eda59671ab8eca3dc558 to your computer and use it in GitHub Desktop.
Save theortsac/b72937ec70f8eda59671ab8eca3dc558 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAXLOG = 30;
int prox[MAXLOG + 1][MAXN];
int main() {
ios::sync_with_stdio(0);ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); // otimizações de entrada/saída
int n, q;
cin >> n >> q;
for (int x = 1; x <= n; x++) {
cin >> prox[0][x];
}
for (int i = 1; i <= MAXLOG; i++) {
for (int x = 1; x <= n; x++) {
prox[i][x] = prox[i-1][[prox[i-1][x]];
}
}
while(q--) {
int x, k;
cin >> x >> k;
for (int i = 0; i <= MAXLOG; i++) {
if (k & (1 << i)) x = prox[i][x];
}
cout << x << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment