//https://github.com/fken57/fken_prime_57_atcoderlib #include #include // Define LOCAL from the compiler command line (for example: -DLOCAL). // In a non-LOCAL build only the no-op macros at the bottom of this file exist; // the implementation itself is deliberately unavailable. #ifdef LOCAL #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fib { namespace local_debug { struct FormatOptions { std::size_t max_depth = 16; std::size_t max_elements = 64; }; namespace detail { template class RuntimeArrayView { public: RuntimeArrayView(const T* data, std::size_t size) : data_(data), size_(size) {} const T* begin() const noexcept { return data_; } const T* end() const noexcept { return data_ + size_; } private: const T* data_; std::size_t size_; }; template const T& make_debug_argument(const T& value, std::size_t, std::true_type) { return value; } // GCC/Clang VLA types cannot be template arguments. Array-to-pointer decay // removes only the runtime-sized outer dimension; its length is restored here. template RuntimeArrayView make_debug_argument(T* data, std::size_t total_bytes, std::false_type) { return RuntimeArrayView(data, total_bytes / sizeof(T)); } template using remove_cvref_t = typename std::remove_cv::type>::type; template struct is_stream_insertable : std::false_type {}; template struct is_stream_insertable< T, std::void_t() << std::declval())>> : std::true_type {}; template struct has_val_method : std::false_type {}; template struct has_val_method< T, std::void_t().val())>> : std::integral_constant< bool, !std::is_void().val())>::value> {}; template struct is_basic_string : std::false_type {}; template struct is_basic_string> : std::true_type {}; template struct is_basic_string_view : std::false_type {}; template struct is_basic_string_view> : std::true_type {}; template struct is_char_array : std::integral_constant< bool, std::is_array::type>::value && std::is_same< typename std::remove_cv::type>::type>::type, char>::value> {}; template struct is_char_pointer : std::integral_constant< bool, std::is_pointer::type>::value && std::is_same< typename std::remove_cv::type>::type>::type, char>::value> {}; template struct is_range : std::false_type {}; template struct is_range< T, std::void_t())), decltype(std::end(std::declval()))>> : std::true_type { }; template struct is_tuple_like : std::false_type {}; template struct is_tuple_like< T, std::void_t>::value)>> : std::true_type {}; template struct is_optional : std::false_type {}; template struct is_optional> : std::true_type {}; template struct is_variant : std::false_type {}; template struct is_variant> : std::true_type {}; template struct is_reference_wrapper : std::false_type {}; template struct is_reference_wrapper> : std::true_type {}; template struct is_unique_ptr : std::false_type {}; template struct is_unique_ptr> : std::true_type {}; template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type {}; // Poison pill: a matching function can only be found through ADL. void fib_debug_fields(); template struct has_debug_fields : std::false_type {}; template struct has_debug_fields< T, std::void_t()))>> : std::true_type {}; template struct NamedDebugFields { const char* names; Tuple values; }; template struct is_named_debug_fields : std::false_type {}; template struct is_named_debug_fields> : std::true_type {}; struct AggregateAny { template constexpr operator T() const noexcept; }; template auto aggregate_initialization_test(std::index_sequence) -> decltype(T{(static_cast(Index), AggregateAny{})...}, std::true_type{}); template auto aggregate_initialization_test(...) -> std::false_type; template using can_initialize_aggregate = decltype( aggregate_initialization_test(std::make_index_sequence{})); template struct AggregateArity { static constexpr std::size_t value = can_initialize_aggregate::value ? Size : AggregateArity::value; }; template struct AggregateArity { static constexpr std::size_t value = 0; }; constexpr std::size_t max_automatic_aggregate_fields = 12; template struct is_automatic_aggregate : std::integral_constant< bool, std::is_aggregate::value && std::is_standard_layout::value && !is_range::value && !is_tuple_like::value && !can_initialize_aggregate< T, max_automatic_aggregate_fields + 1>::value> {}; inline std::string trim_field_name(std::string value) { std::size_t first = 0; while (first < value.size() && std::isspace(static_cast(value[first]))) { ++first; } std::size_t last = value.size(); while (last > first && std::isspace(static_cast(value[last - 1]))) { --last; } value = value.substr(first, last - first); constexpr const char prefix[] = "value."; if (value.compare(0, sizeof(prefix) - 1, prefix) == 0) { value.erase(0, sizeof(prefix) - 1); } return value; } inline std::vector split_field_names(const char* names) { std::vector result; if (names == nullptr) return result; std::string current; int nesting = 0; char quote = '\0'; bool escaped = false; for (const char ch : std::string(names)) { if (quote != '\0') { current.push_back(ch); if (escaped) escaped = false; else if (ch == '\\') escaped = true; else if (ch == quote) quote = '\0'; continue; } if (ch == '\"' || ch == '\'') { quote = ch; current.push_back(ch); } else if (ch == '(' || ch == '[' || ch == '{') { ++nesting; current.push_back(ch); } else if (ch == ')' || ch == ']' || ch == '}') { --nesting; current.push_back(ch); } else if (ch == ',' && nesting == 0) { result.push_back(trim_field_name(current)); current.clear(); } else { current.push_back(ch); } } if (!current.empty()) result.push_back(trim_field_name(current)); return result; } inline std::mutex& output_mutex() { static std::mutex mutex; return mutex; } inline void write_escaped(std::ostream& out, std::string_view value, char quote) { out.put(quote); for (const unsigned char ch : value) { switch (ch) { case '\\': out << "\\\\"; break; case '\n': out << "\\n"; break; case '\r': out << "\\r"; break; case '\t': out << "\\t"; break; case '\0': out << "\\0"; break; case '\"': if (quote == '\"') out << "\\\""; else out.put(static_cast(ch)); break; case '\'': if (quote == '\'') out << "\\'"; else out.put(static_cast(ch)); break; default: if (ch >= 0x20 && ch < 0x7f) { out.put(static_cast(ch)); } else { const auto flags = out.flags(); const auto fill = out.fill(); out << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(ch); out.flags(flags); out.fill(fill); } } } out.put(quote); } class Formatter { public: Formatter(std::ostream& out, FormatOptions options) : out_(out), options_(options) {} template void write(const T& value, std::size_t depth = 0) { using U = remove_cvref_t; if (depth > options_.max_depth) { out_ << "..."; } else if constexpr (has_debug_fields::value) { write_custom_object(fib_debug_fields(value), depth); } else if constexpr (std::is_same::value) { out_ << "nullptr"; } else if constexpr (is_char_array::value) { constexpr std::size_t size = std::extent::type>::value; std::size_t length = size; if (length != 0 && value[length - 1] == '\0') --length; write_escaped(out_, std::string_view(value, length), '\"'); } else if constexpr (is_char_pointer::value) { if (value == nullptr) out_ << "nullptr"; else write_escaped(out_, std::string_view(value), '\"'); } else if constexpr (is_basic_string::value || is_basic_string_view::value) { write_escaped(out_, std::string_view(value.data(), value.size()), '\"'); } else if constexpr (std::is_same::value) { const char data[] = {value}; write_escaped(out_, std::string_view(data, 1), '\''); } else if constexpr (std::is_same::value) { out_ << (value ? "true" : "false"); } else if constexpr (is_optional::value) { if (value) { out_ << "optional("; write(*value, depth + 1); out_ << ')'; } else { out_ << "nullopt"; } } else if constexpr (is_variant::value) { if (value.valueless_by_exception()) { out_ << "variant[valueless]"; } else { out_ << "variant[" << value.index() << "]("; std::visit([&](const auto& item) { write(item, depth + 1); }, value); out_ << ')'; } } else if constexpr (std::is_same::value) { out_ << "monostate"; } else if constexpr (is_reference_wrapper::value) { write(value.get(), depth + 1); } else if constexpr (std::is_pointer::value) { if (value == nullptr) { out_ << "nullptr"; } else if constexpr ( std::is_object::type>::value) { out_ << "(value) << '>'; } else { out_ << ""; } } else if constexpr (is_unique_ptr::value || is_shared_ptr::value) { if (!value) out_ << "nullptr"; else out_ << "(value.get()) << '>'; } else if constexpr (is_tuple_like::value && !is_range::value) { write_tuple(value, depth); } else if constexpr (is_range::value) { write_range(value, depth); } else if constexpr (std::is_enum::value) { using Underlying = typename std::underlying_type::type; out_ << static_cast(value); } else if constexpr (is_stream_insertable::value) { out_ << value; } else if constexpr (has_val_method::value) { write(value.val(), depth + 1); } else if constexpr (is_automatic_aggregate::value) { write_aggregate(value, depth); } else { out_ << "'; } } private: template void write_custom_object(const Fields& fields, std::size_t depth) { using U = remove_cvref_t; if constexpr (is_named_debug_fields::value) { write_object_tuple( fields.values, depth, split_field_names(fields.names)); } else if constexpr (is_tuple_like::value) { write_object_tuple(fields, depth, {}); } else { out_ << '{'; write(fields, depth + 1); out_ << '}'; } } template void write_object_tuple_items(const Tuple& fields, std::size_t depth, const std::vector& names, std::index_sequence) { (write_object_field(fields, depth, names), ...); } template void write_object_field(const Tuple& fields, std::size_t depth, const std::vector& names) { out_ << (Index == 0 ? "\n" : ",\n"); write_indent(depth + 1); const std::string key = Index < names.size() ? names[Index] : "_" + std::to_string(Index); write_escaped(out_, key, '\"'); out_ << ": "; write(std::get(fields), depth + 1); } template void write_object_tuple(const Tuple& fields, std::size_t depth, const std::vector& names) { constexpr std::size_t size = std::tuple_size>::value; out_ << '{'; write_object_tuple_items( fields, depth, names, std::make_index_sequence{}); if constexpr (size != 0) { out_ << '\n'; write_indent(depth); } out_ << '}'; } template void write_automatic_object(std::size_t depth, const Fields&... fields) { write_object_tuple(std::forward_as_tuple(fields...), depth, {}); } template void write_aggregate(const T& value, std::size_t depth) { constexpr std::size_t size = AggregateArity::value; if constexpr (size == 0) { write_automatic_object(depth); } else if constexpr (size == 1) { const auto& [f0] = value; write_automatic_object(depth, f0); } else if constexpr (size == 2) { const auto& [f0, f1] = value; write_automatic_object(depth, f0, f1); } else if constexpr (size == 3) { const auto& [f0, f1, f2] = value; write_automatic_object(depth, f0, f1, f2); } else if constexpr (size == 4) { const auto& [f0, f1, f2, f3] = value; write_automatic_object(depth, f0, f1, f2, f3); } else if constexpr (size == 5) { const auto& [f0, f1, f2, f3, f4] = value; write_automatic_object(depth, f0, f1, f2, f3, f4); } else if constexpr (size == 6) { const auto& [f0, f1, f2, f3, f4, f5] = value; write_automatic_object(depth, f0, f1, f2, f3, f4, f5); } else if constexpr (size == 7) { const auto& [f0, f1, f2, f3, f4, f5, f6] = value; write_automatic_object(depth, f0, f1, f2, f3, f4, f5, f6); } else if constexpr (size == 8) { const auto& [f0, f1, f2, f3, f4, f5, f6, f7] = value; write_automatic_object(depth, f0, f1, f2, f3, f4, f5, f6, f7); } else if constexpr (size == 9) { const auto& [f0, f1, f2, f3, f4, f5, f6, f7, f8] = value; write_automatic_object( depth, f0, f1, f2, f3, f4, f5, f6, f7, f8); } else if constexpr (size == 10) { const auto& [f0, f1, f2, f3, f4, f5, f6, f7, f8, f9] = value; write_automatic_object( depth, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9); } else if constexpr (size == 11) { const auto& [f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10] = value; write_automatic_object( depth, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10); } else if constexpr (size == 12) { const auto& [f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11] = value; write_automatic_object( depth, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11); } } template void write_tuple_items(const Tuple& value, std::size_t depth, std::index_sequence) { std::size_t item_index = 0; ((out_ << (item_index++ == 0 ? "" : ", "), write(std::get(value), depth + 1)), ...); } template void write_tuple(const Tuple& value, std::size_t depth) { out_ << '('; write_tuple_items( value, depth, std::make_index_sequence>::value>{}); out_ << ')'; } template void write_range(const Range& value, std::size_t depth) { using Item = remove_cvref_t; constexpr bool multiline = is_range::value && !is_basic_string::value && !is_basic_string_view::value && !is_char_array::value && !is_char_pointer::value; constexpr bool object_items = has_debug_fields::value || is_automatic_aggregate::value; constexpr bool pretty = multiline || object_items; out_ << '['; std::size_t count = 0; for (const auto& item : value) { if (count == options_.max_elements) { write_range_separator(count, pretty, depth); out_ << "..."; ++count; break; } write_range_separator(count, pretty, depth); write(item, depth + 1); ++count; } if constexpr (pretty) { if (count != 0) { out_ << '\n'; write_indent(depth); } } out_ << ']'; } void write_range_separator(std::size_t count, bool multiline, std::size_t depth) { if (multiline) { out_ << (count == 0 ? "\n" : ",\n"); write_indent(depth + 1); } else if (count != 0) { out_ << ", "; } } void write_indent(std::size_t depth) { for (std::size_t i = 0; i < depth * 2; ++i) out_.put(' '); } std::ostream& out_; FormatOptions options_; }; template std::string format_values(const FormatOptions& options, const Args&... args) { std::ostringstream out; Formatter formatter(out, options); std::size_t index = 0; ((out << (index++ == 0 ? "" : " "), formatter.write(args)), ...); return out.str(); } template std::string format_tuple_values(const FormatOptions& options, const Tuple& values, std::index_sequence) { return format_values(options, std::get(values)...); } } // namespace detail template std::string to_debug_string(const T& value, const FormatOptions& options = FormatOptions{}) { return detail::format_values(options, value); } // Writes one formatted line to stderr. This function only exists in LOCAL builds. template void print(const Args&... args) { const std::string message = detail::format_values(FormatOptions{}, args...); std::lock_guard lock(detail::output_mutex()); std::cerr << message << '\n'; std::cerr.flush(); } template void print_tuple_at(const char* file, int line, const char* expressions, const Tuple& values) { constexpr std::size_t size = std::tuple_size>::value; const std::string message = detail::format_tuple_values( FormatOptions{}, values, std::make_index_sequence{}); std::lock_guard lock(detail::output_mutex()); std::cerr << "[debug " << file << ':' << line << ']'; if (expressions != nullptr && expressions[0] != '\0') { std::cerr << ' ' << expressions << " ="; } if (!message.empty()) std::cerr << ' ' << message; std::cerr << '\n'; std::cerr.flush(); } template void print_value_at(const char* file, int line, const char* expression, const T& value) { const std::string message = detail::format_values(FormatOptions{}, value); std::lock_guard lock(detail::output_mutex()); std::cerr << "[debug " << file << ':' << line << ']'; if (expression != nullptr && expression[0] != '\0') { std::cerr << ' ' << expression << " ="; } if (!message.empty()) std::cerr << ' ' << message; std::cerr << '\n'; std::cerr.flush(); } class LoopDetected : public std::runtime_error { public: explicit LoopDetected(const std::string& message) : std::runtime_error(message) {} }; class LoopGuard { public: LoopGuard(std::string label, const char* file, int line, std::uint64_t max_iterations, std::chrono::milliseconds timeout, std::size_t max_same_state = 0, FormatOptions format_options = FormatOptions{}) : label_(std::move(label)), file_(file), line_(line), max_iterations_(max_iterations), timeout_(timeout), max_same_state_(max_same_state), format_options_(format_options), started_at_(Clock::now()) {} LoopGuard(const LoopGuard&) = delete; LoopGuard& operator=(const LoopGuard&) = delete; LoopGuard(LoopGuard&&) = default; LoopGuard& operator=(LoopGuard&&) = default; template void tick(const State&... state) { ++iterations_; last_state_ = detail::format_values(format_options_, state...); if (max_same_state_ != 0) { if (last_state_ == previous_state_) { ++same_state_count_; } else { previous_state_ = last_state_; same_state_count_ = 1; } } if (max_iterations_ != 0 && iterations_ > max_iterations_) { fail("iteration limit exceeded"); } if (timeout_.count() != 0 && Clock::now() - started_at_ > timeout_) { fail("time limit exceeded"); } if (max_same_state_ != 0 && same_state_count_ > max_same_state_) { fail("same state repeated"); } } std::uint64_t iterations() const noexcept { return iterations_; } private: using Clock = std::chrono::steady_clock; [[noreturn]] void fail(const char* reason) const { const auto elapsed = std::chrono::duration_cast( Clock::now() - started_at_) .count(); std::ostringstream message; message << "loop '" << label_ << "' detected at " << file_ << ':' << line_ << "; reason=" << reason << "; iterations=" << iterations_ << "; elapsed_ms=" << elapsed; if (!last_state_.empty()) message << "; state=" << last_state_; const std::string text = message.str(); { std::lock_guard lock(detail::output_mutex()); std::cerr << "[loop-detected] " << text << '\n'; std::cerr.flush(); } throw LoopDetected(text); } std::string label_; const char* file_; int line_; std::uint64_t max_iterations_; std::chrono::milliseconds timeout_; std::size_t max_same_state_; FormatOptions format_options_; Clock::time_point started_at_; std::uint64_t iterations_ = 0; std::size_t same_state_count_ = 0; std::string previous_state_; std::string last_state_; }; inline LoopGuard make_loop_guard(const char* label, const char* file, int line, std::uint64_t max_iterations, std::int64_t timeout_milliseconds, std::size_t max_same_state = 0) { return LoopGuard(label, file, line, max_iterations, std::chrono::milliseconds(timeout_milliseconds), max_same_state); } } // namespace local_debug } // namespace fib // GCC/Clang report whether sizeof(value) is runtime-sized. Standard C++ types // take the true_type path; a VLA takes the runtime-array-view path. #if defined(__GNUC__) || defined(__clang__) #define FIB_DEBUG_DETAIL_SIZE_TAG(value) \ std::integral_constant{} #else #define FIB_DEBUG_DETAIL_SIZE_TAG(value) std::true_type{} #endif #define FIB_DEBUG_DETAIL_ARGUMENT(value) \ ::fib::local_debug::detail::make_debug_argument( \ (value), sizeof(value), FIB_DEBUG_DETAIL_SIZE_TAG(value)) #define FIB_DEBUG_DETAIL_ONE(expressions, value) \ ::fib::local_debug::print_value_at( \ __FILE__, __LINE__, expressions, FIB_DEBUG_DETAIL_ARGUMENT(value)) #define FIB_DEBUG_DETAIL_MANY(expressions, ...) \ ::fib::local_debug::print_tuple_at( \ __FILE__, __LINE__, expressions, std::forward_as_tuple(__VA_ARGS__)) #define FIB_DEBUG_DETAIL_SELECT( \ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, \ _16, NAME, ...) NAME // One argument gets transparent VLA support. For a VLA among multiple values, // wrap it in FIB_VLA: FIB_DEBUG(FIB_VLA(dp), another_value). #define FIB_DEBUG_DETAIL_DISPATCH(expressions, ...) \ FIB_DEBUG_DETAIL_SELECT( \ __VA_ARGS__, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_MANY, \ FIB_DEBUG_DETAIL_MANY, FIB_DEBUG_DETAIL_ONE, \ FIB_DEBUG_DETAIL_UNUSED)(expressions, __VA_ARGS__) #define FIB_DEBUG(...) FIB_DEBUG_DETAIL_DISPATCH(#__VA_ARGS__, __VA_ARGS__) #define FIB_VLA(value) FIB_DEBUG_DETAIL_ARGUMENT(value) #define FIB_DEBUG_FIELDS(Type, ...) \ inline auto fib_debug_fields(const Type& value) { \ return ::fib::local_debug::detail::NamedDebugFields< \ decltype(std::tie(__VA_ARGS__))>{#__VA_ARGS__, \ std::tie(__VA_ARGS__)}; \ } // Usage: FIB_LOOP_GUARD(name, max_iterations, timeout_ms[, max_same_state]). // A zero limit disables that particular check. Call FIB_LOOP_TICK in the loop. #define FIB_LOOP_GUARD(name, ...) \ auto name = ::fib::local_debug::make_loop_guard( \ #name, __FILE__, __LINE__, __VA_ARGS__) #define FIB_LOOP_TICK(name, ...) (name).tick(__VA_ARGS__) #define FIB_DEBUG_ENABLED 1 #else #define FIB_DEBUG(...) ((void)0) #define FIB_VLA(value) (value) #define FIB_DEBUG_FIELDS(Type, ...) #define FIB_LOOP_GUARD(name, ...) ((void)0) #define FIB_LOOP_TICK(name, ...) ((void)0) #define FIB_DEBUG_ENABLED 0 #endif // Injecting debug.hpp <- _fib/common.hpp using namespace std; using namespace atcoder; /* frequently used types */ using ll = long long; using vll = vector; using vvll = vector; using vvvll = vector; using ull = unsigned long long; using vull = vector; using vvull = vector; using vvvull = vector; using Graph = vector>; using vch = vector; using vvch = vector>; using vs = vector; using Pll = pair; using vPll = vector; using pll = pair; using pii = pair; using tup = tuple; using bl = bool; using vbl = vector; using vvbl = vector; using vvvbl = vector; using ld = long double; using vld = vector; using vvld = vector; using vvvld = vector; /* modint aliases: mint remains compatible with the old template. */ using mint998 = atcoder::modint998244353; using mint107 = atcoder::modint1000000007; using mint = mint998; using vmint = vector; using vvmint = vector; using vvvmint = vector; template using static_mint = atcoder::static_modint; template using dynamic_mint = atcoder::dynamic_modint; constexpr int infint = (1 << 30) - 1; constexpr ll inf = 1LL << 60; template struct WeightedEdge { int to; Cost weight; int id = -1; }; template using WeightedGraphT = vector>>; using WeightedGraphEdge = WeightedEdge; using WeightedGraph = WeightedGraphT; template inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } #define rep(i, x, lim) for (ll i = (x); i < static_cast(lim); ++i) #define all(x) std::begin(x), std::end(x) #define rall(x) std::rbegin(x), std::rend(x) template constexpr int sz(const Container& container) { return static_cast(container.size()); } namespace fib { namespace utility { template auto array_max(const Range& range) { using Value = std::decay_t; const auto first = std::begin(range); const auto last = std::end(range); assert(first != last && "arr_max requires a non-empty range"); return Value(*std::max_element(first, last)); } template auto array_min(const Range& range) { using Value = std::decay_t; const auto first = std::begin(range); const auto last = std::end(range); assert(first != last && "arr_min requires a non-empty range"); return Value(*std::min_element(first, last)); } template auto array_sum(const Range& range) { using Value = std::decay_t; Value result{}; for (const auto& value : range) result += value; return result; } } // namespace utility } // namespace fib // Function-backed macros evaluate the range expression exactly once. #define arr_max(range) (::fib::utility::array_max((range))) #define arr_min(range) (::fib::utility::array_min((range))) #define arr_sum(range) (::fib::utility::array_sum((range))) constexpr ll mod1000000007 = 1000000007LL; constexpr ll mod998244353 = 998244353LL; inline constexpr array dx{1, -1, 0, 0, 1, 1, -1, -1}; inline constexpr array dy{0, 0, 1, -1, 1, -1, 1, -1}; inline constexpr array pow2ll{ 1ULL, 2ULL, 4ULL, 8ULL, 16ULL, 32ULL, 64ULL, 128ULL, 256ULL, 512ULL, 1024ULL, 2048ULL, 4096ULL, 8192ULL, 16384ULL, 32768ULL, 65536ULL, 131072ULL, 262144ULL, 524288ULL, 1048576ULL, 2097152ULL, 4194304ULL, 8388608ULL, 16777216ULL, 33554432ULL, 67108864ULL, 134217728ULL, 268435456ULL, 536870912ULL, 1073741824ULL, 2147483648ULL, 4294967296ULL, 8589934592ULL, 17179869184ULL, 34359738368ULL, 68719476736ULL, 137438953472ULL, 274877906944ULL, 549755813888ULL, 1099511627776ULL, 2199023255552ULL, 4398046511104ULL, 8796093022208ULL, 17592186044416ULL, 35184372088832ULL, 70368744177664ULL, 140737488355328ULL, 281474976710656ULL, 562949953421312ULL, 1125899906842624ULL, 2251799813685248ULL, 4503599627370496ULL, 9007199254740992ULL, 18014398509481984ULL, 36028797018963968ULL, 72057594037927936ULL, 144115188075855872ULL, 288230376151711744ULL, 576460752303423488ULL, 1152921504606846976ULL, 2305843009213693952ULL, 4611686018427387904ULL, 9223372036854775808ULL, }; inline constexpr array pow10ll{ 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL, 1000000ULL, 10000000ULL, 100000000ULL, 1000000000ULL, 10000000000ULL, 100000000000ULL, 1000000000000ULL, 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL, 10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL, 10000000000000000000ULL, }; // Skipping already injected ../common.hpp template struct DijkstraResult { Cost infinity{}; vector dist; vector parent_vertex; vector parent_edge; bool reachable(int vertex) const { return dist[vertex] != infinity; } vector restore_vertices(int target) const { if (!reachable(target)) return {}; vector path; for (int vertex = target; vertex != -1; vertex = parent_vertex[vertex]) { path.push_back(vertex); } reverse(path.begin(), path.end()); return path; } vector restore_edges(int target) const { if (!reachable(target)) return {}; vector path; while (parent_vertex[target] != -1) { path.push_back(parent_edge[target]); target = parent_vertex[target]; } reverse(path.begin(), path.end()); return path; } }; template DijkstraResult dijkstra( const WeightedGraphT& graph, const vector& sources, Cost infinity = std::numeric_limits::max() / Cost(4)) { const int n = static_cast(graph.size()); DijkstraResult result{ infinity, vector(n, infinity), vector(n, -1), vector(n, -1), }; using QueueEntry = pair; priority_queue, greater> queue; for (const int source : sources) { assert(0 <= source && source < n); if (result.dist[source] == Cost{}) continue; result.dist[source] = Cost{}; queue.emplace(Cost{}, source); } while (!queue.empty()) { const auto [distance, vertex] = queue.top(); queue.pop(); if (result.dist[vertex] != distance) continue; for (int index = 0; index < static_cast(graph[vertex].size()); ++index) { const auto& edge = graph[vertex][index]; assert(!(edge.weight < Cost{}) && "dijkstra requires non-negative edge costs"); if (edge.weight > infinity || result.dist[vertex] > infinity - edge.weight) { continue; } const Cost next_distance = result.dist[vertex] + edge.weight; if (next_distance >= result.dist[edge.to]) continue; result.dist[edge.to] = next_distance; result.parent_vertex[edge.to] = vertex; result.parent_edge[edge.to] = edge.id == -1 ? index : edge.id; queue.emplace(next_distance, edge.to); } } return result; } template DijkstraResult dijkstra( const WeightedGraphT& graph, int source, Cost infinity = std::numeric_limits::max() / Cost(4)) { return dijkstra(graph, vector{source}, infinity); } // Injecting _fib/Graph/Dijkstra.hpp <- solve/cpp/A.cpp // Injecting common.hpp <- _fib/template.hpp using ac=array; ll newhash(char c,ll modi,ll hash){ ll cnt=0; FIB_DEBUG(hash); if(c == '('){ cnt=3*hash; } if(c == ')'){ cnt=3*hash; cnt+=1; } if(c == '|'){ cnt=3*hash; cnt+=2; } cnt%=modi; return cnt; } #ifndef FIB_NO_MAIN void solve() { ll Q; map> mp; map mpbl; ac c; for(auto v:c) v='#'; Pll now={1,1}; mp[{1,1}]=c; mpbl[now]=true; stack stc; cin >> Q; while(Q--){ ll type; cin >> type; if(type==1){ char c; cin >> c; stc.push(c); ac cnt=mp[now]; now.first = newhash(c,998244353,now.first); now.second = newhash(c,1000000007,now.second); rep(i,1,6){ cnt[6-i]=cnt[5-i]; } cnt[0]=c; ll i998=998244353; ll i107=1000000007; if(cnt[0]==')' && cnt[1]=='|' && cnt[2]=='('){ mpbl[now]=mpbl[{(((now.first-7+i998)%i998)*480636170)%i998,(((now.second-7+i107)%i107)*370370373)%i107}]; mp[now]=cnt; } else{ mp[now]=cnt; } } else{ ac cnt=mp[now]; ll i998=998244353; ll i107=1000000007; if(stc.top()=='('){ now.first=(((now.first-0+i998)%i998)* 332748118)%i998; now.second=(((now.second-0+i107)%i107)* 333333336)%i107; } if(stc.top()==')'){ now.first=(((now.first-1+i998)%i998)* 332748118)%i998; now.second=(((now.second-1+i107)%i107)* 333333336)%i107; } if(stc.top()=='|'){ now.first=(((now.first-2+i998)%i998)* 332748118)%i998; now.second=(((now.second-2+i107)%i107)* 333333336)%i107; } } /* 332748118 333333336*/ FIB_DEBUG(mp); if(mpbl[now]){ cout << "Yes" << endl; } else{ cout << "No" << endl; } } } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); ll T = 1; // cin >> T; while (T--) { solve(); } } #endif // Injecting _fib/template.hpp <- solve/cpp/A.cpp