只有50分
#include <iostream>
#include <vector>
#include <cmath>
int main() {
int n, m;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cin >> m;
// Preprocess the final target city for each city and time
std::vector<std::vector<int>> target(n, std::vector<int>(31));
for (int i = 0; i < n; i++) {
target[i][0] = a[i];
}
for (int j = 1; j <= 30; j++) {
for (int i = 0; i < n; i++) {
target[i][j] = target[target[i][j-1]-1][j-1];
}
}
for (int i = 0; i < m; i++) {
int S, t1, t2;
std::cin >> S >> t1 >> t2;
int target_city = S;
for (int j = 0; j <= 30; j++) {
if (t1 & (1 << j)) {
target_city = target[target_city-1][j];
}
}
// Output the target city after t1^t2 time units
std::cout << target_city << std::endl;
}
return 0;
}