結果
問題 | No.2308 [Cherry 5th Tune B] もしかして、真? |
ユーザー | suisen |
提出日時 | 2023-05-19 22:42:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 546 ms / 2,000 ms |
コード長 | 45,672 bytes |
コンパイル時間 | 4,294 ms |
コンパイル使用メモリ | 335,296 KB |
実行使用メモリ | 28,852 KB |
最終ジャッジ日時 | 2024-12-20 01:12:20 |
合計ジャッジ時間 | 20,489 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 331 ms
8,924 KB |
testcase_02 | AC | 407 ms
15,152 KB |
testcase_03 | AC | 373 ms
14,856 KB |
testcase_04 | AC | 366 ms
14,888 KB |
testcase_05 | AC | 389 ms
15,420 KB |
testcase_06 | AC | 426 ms
24,400 KB |
testcase_07 | AC | 418 ms
20,688 KB |
testcase_08 | AC | 477 ms
25,344 KB |
testcase_09 | AC | 456 ms
25,552 KB |
testcase_10 | AC | 394 ms
14,764 KB |
testcase_11 | AC | 433 ms
14,792 KB |
testcase_12 | AC | 425 ms
15,556 KB |
testcase_13 | AC | 425 ms
14,788 KB |
testcase_14 | AC | 415 ms
16,092 KB |
testcase_15 | AC | 421 ms
15,708 KB |
testcase_16 | AC | 423 ms
14,428 KB |
testcase_17 | AC | 426 ms
14,560 KB |
testcase_18 | AC | 425 ms
14,560 KB |
testcase_19 | AC | 432 ms
14,688 KB |
testcase_20 | AC | 422 ms
15,840 KB |
testcase_21 | AC | 527 ms
28,292 KB |
testcase_22 | AC | 546 ms
28,364 KB |
testcase_23 | AC | 532 ms
28,328 KB |
testcase_24 | AC | 509 ms
27,808 KB |
testcase_25 | AC | 513 ms
27,236 KB |
testcase_26 | AC | 532 ms
27,828 KB |
testcase_27 | AC | 528 ms
28,852 KB |
testcase_28 | AC | 516 ms
28,488 KB |
testcase_29 | AC | 514 ms
28,436 KB |
testcase_30 | AC | 510 ms
26,208 KB |
testcase_31 | AC | 122 ms
26,088 KB |
testcase_32 | AC | 120 ms
26,432 KB |
testcase_33 | AC | 126 ms
28,168 KB |
testcase_34 | AC | 129 ms
25,996 KB |
testcase_35 | AC | 130 ms
27,732 KB |
testcase_36 | AC | 133 ms
28,036 KB |
testcase_37 | AC | 11 ms
6,820 KB |
testcase_38 | AC | 431 ms
15,816 KB |
ソースコード
#include <bits/stdc++.h> #ifdef _MSC_VER # include <intrin.h> #else # include <x86intrin.h> #endif #include <limits> #include <type_traits> namespace suisen { // ! utility template <typename ...Types> using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>; template <bool cond_v, typename Then, typename OrElse> constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) { if constexpr (cond_v) { return std::forward<Then>(then); } else { return std::forward<OrElse>(or_else); } } // ! function template <typename ReturnType, typename Callable, typename ...Args> using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>; template <typename F, typename T> using is_uni_op = is_same_as_invoke_result<T, F, T>; template <typename F, typename T> using is_bin_op = is_same_as_invoke_result<T, F, T, T>; template <typename Comparator, typename T> using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>; // ! integral template <typename T, typename = constraints_t<std::is_integral<T>>> constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits; template <typename T, unsigned int n> struct is_nbit { static constexpr bool value = bit_num<T> == n; }; template <typename T, unsigned int n> static constexpr bool is_nbit_v = is_nbit<T, n>::value; // ? template <typename T> struct safely_multipliable {}; template <> struct safely_multipliable<int> { using type = long long; }; template <> struct safely_multipliable<long long> { using type = __int128_t; }; template <> struct safely_multipliable<unsigned int> { using type = unsigned long long; }; template <> struct safely_multipliable<unsigned long int> { using type = __uint128_t; }; template <> struct safely_multipliable<unsigned long long> { using type = __uint128_t; }; template <> struct safely_multipliable<float> { using type = float; }; template <> struct safely_multipliable<double> { using type = double; }; template <> struct safely_multipliable<long double> { using type = long double; }; template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type; template <typename T, typename = void> struct rec_value_type { using type = T; }; template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> { using type = typename rec_value_type<typename T::value_type>::type; }; template <typename T> using rec_value_type_t = typename rec_value_type<T>::type; } // namespace suisen // ! type aliases using i128 = __int128_t; using u128 = __uint128_t; template <typename T> using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>; // ! macros (internal) #define DETAIL_OVERLOAD2(_1,_2,name,...) name #define DETAIL_OVERLOAD3(_1,_2,_3,name,...) name #define DETAIL_OVERLOAD4(_1,_2,_3,_4,name,...) name #define DETAIL_REP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s)) #define DETAIL_REP3(i,l,r) DETAIL_REP4(i,l,r,1) #define DETAIL_REP2(i,n) DETAIL_REP3(i,0,n) #define DETAIL_REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s)) #define DETAIL_REPINF2(i,l) DETAIL_REPINF3(i,l,1) #define DETAIL_REPINF1(i) DETAIL_REPINF2(i,0) #define DETAIL_RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s)) #define DETAIL_RREP3(i,l,r) DETAIL_RREP4(i,l,r,1) #define DETAIL_RREP2(i,n) DETAIL_RREP3(i,0,n) #define DETAIL_CAT_I(a, b) a##b #define DETAIL_CAT(a, b) DETAIL_CAT_I(a, b) #define DETAIL_UNIQVAR(tag) DETAIL_CAT(tag, __LINE__) // ! macros #define REP(...) DETAIL_OVERLOAD4(__VA_ARGS__, DETAIL_REP4 , DETAIL_REP3 , DETAIL_REP2 )(__VA_ARGS__) #define RREP(...) DETAIL_OVERLOAD4(__VA_ARGS__, DETAIL_RREP4 , DETAIL_RREP3 , DETAIL_RREP2 )(__VA_ARGS__) #define REPINF(...) DETAIL_OVERLOAD3(__VA_ARGS__, DETAIL_REPINF3, DETAIL_REPINF2, DETAIL_REPINF1)(__VA_ARGS__) #define LOOP(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> DETAIL_UNIQVAR(loop_variable) = n; DETAIL_UNIQVAR(loop_variable) --> 0;) #define ALL(iterable) std::begin(iterable), std::end(iterable) #define INPUT(type, ...) type __VA_ARGS__; read(__VA_ARGS__) // ! debug #ifdef LOCAL # define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__) template <class T, class... Args> void debug_internal(const char* s, T&& first, Args&&... args) { constexpr const char* prefix = "[\033[32mDEBUG\033[m] "; constexpr const char* open_brakets = sizeof...(args) == 0 ? "" : "("; constexpr const char* close_brakets = sizeof...(args) == 0 ? "" : ")"; std::cerr << prefix << open_brakets << s << close_brakets << ": " << open_brakets << std::forward<T>(first); ((std::cerr << ", " << std::forward<Args>(args)), ...); std::cerr << close_brakets << "\n"; } #else # define debug(...) void(0) #endif // ! I/O utilities // __int128_t std::ostream& operator<<(std::ostream& dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char* d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } // __uint128_t std::ostream& operator<<(std::ostream& dest, __uint128_t value) { std::ostream::sentry s(dest); if (s) { char buffer[128]; char* d = std::end(buffer); do { --d; *d = "0123456789"[value % 10]; value /= 10; } while (value != 0); int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } // pair template <typename T, typename U> std::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) { return out << a.first << ' ' << a.second; } // tuple template <unsigned int N = 0, typename ...Args> std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) { if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) return out; else { out << std::get<N>(a); if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) out << ' '; return operator<<<N + 1>(out, a); } } // vector template <typename T> std::ostream& operator<<(std::ostream& out, const std::vector<T>& a) { for (auto it = a.begin(); it != a.end();) { out << *it; if (++it != a.end()) out << ' '; } return out; } // array template <typename T, size_t N> std::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) { for (auto it = a.begin(); it != a.end();) { out << *it; if (++it != a.end()) out << ' '; } return out; } inline void print() { std::cout << '\n'; } template <typename Head, typename... Tail> inline void print(const Head& head, const Tail &...tails) { std::cout << head; if (sizeof...(tails)) std::cout << ' '; print(tails...); } template <typename Iterable> auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) { for (auto it = v.begin(); it != v.end();) { std::cout << *it; if (++it != v.end()) std::cout << sep; } std::cout << end; } __int128_t stoi128(const std::string& s) { __int128_t ret = 0; for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0'; if (s[0] == '-') ret = -ret; return ret; } __uint128_t stou128(const std::string& s) { __uint128_t ret = 0; for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } // __int128_t std::istream& operator>>(std::istream& in, __int128_t& v) { std::string s; in >> s; v = stoi128(s); return in; } // __uint128_t std::istream& operator>>(std::istream& in, __uint128_t& v) { std::string s; in >> s; v = stou128(s); return in; } // pair template <typename T, typename U> std::istream& operator>>(std::istream& in, std::pair<T, U>& a) { return in >> a.first >> a.second; } // tuple template <unsigned int N = 0, typename ...Args> std::istream& operator>>(std::istream& in, std::tuple<Args...>& a) { if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) return in; else return operator>><N + 1>(in >> std::get<N>(a), a); } // vector template <typename T> std::istream& operator>>(std::istream& in, std::vector<T>& a) { for (auto it = a.begin(); it != a.end(); ++it) in >> *it; return in; } // array template <typename T, size_t N> std::istream& operator>>(std::istream& in, std::array<T, N>& a) { for (auto it = a.begin(); it != a.end(); ++it) in >> *it; return in; } template <typename ...Args> void read(Args &...args) { (std::cin >> ... >> args); } // ! integral utilities // Returns pow(-1, n) template <typename T> constexpr inline int pow_m1(T n) { return -(n & 1) | 1; } // Returns pow(-1, n) template <> constexpr inline int pow_m1<bool>(bool n) { return -int(n) | 1; } // Returns floor(x / y) template <typename T> constexpr inline T fld(const T x, const T y) { return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y; } template <typename T> constexpr inline T cld(const T x, const T y) { return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y; } template <typename T, std::enable_if_t<std::negation_v<suisen::is_nbit<T, 64>>, std::nullptr_t> = nullptr> __attribute__((target("popcnt"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); } template <typename T, std::enable_if_t<suisen::is_nbit_v<T, 64>, std::nullptr_t> = nullptr> __attribute__((target("popcnt"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); } template <typename T, std::enable_if_t<std::negation_v<suisen::is_nbit<T, 64>>, std::nullptr_t> = nullptr> constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; } template <typename T, std::enable_if_t<suisen::is_nbit_v<T, 64>, std::nullptr_t> = nullptr> constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; } template <typename T, std::enable_if_t<std::negation_v<suisen::is_nbit<T, 64>>, std::nullptr_t> = nullptr> constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; } template <typename T, std::enable_if_t<suisen::is_nbit_v<T, 64>, std::nullptr_t> = nullptr> constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; } template <typename T> constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); } template <typename T> constexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); } template <typename T> constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; } template <typename T> constexpr inline int parity(const T x) { return popcount(x) & 1; } // ! container template <typename T, typename Comparator> auto priqueue_comp(const Comparator comparator) { return std::priority_queue<T, std::vector<T>, Comparator>(comparator); } template <typename Container> void sort_unique_erase(Container& a) { std::sort(a.begin(), a.end()); a.erase(std::unique(a.begin(), a.end()), a.end()); } template <typename InputIterator, typename BiConsumer> auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) { if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr); } template <typename Container, typename BiConsumer> auto foreach_adjacent_values(Container &&c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) { foreach_adjacent_values(c.begin(), c.end(), f); } // ! other utilities // x <- min(x, y). returns true iff `x` has chenged. template <typename T> inline bool chmin(T& x, const T& y) { return y >= x ? false : (x = y, true); } // x <- max(x, y). returns true iff `x` has chenged. template <typename T> inline bool chmax(T& x, const T& y) { return y <= x ? false : (x = y, true); } template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr> std::string bin(T val, int bit_num = -1) { std::string res; if (bit_num != -1) { for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1); } else { for (; val; val >>= 1) res += '0' + (val & 1); std::reverse(res.begin(), res.end()); } return res; } template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr> std::vector<T> digits_low_to_high(T val, T base = 10) { std::vector<T> res; for (; val; val /= base) res.push_back(val % base); if (res.empty()) res.push_back(T{ 0 }); return res; } template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr> std::vector<T> digits_high_to_low(T val, T base = 10) { auto res = digits_low_to_high(val, base); std::reverse(res.begin(), res.end()); return res; } template <typename T> std::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) { std::ostringstream ss; for (auto it = v.begin(); it != v.end();) { ss << *it; if (++it != v.end()) ss << sep; } ss << end; return ss.str(); } template <typename Func, typename Seq> auto transform_to_vector(const Func &f, const Seq &s) { std::vector<std::invoke_result_t<Func, typename Seq::value_type>> v; v.reserve(std::size(s)), std::transform(std::begin(s), std::end(s), std::back_inserter(v), f); return v; } template <typename T, typename Seq> auto copy_to_vector(const Seq &s) { std::vector<T> v; v.reserve(std::size(s)), std::copy(std::begin(s), std::end(s), std::back_inserter(v)); return v; } template <typename Seq> Seq concat(Seq s, const Seq &t) { s.reserve(std::size(s) + std::size(t)); std::copy(std::begin(t), std::end(t), std::back_inserter(s)); return s; } template <typename Seq> std::vector<Seq> split(const Seq s, typename Seq::value_type delim) { std::vector<Seq> res; for (auto itl = std::begin(s), itr = itl;; itl = ++itr) { while (itr != std::end(s) and *itr != delim) ++itr; res.emplace_back(itl, itr); if (itr == std::end(s)) return res; } } int digit_to_int(char c) { return c - '0'; } int lowercase_to_int(char c) { return c - 'a'; } int uppercase_to_int(char c) { return c - 'A'; } std::vector<int> digit_str_to_ints(const std::string &s) { return transform_to_vector(digit_to_int, s); } std::vector<int> lowercase_str_to_ints(const std::string &s) { return transform_to_vector(lowercase_to_int, s); } std::vector<int> uppercase_str_to_ints(const std::string &s) { return transform_to_vector(uppercase_to_int, s); } const std::string Yes = "Yes", No = "No", YES = "YES", NO = "NO"; namespace suisen {} using namespace suisen; using namespace std; struct io_setup { io_setup(int precision = 20) { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(precision); } } io_setup_ {}; // ! code from here #include <algorithm> #include <cassert> #include <cstdint> #include <optional> #include <string> #include <random> #include <tuple> #include <vector> #include <utility> namespace suisen::internal::implicit_treap { template <typename T, typename Derived> struct Node { using random_engine = std::mt19937; static inline random_engine rng{ std::random_device{}() }; using priority_type = std::invoke_result_t<random_engine>; static priority_type random_priority() { return rng(); } using node_type = Derived; using node_pointer = uint32_t; using size_type = uint32_t; using difference_type = int32_t; using value_type = T; using pointer = value_type*; using const_pointer = const value_type*; using reference = value_type&; using const_reference = const value_type&; static inline std::vector<node_type> _nodes{}; static inline std::vector<node_pointer> _erased{}; static constexpr node_pointer null = ~node_pointer(0); node_pointer _ch[2]{ null, null }; value_type _val; size_type _size; priority_type _priority; node_pointer _prev = null, _next = null; Node(const value_type val = {}): _val(val), _size(1), _priority(random_priority()) {} static void reserve(size_type capacity) { _nodes.reserve(capacity); } static bool is_null(node_pointer t) { return t == null; } static bool is_not_null(node_pointer t) { return not is_null(t); } static node_type& node(node_pointer t) { return _nodes[t]; } static const node_type& const_node(node_pointer t) { return _nodes[t]; } static value_type& value(node_pointer t) { return node(t)._val; } static value_type set_value(node_pointer t, const value_type& new_val) { return std::exchange(value(t), new_val); } static bool empty(node_pointer t) { return is_null(t); } static size_type& size(node_pointer t) { return node(t)._size; } static size_type safe_size(node_pointer t) { return empty(t) ? 0 : size(t); } static priority_type& priority(node_pointer t) { return node(t)._priority; } static void set_priority(node_pointer t, priority_type new_priority) { priority(t) = new_priority; } static node_pointer& prev(node_pointer t) { return node(t)._prev; } static node_pointer& next(node_pointer t) { return node(t)._next; } static void link(node_pointer l, node_pointer r) { next(l) = r, prev(r) = l; } static node_pointer min(node_pointer t) { while (true) { node_pointer nt = child0(t); if (is_null(nt)) return t; t = nt; } } static node_pointer max(node_pointer t) { while (true) { node_pointer nt = child1(t); if (is_null(nt)) return t; t = nt; } } static node_pointer& child0(node_pointer t) { return node(t)._ch[0]; } static node_pointer& child1(node_pointer t) { return node(t)._ch[1]; } static node_pointer& child(node_pointer t, bool b) { return node(t)._ch[b]; } static node_pointer set_child0(node_pointer t, node_pointer cid) { return std::exchange(child0(t), cid); } static node_pointer set_child1(node_pointer t, node_pointer cid) { return std::exchange(child1(t), cid); } static node_pointer set_child(node_pointer t, bool b, node_pointer cid) { return std::exchange(child(t, b), cid); } static node_pointer update(node_pointer t) { // t : not null size(t) = safe_size(child0(t)) + safe_size(child1(t)) + 1; return t; } static node_pointer empty_node() { return null; } template <typename ...Args> static node_pointer create_node(Args &&...args) { if (_erased.size()) { node_pointer res = _erased.back(); _erased.pop_back(); node(res) = node_type(std::forward<Args>(args)...); return res; } else { node_pointer res = _nodes.size(); _nodes.emplace_back(std::forward<Args>(args)...); return res; } } static void delete_node(node_pointer t) { _erased.push_back(t); } static void delete_tree(node_pointer t) { if (is_null(t)) return; delete_tree(child0(t)); delete_tree(child1(t)); delete_node(t); } template <typename ...Args> static node_pointer build(Args &&... args) { std::vector<value_type> dat(std::forward<Args>(args)...); const size_t n = dat.size(); std::vector<priority_type> priorities(n); std::generate(priorities.begin(), priorities.end(), random_priority); std::make_heap(priorities.begin(), priorities.end()); std::vector<node_pointer> nodes(n); auto rec = [&](auto rec, size_t heap_index, size_t dat_index_offset) -> std::pair<size_t, node_pointer> { if (heap_index >= n) return { 0, null }; auto [lsiz, lch] = rec(rec, 2 * heap_index + 1, dat_index_offset); dat_index_offset += lsiz; node_pointer root = create_node(std::move(dat[dat_index_offset])); nodes[dat_index_offset] = root; set_priority(root, priorities[heap_index]); if (dat_index_offset) { link(nodes[dat_index_offset - 1], root); } dat_index_offset += 1; auto [rsiz, rch] = rec(rec, 2 * heap_index + 2, dat_index_offset); set_child0(root, lch); set_child1(root, rch); return { lsiz + 1 + rsiz, node_type::update(root) }; }; return rec(rec, 0, 0).second; } static std::pair<node_pointer, node_pointer> split(node_pointer t, size_type k) { if (k == 0) return { null, t }; if (k == size(t)) return { t, null }; static std::vector<node_pointer> lp{}, rp{}; while (true) { if (const size_type lsiz = safe_size(child0(t)); k <= lsiz) { if (rp.size()) set_child0(rp.back(), t); rp.push_back(t); if (k == lsiz) { if (lp.size()) set_child1(lp.back(), child0(t)); node_pointer lt = set_child0(t, null), rt = null; while (lp.size()) node_type::update(lt = lp.back()), lp.pop_back(); while (rp.size()) node_type::update(rt = rp.back()), rp.pop_back(); return { lt, rt }; } t = child0(t); } else { if (lp.size()) set_child1(lp.back(), t); lp.push_back(t); t = child1(t); k -= lsiz + 1; } } } static std::tuple<node_pointer, node_pointer, node_pointer> split(node_pointer t, size_type l, size_type r) { auto [tlm, tr] = split(t, r); auto [tl, tm] = split(tlm, l); return { tl, tm, tr }; } static node_pointer merge_impl(node_pointer tl, node_pointer tr) { if (priority(tl) < priority(tr)) { if (node_pointer tm = child0(tr); is_null(tm)) { link(max(tl), tr); set_child0(tr, tl); } else { set_child0(tr, merge(tl, tm)); } return node_type::update(tr); } else { if (node_pointer tm = child1(tl); is_null(tm)) { link(tl, min(tr)); set_child1(tl, tr); } else { set_child1(tl, merge(tm, tr)); } return node_type::update(tl); } } static node_pointer merge(node_pointer tl, node_pointer tr) { if (is_null(tl)) return tr; if (is_null(tr)) return tl; return merge_impl(tl, tr); } static node_pointer merge(node_pointer tl, node_pointer tm, node_pointer tr) { return merge(merge(tl, tm), tr); } static node_pointer insert_impl(node_pointer t, size_type k, node_pointer new_node) { if (is_null(t)) return new_node; static std::vector<node_pointer> st; bool b = false; while (true) { if (is_null(t) or priority(new_node) > priority(t)) { if (is_null(t)) { t = new_node; } else { auto [tl, tr] = split(t, k); if (is_not_null(tl)) link(max(tl), new_node); if (is_not_null(tr)) link(new_node, min(tr)); set_child0(new_node, tl); set_child1(new_node, tr); t = node_type::update(new_node); } if (st.size()) { set_child(st.back(), b, t); do t = node_type::update(st.back()), st.pop_back(); while (st.size()); } return t; } else { if (const size_type lsiz = safe_size(child0(t)); k <= lsiz) { if (k == lsiz) link(new_node, t); st.push_back(t), b = false; t = child0(t); } else { if (k == lsiz + 1) link(t, new_node); st.push_back(t), b = true; t = child1(t); k -= lsiz + 1; } } } } template <typename ...Args> static node_pointer insert(node_pointer t, size_type k, Args &&...args) { return insert_impl(t, k, create_node(std::forward<Args>(args)...)); } static std::pair<node_pointer, value_type> erase(node_pointer t, size_type k) { if (const size_type lsiz = safe_size(child0(t)); k == lsiz) { delete_node(t); return { merge(child0(t), child1(t)), std::move(value(t)) }; } else if (k < lsiz) { auto [c0, v] = erase(child0(t), k); set_child0(t, c0); if (is_not_null(c0) and k == lsiz - 1) link(max(c0), t); return { node_type::update(t), std::move(v) }; } else { auto [c1, v] = erase(child1(t), k - (lsiz + 1)); set_child1(t, c1); if (is_not_null(c1) and k == lsiz + 1) link(t, min(c1)); return { node_type::update(t), std::move(v) }; } } static node_pointer rotate(node_pointer t, size_type k) { auto [tl, tr] = split(t, k); return merge(tr, tl); } static node_pointer rotate(node_pointer t, size_type l, size_type m, size_type r) { auto [tl, tm, tr] = split(t, l, r); return merge(tl, rotate(tm, m - l), tr); } template <typename Func> static node_pointer set_update(node_pointer t, size_type k, const Func& f) { if (const size_type lsiz = safe_size(child0(t)); k == lsiz) { value_type& val = value(t); val = f(const_cast<const value_type&>(val)); } else if (k < lsiz) { set_child0(t, set_update(child0(t), k, f)); } else { set_child1(t, set_update(child1(t), k - (lsiz + 1), f)); } return node_type::update(t); } static std::vector<value_type> dump(node_pointer t) { std::vector<value_type> res; res.reserve(safe_size(t)); auto rec = [&](auto rec, node_pointer t) -> void { if (is_null(t)) return; rec(rec, child0(t)); res.push_back(value(t)); rec(rec, child1(t)); }; rec(rec, t); return res; } template <bool reversed_, bool constant_> struct NodeIterator { static constexpr bool constant = constant_; static constexpr bool reversed = reversed_; friend Node; friend Derived; using difference_type = Node::difference_type; using value_type = Node::value_type; using pointer = std::conditional_t<constant, Node::const_pointer, Node::pointer>; using reference = std::conditional_t<constant, Node::const_reference, Node::reference>; using iterator_category = std::random_access_iterator_tag; NodeIterator(): NodeIterator(null) {} explicit NodeIterator(node_pointer root): NodeIterator(root, 0, null) {} NodeIterator(const NodeIterator<reversed, not constant>& it): NodeIterator(it._root, it._index, it._cur) {} reference operator*() const { if (is_null(_cur) and _index != safe_size(_root)) { _cur = _root; for (size_type k = _index;;) { if (size_type siz = safe_size(child(_cur, reversed)); k == siz) { break; } else if (k < siz) { _cur = child(_cur, reversed); } else { _cur = child(_cur, not reversed); k -= siz + 1; } } } return value(_cur); } reference operator[](difference_type k) const { return *((*this) + k); } NodeIterator& operator++() { return *this += 1; } NodeIterator& operator--() { return *this -= 1; } NodeIterator& operator+=(difference_type k) { return suc(+k), * this; } NodeIterator& operator-=(difference_type k) { return suc(-k), * this; } NodeIterator operator++(int) { NodeIterator res = *this; ++(*this); return res; } NodeIterator operator--(int) { NodeIterator res = *this; --(*this); return res; } friend NodeIterator operator+(NodeIterator it, difference_type k) { return it += k; } friend NodeIterator operator+(difference_type k, NodeIterator it) { return it += k; } friend NodeIterator operator-(NodeIterator it, difference_type k) { return it -= k; } friend difference_type operator-(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index - rhs._index; } friend bool operator==(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index == rhs._index; } friend bool operator!=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index != rhs._index; } friend bool operator<(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index < rhs._index; } friend bool operator>(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index > rhs._index; } friend bool operator<=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index <= rhs._index; } friend bool operator>=(const NodeIterator& lhs, const NodeIterator& rhs) { return lhs._index >= rhs._index; } static NodeIterator begin(node_pointer root) { return NodeIterator(root, 0, null); } static NodeIterator end(node_pointer root) { return NodeIterator(root, safe_size(root), null); } int size() const { return safe_size(_root); } int index() const { return _index; } private: node_pointer _root; size_type _index; mutable node_pointer _cur; // it==end() or uninitialized (updates only index) NodeIterator(node_pointer root, size_type index, node_pointer cur): _root(root), _index(index), _cur(cur) {} void suc(difference_type k) { _index += k; if (_index == safe_size(_root) or std::abs(k) >= 20) _cur = null; if (is_null(_cur)) return; const bool positive = k < 0 ? (k = -k, reversed) : not reversed; if (positive) { while (k-- > 0) _cur = next(_cur); } else { while (k-- > 0) _cur = prev(_cur); } } node_pointer root() const { return _root; } void set_root(node_pointer new_root, size_type new_index) { _root = new_root, _index = new_index; } node_pointer get_child0() const { return child0(_cur); } node_pointer get_child1() const { return child1(_cur); } template <typename Predicate> static NodeIterator binary_search(node_pointer t, const Predicate& f) { NodeIterator res(t, safe_size(t), null); if (is_null(t)) return res; NodeIterator it(t, safe_size(child0(t)), t); while (is_not_null(it._cur)) { if (f(it)) { res = it; it._cur = it.get_child0(); it._index -= is_null(it._cur) ? 1 : safe_size(it.get_child1()) + 1; } else { it._cur = it.get_child1(); it._index += is_null(it._cur) ? 1 : safe_size(it.get_child0()) + 1; } } return res; } size_type get_gap_index_left() const { if constexpr (reversed) return size() - index(); else return index(); } size_type get_element_index_left() const { if constexpr (reversed) return size() - index() - 1; else return index(); } }; using iterator = NodeIterator<false, false>; using reverse_iterator = NodeIterator<true, false>; using const_iterator = NodeIterator<false, true>; using const_reverse_iterator = NodeIterator<true, true>; template <typename> struct is_node_iterator: std::false_type {}; template <bool reversed_, bool constant_> struct is_node_iterator<NodeIterator<reversed_, constant_>>: std::true_type {}; template <typename X> static constexpr bool is_node_iterator_v = is_node_iterator<X>::value; static iterator begin(node_pointer t) { return iterator::begin(t); } static iterator end(node_pointer t) { return iterator::end(t); } static reverse_iterator rbegin(node_pointer t) { return reverse_iterator::begin(t); } static reverse_iterator rend(node_pointer t) { return reverse_iterator::end(t); } static const_iterator cbegin(node_pointer t) { return const_iterator::begin(t); } static const_iterator cend(node_pointer t) { return const_iterator::end(t); } static const_reverse_iterator crbegin(node_pointer t) { return const_reverse_iterator::begin(t); } static const_reverse_iterator crend(node_pointer t) { return const_reverse_iterator::end(t); } // Find the first element that satisfies the condition f : iterator -> { false, true }. // Returns const_iterator template <typename Iterator, typename Predicate, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static Iterator binary_search(node_pointer t, const Predicate& f) { return Iterator::binary_search(t, f); } // comp(T t, U u) = (t < u) template <typename Iterator, typename U, typename Compare = std::less<>, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static Iterator lower_bound(node_pointer t, const U& target, Compare comp) { return binary_search<Iterator>(t, [&](Iterator it) { return not comp(*it, target); }); } // comp(T u, U t) = (u < t) template <typename Iterator, typename U, typename Compare = std::less<>, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static Iterator upper_bound(node_pointer t, const U& target, Compare comp) { return binary_search<Iterator>(t, [&](Iterator it) { return comp(target, *it); }); } template <typename Iterator, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static node_pointer insert(Iterator it, const value_type& val) { return insert(it.root(), it.get_gap_index_left(), val); } template <typename Iterator, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static std::pair<node_pointer, value_type> erase(Iterator it) { return erase(it.root(), it.get_element_index_left()); } template <typename Iterator, std::enable_if_t<is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> static std::pair<node_pointer, node_pointer> split(Iterator it) { return split(it.root(), it.get_gap_index_left()); } }; } // namespace suisen::internal::implicit_treap namespace suisen { namespace internal::implicit_treap { template <typename T> struct DefaultNode: Node<T, DefaultNode<T>> { using base = Node<T, DefaultNode<T>>; using base::base; }; } template <typename T> class DynamicArray { using node_type = internal::implicit_treap::DefaultNode<T>; using node_pointer = typename node_type::node_pointer; node_pointer _root; struct node_pointer_construct {}; DynamicArray(node_pointer root, node_pointer_construct): _root(root) {} public: using value_type = typename node_type::value_type; DynamicArray(): _root(node_type::empty_node()) {} explicit DynamicArray(size_t n, const value_type& fill_value = {}): _root(node_type::build(n, fill_value)) {} template <typename U> DynamicArray(const std::vector<U>& dat) : _root(node_type::build(dat.begin(), dat.end())) {} void free() { node_type::delete_tree(_root); _root = node_type::empty_node(); } void clear() { free(); } static void reserve(size_t capacity) { node_type::reserve(capacity); } bool empty() const { return node_type::empty(_root); } int size() const { return node_type::safe_size(_root); } value_type& operator[](size_t k) { assert(k < size_t(size())); return begin()[k]; } const value_type& operator[](size_t k) const { assert(k < size_t(size())); return cbegin()[k]; } value_type& front() { return *begin(); } value_type& back() { return *rbegin(); } const value_type& front() const { return *cbegin(); } const value_type& back() const { return *crbegin(); } void insert(size_t k, const value_type& val) { assert(k <= size_t(size())); _root = node_type::insert(_root, k, val); } void push_front(const value_type& val) { insert(0, val); } void push_back(const value_type& val) { insert(size(), val); } value_type erase(size_t k) { assert(k <= size_t(size())); value_type v; std::tie(_root, v) = node_type::erase(_root, k); return v; } value_type pop_front() { return erase(0); } value_type pop_back() { return erase(size() - 1); } // Split immediately before the k-th element. DynamicArray split(size_t k) { assert(k <= size_t(size())); node_pointer root_r; std::tie(_root, root_r) = node_type::split(_root, k); return DynamicArray(root_r, node_pointer_construct{}); } void merge(DynamicArray r) { _root = node_type::merge(_root, r._root); } void rotate(size_t k) { assert(k <= size_t(size())); _root = node_type::rotate(_root, k); } void rotate(size_t l, size_t m, size_t r) { assert(l <= m and m <= r and r <= size_t(size())); _root = node_type::rotate(_root, l, m, r); } std::vector<value_type> dump() const { return node_type::dump(_root); } using iterator = typename node_type::iterator; using reverse_iterator = typename node_type::reverse_iterator; using const_iterator = typename node_type::const_iterator; using const_reverse_iterator = typename node_type::const_reverse_iterator; iterator begin() { return node_type::begin(_root); } iterator end() { return node_type::end(_root); } reverse_iterator rbegin() { return node_type::rbegin(_root); } reverse_iterator rend() { return node_type::rend(_root); } const_iterator begin() const { return cbegin(); } const_iterator end() const { return cend(); } const_reverse_iterator rbegin() const { return crbegin(); } const_reverse_iterator rend() const { return crend(); } const_iterator cbegin() const { return node_type::cbegin(_root); } const_iterator cend() const { return node_type::cend(_root); } const_reverse_iterator crbegin() const { return node_type::crbegin(_root); } const_reverse_iterator crend() const { return node_type::crend(_root); } // Find the first element that satisfies the condition f. // Returns { position, optional(value) } // Requirements: f(A[i]) must be monotonic template <typename Predicate> iterator binary_search(const Predicate& f) { return node_type::template binary_search<iterator>(_root, f); } // comp(T t, U u) = (t < u) // Requirements: sequence is sorted template <typename U, typename Compare = std::less<>> iterator lower_bound(const U& target, Compare comp = {}) { return node_type::template lower_bound<iterator>(_root, target, comp); } // comp(T u, U t) = (u < t) // Requirements: sequence is sorted template <typename U, typename Compare = std::less<>> iterator upper_bound(const U& target, Compare comp = {}) { return node_type::template upper_bound<iterator>(_root, target, comp); } // Find the first element that satisfies the condition f. // Returns { position, optional(value) } // Requirements: f(A[i]) must be monotonic template <typename Predicate> const_iterator binary_search(const Predicate& f) const { return node_type::template binary_search<const_iterator>(_root, f); } // comp(T t, U u) = (t < u) // Requirements: sequence is sorted template <typename U, typename Compare = std::less<>> const_iterator lower_bound(const U& target, Compare comp = {}) const { return node_type::template lower_bound<const_iterator>(_root, target, comp); } // comp(T u, U t) = (u < t) // Requirements: sequence is sorted template <typename U, typename Compare = std::less<>> const_iterator upper_bound(const U& target, Compare comp = {}) const { return node_type::template upper_bound<const_iterator>(_root, target, comp); } template <typename Iterator, std::enable_if_t<node_type::template is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> void insert(Iterator it, const value_type &val) { _root = node_type::insert(it, val); } template <typename Iterator, std::enable_if_t<node_type::template is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> value_type erase(Iterator it) { value_type erased; std::tie(_root, erased) = node_type::erase(it); return erased; } template <typename Iterator, std::enable_if_t<node_type::template is_node_iterator_v<Iterator>, std::nullptr_t> = nullptr> DynamicArray split(Iterator it) { node_pointer root_r; std::tie(_root, root_r) = node_type::split(it); return DynamicArray(root_r, node_pointer_construct{}); } // handling internal nodes using internal_node = node_type; using internal_node_pointer = node_pointer; internal_node_pointer& root_node() { return _root; } const internal_node_pointer& root_node() const { return _root; } void set_root_node(internal_node_pointer new_root) { root_node() = new_root; } }; } // namespace suisen bool ops[4][2][2] { { { 0, 0 }, { 0, 1 } }, { { 0, 1 }, { 1, 1 }, }, { { 0, 1 }, { 1, 0 } }, { { 1, 1 }, { 0, 1 } } }; array<char, 256> op; void solve() { op['a'] = 0; op['o'] = 1; op['x'] = 2; op['i'] = 3; int n; read(n); vector<bool> init_a(n); vector<int> init_y(n - 1); REP(i, n) { string s; read(s); init_a[i] = s == "True"; } REP(i, n - 1) { string s; read(s); init_y[i] = op[s.front()]; } DynamicArray<bool> a(init_a); DynamicArray<int> y(init_y); LOOP(n - 1) { // debug(a.dump()); // debug(y.dump()); int pos; read(pos); --pos; a[pos] = ops[y[pos]][a[pos]][a[pos + 1]]; y.erase(pos); a.erase(pos + 1); } print(a.front() ? "True" : "False"); } int main() { int t; read(t); LOOP(t) { solve(); } return 0; }