#include #include #include #include static inline constexpr std::vector> prepare(const int_fast32_t N, const std::string& S) noexcept { std::vector> csum_count(N + 1, { 0, 0 }); for (int_fast32_t i = 0; i != N; ++i) { if (S[i] == 'w' || S[i] == 'a') csum_count[i + 1] = { csum_count[i][0], csum_count[i][1] + 1 }; else csum_count[i + 1] = { csum_count[i][0] + 1, csum_count[i][1] }; } return csum_count; } static inline constexpr std::string solve(const int_fast32_t N, const int_fast32_t Q, const std::string& S, std::vector& T, std::vector& X, const std::vector>& csum_count) noexcept { std::string ans(Q, '?'); for (int_fast32_t i = 0; i != Q; ++i) { --X[i]; // 1-indexed → 0-indexed T[i] = std::min(T[i], 60); // T[i] が 60 より大きい場合を考慮する必要はない(解説参照) // 二分探索で、S の l 文字目までの部分だけで T[i] 秒後に X[i] 文字以下になるような最大の l を求める int_fast32_t l = 0, r = N + 1; while (l + 1 < r) { const int_fast32_t c = (l + r) / 2; if (csum_count[c][1] > (X[i] - csum_count[c][0]) / (5 * (INT64_C(1) << T[i]) - 4)) // 1つ後の条件式ではオーバーフローの危険があるので、精度を緩めてオーバーフローを回避 r = c; else if (csum_count[c][0] + csum_count[c][1] * (5 * (INT64_C(1) << T[i]) - 4) > X[i]) r = c; else l = c; } if (csum_count[l][0] + csum_count[l][1] * (5 * (INT64_C(1) << T[i]) - 4) == X[i]) ans[i] = S[l]; // ぴったりだったら、そこが答え else { X[i] -= csum_count[l][0] + csum_count[l][1] * (5 * (INT64_C(1) << T[i]) - 4); const char* target = S.data(); uint_fast32_t cursor = l; for (--T[i]; X[i] != 0; --T[i]) { if (target[cursor] == 'a') target = "answer"; else if (target[cursor] == 'w') target = "warong"; else break; // 異常終了 for (cursor = 0; X[i] != 0; ++cursor) { if (target[cursor] != 'w' && target[cursor] != 'a') --X[i]; else if (X[i] < 5 * (INT64_C(1) << T[i]) - 4) break; // 進めたら行き過ぎるので、中止 else X[i] -= 5 * (INT64_C(1) << T[i]) - 4; } } ans[i] = target[cursor]; } } return ans; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int_fast32_t N, Q, i; std::string S; std::cin >> N >> Q; S.reserve(N), std::cin >> S; std::vector T(Q), X(Q); for (i = 0; i != Q; ++i) std::cin >> T[i] >> X[i]; std::cout << solve(N, Q, S, T, X, prepare(N, S)) << '\n'; return 0; }