#include "validation.h" const int MAX_Q = 1000; const int EXTRA_Q = 2000; int query(std::vector>& f, int c, int r) { r--; int k = 0; while(c > 0) { if(c % 2 == 1) r = f[k][r]; c /= 2; k++; } r++; return r; } int main(int argc, char* argv[]) { // Set up the input and answer streams. std::ifstream in(argv[1]); // std::ifstream ans(argv[2]); // Only for custom checker. OutputValidator v(argc, argv); int n; in >> n; std::cout << n << std::endl; std::vector> f(30, std::vector(n)); // f[i][j] = f^(2^i)(j) for(int i = 0; i < n; i++) { in >> f[0][i]; f[0][i]--; } for(int i = 1; i < 30; i++) { for(int j = 0; j < n; j++) { f[i][j] = f[i - 1][f[i - 1][j]]; } } int used_q = 0; while(true) { std::string q = v.test_strings({"?", "!"}); if(q == "?") { used_q++; v.check(used_q <= EXTRA_Q, "Used more than ", EXTRA_Q, " queries, aborting."); v.space(); int c = v.read_integer("c", 1, n); v.space(); int r = v.read_integer("r", 1, n); v.newline(); std::cout << query(f, c, r) << std::endl; } else { v.space(); int c = v.read_integer("c", 1, n); v.space(); int r = v.read_integer("r", 1, n); v.newline(); std::cerr << "Used " << used_q << " queries. "; // No endl, so that a possible WA message ends up on the same line. v.check(query(f, c, r) == c, "Answer is wrong."); v.check(used_q <= MAX_Q, "Used more than ", MAX_Q, " queries."); break; } } }