// BEGIN: main.cpp #line 1 "main.cpp" #define PROBLEM "https://yukicoder.me/problems/no/2292" // BEGIN: my_template.hpp #line 2 "my_template.hpp" #if defined(USE_PCH) #include #if defined(__GNUC__) #pragma GCC optimize("Ofast,unroll-loops") #if !defined(MASPY_NON_AVX2) #pragma GCC target("avx2,popcnt") #endif #endif #else // MASPY_TEMPLATE_BODY #include #include #if defined(__GNUC__) #pragma GCC optimize("Ofast,unroll-loops") #endif using namespace std; using ll = long long; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i128 = __int128; using u128 = unsigned __int128; using f128 = __float128; template constexpr bool dependent_false = false; template constexpr T infty = [] { static_assert(dependent_false, "infty is not defined"); return T{}; }(); template <> constexpr int infty = 1'010'000'000; template <> constexpr ll infty = 4'040'000'000'000'000'000; template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr i128 infty = i128(infty) * 4'000'000'000'000'000'000; template <> constexpr double infty = infty; template <> constexpr long double infty = infty; using pi = pair; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using pq_max = priority_queue; template using pq_min = priority_queue, greater>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR1_R(a) for (ll i = ll(a) - 1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = ll(a) - 1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i) #define overload3(a, b, c, d, ...) d #define FOR(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll // require y > 0 template T floor(T x, T y) { return x / y - (x % y < 0); } // require y > 0 template T ceil(T x, T y) { return (x / y) + (x % y > 0); } // require y > 0 template T bmod(T x, T y) { T r = x % y; return (r < 0 ? r + y : r); } // require y > 0 template pair divmod(T x, T y) { T q = x / y, r = x % y; if (r < 0) --q, r += y; return {q, r}; } constexpr auto TEN = [] { array A{}; A[0] = 1; for (int i = 1; i < 20; ++i) A[i] = 10 * A[i - 1]; return A; }(); template T SUM(const U &A) { return std::accumulate(A.begin(), A.end(), T{}); } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) template inline long long LB(const C &c, const T &x) { return lower_bound(c.begin(), c.end(), x) - c.begin(); } template inline long long UB(const C &c, const T &x) { return upper_bound(c.begin(), c.end(), x) - c.begin(); } #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) template T POP(deque &que) { T a = que.front(); que.pop_front(); return a; } template T POP(priority_queue &que) { T a = que.top(); que.pop(); return a; } template T POP(vc &que) { T a = que.back(); que.pop_back(); return a; } template i128 binary_search(F check, i128 ok, i128 ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (1) { i128 x = (ok + ng) / 2; if (x == ok || x == ng) break; (check(x) ? ok : ng) = x; } return ok; } template double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; (check(x) ? ok : ng) = x; } return (ok + ng) / 2; } template inline bool chmax(T &a, const S &b) { T c = max(a, b); bool changed = (c != a); a = c; return changed; } template inline bool chmin(T &a, const S &b) { T c = min(a, b); bool changed = (c != a); a = c; return changed; } // bit operations int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); } int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); } int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template T kth_bit(int k) { assert(0 <= k && k < int(8 * sizeof(T))); return T(1) << k; } template bool has_kth_bit(T x, int k) { assert(0 <= k && k < int(8 * sizeof(T))); return x >> k & 1; } template struct all_bit { static_assert(is_integral::value); T s; all_bit(T s) : s(s) { assert(s >= 0); } struct iter { T s; int operator*() const { return lowbit(s); } void operator++() { s &= s - 1; } bool operator!=(nullptr_t) const { return s; } }; iter begin() const { return {s}; } nullptr_t end() const { return nullptr; } }; template struct all_subset { static_assert(is_integral::value); T s; all_subset(T s) : s(s) { assert(s >= 0); } struct iter { T s, t; bool done = false; T operator*() const { return t; } void operator++() { done = (t == 0); t = (t - 1) & s; } bool operator!=(nullptr_t) const { return !done; } }; iter begin() const { return {s, s}; } nullptr_t end() const { return nullptr; } }; constexpr u64 full_mask(int n) { assert(0 <= n && n <= 64); return n == 64 ? -1ULL : (1ULL << n) - 1; } u64 bit_reverse(u64 x) { x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL); x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL); x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL); x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL); x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL); x = (x << 32) | (x >> 32); return x; } // ? は -1 vc s_to_vi(const string &S, char first_char) { vc A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template vc cumsum(const vc &A, int off = 1) { int N = A.size(); vc B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort template vc argsort(const vc &A) { vc ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template vc rearrange(const vc &A, const vc &I) { vc B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template void concat(vc &first, const Vectors &...others) { first.reserve(first.size() + (others.size() + ... + 0)); (first.insert(first.end(), others.begin(), others.end()), ...); } // i128 template , int> = 0> constexpr i128 abs(T x) { return x < 0 ? -x : x; } constexpr i128 gcd(i128 a, i128 b) { while (b != 0) { i128 c = a % b; a = b, b = c; } return abs(a); } #endif // MASPY_TEMPLATE_BODY // END: my_template.hpp // BEGIN: other/io.hpp #line 2 "other/io.hpp" #define FASTIO // https://judge.yosupo.jp/submission/21623 namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf uint32_t pil = 0, pir = 0, por = 0; bool input_eof = false; template constexpr bool is_signed_integer_v = is_signed_v || is_same_v; template struct unsigned_integer { using type = make_unsigned_t; }; template <> struct unsigned_integer { using type = u128; }; template <> struct unsigned_integer { using type = u128; }; template using unsigned_integer_t = typename unsigned_integer::type; [[noreturn]] inline void input_error(const char *message) { fputs(message, stderr); fputc('\n', stderr); exit(EXIT_FAILURE); } struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { uint32_t n = pir - pil; memmove(ibuf, ibuf + pil, n); pil = 0; pir = n; if (input_eof) return; pir += fread(ibuf + pir, 1, SZ - pir, stdin); if (ferror(stdin)) input_error("fastio: input error"); if (feof(stdin)) { input_eof = true; // Allows the last token to end exactly at EOF without a trailing // whitespace. if (pir < SZ) ibuf[pir++] = '\n'; } } inline char get_char() { if (pil == pir) { load(); if (pil == pir) input_error("fastio: unexpected EOF"); } return ibuf[pil++]; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do c = get_char(); while (isspace(static_cast(c))); } void rd(string &x) { x.clear(); char c; do c = get_char(); while (isspace(static_cast(c))); do { x += c; c = get_char(); } while (!isspace(static_cast(c))); } template void rd_real(T &x) { string s; rd(s); x = stod(s); } template void rd_integer_slow(T &x) { char c; do c = get_char(); while (c < '-'); bool minus = 0; if constexpr (is_signed_integer_v) { if (c == '-') { minus = 1, c = get_char(); } } x = 0; assert('0' <= c && c <= '9'); while ('0' <= c && c <= '9') { x = x * 10 + (c & 15), c = get_char(); } assert(isspace(static_cast(c))); if constexpr (is_signed_integer_v) { if (minus) x = -x; } } template void rd_integer(T &x) { if (pil + 100 > pir) { load(); if (pil + 100 > pir) { rd_integer_slow(x); return; } } char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed_integer_v) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; assert('0' <= c && c <= '9'); while ('0' <= c && c <= '9') { x = x * 10 + (c & 15), c = ibuf[pil++]; } assert(isspace(static_cast(c))); if constexpr (is_signed_integer_v) { if (minus) x = -x; } } template enable_if_t || is_same_v || is_same_v> rd( T &x) { rd_integer(x); } template enable_if_t || is_same_v> rd(T &x) { rd_real(x); } template void rd(pair &p) { rd(p.first), rd(p.second); } template void rd_tuple(T &t) { if constexpr (N < tuple_size::value) { auto &x = get(t); rd(x); rd_tuple(t); } } template void rd(tuple &tpl) { rd_tuple(tpl); } template void rd(array &x) { for (auto &d : x) rd(d); } template void rd(vc &x) { for (auto &d : x) rd(d); } template void read(T &...x) { (rd(x), ...); } inline void wt_range(const char *s, size_t n) { size_t i = 0; while (i < n) { if (por == SZ) flush(); size_t chunk = min(n - i, (size_t)(SZ - por)); memcpy(obuf + por, s + i, chunk); por += chunk; i += chunk; } } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const char *s) { wt_range(s, strlen(s)); } void wt(const string &s) { wt_range(s.data(), s.size()); } template void wt_integer(T x) { if (por > SZ - 100) flush(); using U = unsigned_integer_t; U y = static_cast(x); if constexpr (is_signed_integer_v) { if (x < 0) { obuf[por++] = '-'; y = U(0) - y; } } int outi; for (outi = 96; y >= 10000; outi -= 4) { memcpy(out + outi, pre.num[y % 10000], 4); y /= 10000; } if (y >= 1000) { memcpy(obuf + por, pre.num[y], 4); por += 4; } else if (y >= 100) { memcpy(obuf + por, pre.num[y] + 1, 3); por += 3; } else if (y >= 10) { int q = (y * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (y - q * 10) | '0'; por += 2; } else obuf[por++] = y | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template inline void wt_real(T x) { static char buf[1000]; int n = std::snprintf(buf, sizeof(buf), "%.15f", (double)x); wt_range(buf, (size_t)n); } template enable_if_t || is_same_v || is_same_v> wt( T x) { wt_integer(x); } template enable_if_t || is_same_v> wt(T x) { wt_real(x); } inline void wt(bool b) { wt(static_cast('0' + (b ? 1 : 0))); } template void wt(const pair &val) { wt(val.first); wt(' '); wt(val.second); } template void wt_tuple(const T &t) { if constexpr (N < tuple_size::value) { if constexpr (N > 0) wt(' '); wt(get(t)); wt_tuple(t); } } template void wt(const tuple &tpl) { wt_tuple(tpl); } template void wt(const array &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template void wt(const vector &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template void print(Head &&head, Tail &&...tail) { wt(forward(head)); ((wt(' '), wt(forward(tail))), ...); wt('\n'); } // gcc expansion. called automaticall after main. void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::flush; using fastio::print; using fastio::read; #if defined(LOCAL) #define HDR "[DEBUG:", __func__, __LINE__, "]" #define SHOW(...) \ SHOW_IMPL(__VA_ARGS__, SHOW8, SHOW7, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, \ SHOW1) \ (__VA_ARGS__) #define SHOW_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define SHOW1(x) print(HDR, #x, "=", (x)), flush() #define SHOW2(x, y) print(HDR, #x, "=", (x), #y, "=", (y)), flush() #define SHOW3(x, y, z) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z)), flush() #define SHOW4(x, y, z, w) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush() #define SHOW5(x, y, z, w, v) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v)), \ flush() #define SHOW6(x, y, z, w, v, u) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u)), \ flush() #define SHOW7(x, y, z, w, v, u, t) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u), #t, "=", (t)), \ flush() #define SHOW8(x, y, z, w, v, u, t, s) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u), #t, "=", (t), #s, "=", (s)), \ flush() #else #define SHOW(...) #endif #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define U32(...) \ u32 __VA_ARGS__; \ read(__VA_ARGS__) #define U64(...) \ u64 __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ read(name) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); } void TIDAK(bool t = 1) { YA(!t); } void Alice(bool t = 1) { print(t ? "Alice" : "Bob"); } void Bob(bool t = 1) { Alice(!t); }// END: other/io.hpp // BEGIN: ds/segtree/dynamic_lazy_segtree.hpp // BEGIN: ds/node_pool.hpp #line 2 "ds/node_pool.hpp" // マルチテストケースでも確保済み chunk を再利用する template struct Node_Pool { union Slot { Node node; Slot* next; Slot() {} ~Slot() {} }; using np = Node*; static constexpr int CHUNK_SIZE = 1 << 12; vc> chunks; int chunk_id = 0; int pos = 0; Slot* free_head = nullptr; ~Node_Pool() { auto& cache = chunk_cache(); for (auto& p : chunks) cache.eb(std::move(p)); } template np create(Args&&... args) { Slot* s = new_slot(); return ::new (&s->node) Node(forward(args)...); } np clone(const np x) { assert(x); Slot* s = new_slot(); return ::new (&s->node) Node(*x); } void destroy(np x) { if (!x) return; x->~Node(); Slot* s = reinterpret_cast(x); s->next = free_head; free_head = s; } // 全 node を無効化する。 // 確保済み chunk は解放せず、次回以降に再利用する。 void reset() { free_head = nullptr; chunk_id = 0; pos = 0; } int used() const { return chunk_id * CHUNK_SIZE + pos; } private: static vc>& chunk_cache() { // static Node_Pool の destructor より先に破棄されないようにする。 static auto* cache = new vc>(); return *cache; } void alloc_chunk() { auto& cache = chunk_cache(); if (cache.empty()) { chunks.eb(make_unique(CHUNK_SIZE)); } else { chunks.eb(std::move(cache.back())); cache.pop_back(); } } Slot* new_slot() { if (free_head) { Slot* s = free_head; free_head = free_head->next; return s; } if (chunk_id == len(chunks)) alloc_chunk(); Slot* s = &chunks[chunk_id][pos++]; if (pos == CHUNK_SIZE) { ++chunk_id; pos = 0; } return s; } };// END: ds/node_pool.hpp #line 3 "ds/segtree/dynamic_lazy_segtree.hpp" template struct Dynamic_Lazy_SegTree_Default_Prod { using MX = typename AM::Monoid_X; using X = typename AM::X; X operator()(ll, ll) const { return MX::id(); } }; // set, multiply: 2H nodes 程度 // apply: 4H nodes 程度 template > struct Dynamic_Lazy_SegTree { using AM = ActedMonoid; using MX = typename AM::Monoid_X; using MA = typename AM::Monoid_A; using X = typename AM::X; using A = typename AM::A; using F = DefaultProd; F default_prod; struct Node { Node *l, *r; X x; A lazy; }; Node_Pool pool; const ll L0, R0; using np = Node *; Dynamic_Lazy_SegTree(ll L0, ll R0, F default_prod = F{}) : default_prod(std::move(default_prod)), L0(L0), R0(R0) {} np new_root() { return new_node(); } np new_node(const X x) { np c = pool.create(); c->l = c->r = nullptr; c->x = x, c->lazy = MA::id(); return c; } np new_node(ll l, ll r) { assert(l < r); return new_node(default_prod(l, r)); } np new_node() { return L0 == R0 ? nullptr : new_node(L0, R0); } np new_node(const vc &dat) { assert(L0 == 0 && R0 == len(dat)); auto dfs = [&](auto &dfs, ll l, ll r) -> Node * { if (l == r) return nullptr; if (r - l == 1) return new_node(dat[l]); ll m = (l + r) / 2; np l_root = dfs(dfs, l, m), r_root = dfs(dfs, m, r); X x = MX::op(l_root->x, r_root->x); np root = new_node(x); root->l = l_root, root->r = r_root; return root; }; return dfs(dfs, 0, len(dat)); } X prod(np root, ll l, ll r) { if (l == r || !root) return MX::id(); assert(L0 <= l && l < r && r <= R0); X x = MX::id(); prod_rec(root, L0, R0, l, r, x, MA::id()); return x; } X prod_all(np root) { return prod(root, L0, R0); } np set(np root, ll i, const X &x) { assert(root && L0 <= i && i < R0); root = clone(root); set_rec(root, L0, R0, i, x); return root; } np multiply(np root, ll i, const X &x) { assert(root && L0 <= i && i < R0); root = clone(root); multiply_rec(root, L0, R0, i, x); return root; } np apply(np root, ll l, ll r, const A &a) { if (l == r) return root; assert(root && L0 <= l && l < r && r <= R0); root = clone(root); apply_rec(root, L0, R0, l, r, a); return root; } template ll max_right(np root, F check, ll L) { assert(L0 <= L && L <= R0 && check(MX::id())); if (L == R0) return R0; assert(root); X x = MX::id(); return max_right_rec(root, check, L0, R0, L, x, MA::id()); } template ll min_left(np root, F check, ll R) { assert(L0 <= R && R <= R0 && check(MX::id())); if (R == L0) return L0; assert(root); X x = MX::id(); return min_left_rec(root, check, L0, R0, R, x, MA::id()); } // 生成済みの葉について f(idx, val, end) を呼ぶ template void enumerate(np root, F f) { bool end = false; auto dfs = [&](auto &dfs, np c, ll l, ll r, A a) -> void { if (!c) return; if (r - l == 1) { f(l, AM::act(c->x, a, 1), end); return; } ll m = (l + r) / 2; a = MA::op(c->lazy, a); dfs(dfs, c->l, l, m, a); if (end) return; dfs(dfs, c->r, m, r, a); if (end) return; }; dfs(dfs, root, L0, R0, MA::id()); } void reset() { pool.reset(); } // root[l:r) を apply(other[l:r),a) で上書きしたものを返す np copy_interval(np root, np other, ll l, ll r, A a) { static_assert(PERSISTENT); assert(L0 <= l && l <= r && r <= R0); if (l == r) return root; assert(root); if (root == other) return apply(root, l, r, a); root = clone(root); copy_interval_rec(root, other, L0, R0, l, r, a); return root; } private: np clone(np c) { if (!c || !PERSISTENT) return c; return pool.clone(c); } np own(np c, ll l, ll r) { return c ? clone(c) : new_node(l, r); } X value(np c, ll l, ll r) { return c ? c->x : default_prod(l, r); } void tag(np c, const A &a, ll len) { c->x = AM::act(c->x, a, len); c->lazy = MA::op(c->lazy, a); } // c は書き換え可能。左右の子も書き換え可能になったかを返す bool push(np c, ll l, ll m, ll r) { assert(r - l >= 2); if (c->lazy == MA::id()) return false; c->l = own(c->l, l, m); c->r = own(c->r, m, r); tag(c->l, c->lazy, m - l); tag(c->r, c->lazy, r - m); c->lazy = MA::id(); return true; } void pull(np c, ll l, ll m, ll r) { c->x = MX::op(value(c->l, l, m), value(c->r, m, r)); } void copy_interval_rec(np c, np d, ll l, ll r, ll ql, ll qr, A a) { // c[ql,qr) <- apply(d[ql,qr),a) // c はすでに新しくしてある if (ql <= l && r <= qr) { if (d) { c->x = d->x; c->lazy = d->lazy; c->l = d->l, c->r = d->r; } else { c->x = default_prod(l, r); c->lazy = MA::id(); c->l = c->r = nullptr; } tag(c, a, r - l); return; } ll m = (l + r) / 2; bool pushed = push(c, l, m, r); if (d) a = MA::op(d->lazy, a); if (ql < m) { if (!pushed) c->l = own(c->l, l, m); copy_interval_rec(c->l, d ? d->l : nullptr, l, m, ql, qr, a); } if (m < qr) { if (!pushed) c->r = own(c->r, m, r); copy_interval_rec(c->r, d ? d->r : nullptr, m, r, ql, qr, a); } pull(c, l, m, r); } // c はすでに新しくしてある void set_rec(np c, ll l, ll r, ll i, const X &x) { if (r - l == 1) { c->x = x; c->lazy = MA::id(); return; } ll m = (l + r) / 2; bool pushed = push(c, l, m, r); if (i < m) { if (!pushed) c->l = own(c->l, l, m); set_rec(c->l, l, m, i, x); } else { if (!pushed) c->r = own(c->r, m, r); set_rec(c->r, m, r, i, x); } pull(c, l, m, r); } void multiply_rec(np c, ll l, ll r, ll i, const X &x) { if (r - l == 1) { c->x = MX::op(c->x, x); c->lazy = MA::id(); return; } ll m = (l + r) / 2; bool pushed = push(c, l, m, r); if (i < m) { if (!pushed) c->l = own(c->l, l, m); multiply_rec(c->l, l, m, i, x); } else { if (!pushed) c->r = own(c->r, m, r); multiply_rec(c->r, m, r, i, x); } pull(c, l, m, r); } void apply_rec(np c, ll l, ll r, ll ql, ll qr, const A &a) { if (ql <= l && r <= qr) { tag(c, a, r - l); return; } ll m = (l + r) / 2; bool pushed = push(c, l, m, r); if (ql < m) { if (!pushed) c->l = own(c->l, l, m); apply_rec(c->l, l, m, ql, qr, a); } if (m < qr) { if (!pushed) c->r = own(c->r, m, r); apply_rec(c->r, m, r, ql, qr, a); } pull(c, l, m, r); } void prod_rec(np c, ll l, ll r, ll ql, ll qr, X &x, A a) { if (!c) { ll L = max(l, ql), R = min(r, qr); x = MX::op(x, AM::act(default_prod(L, R), a, R - L)); return; } if (ql <= l && r <= qr) { x = MX::op(x, AM::act(c->x, a, r - l)); return; } ll m = (l + r) / 2; a = MA::op(c->lazy, a); if (ql < m) prod_rec(c->l, l, m, ql, qr, x, a); if (m < qr) prod_rec(c->r, m, r, ql, qr, x, a); } template ll max_right_rec(np c, const F &check, ll l, ll r, ll ql, X &x, A a) { if (r <= ql) return r; if (ql <= l) { X y = MX::op(x, AM::act(value(c, l, r), a, r - l)); if (check(y)) { x = y; return r; } } if (r - l == 1) return l; ll m = (l + r) / 2; if (c) a = MA::op(c->lazy, a); if (ql < m) { ll k = max_right_rec(c ? c->l : nullptr, check, l, m, ql, x, a); if (k < m) return k; } return max_right_rec(c ? c->r : nullptr, check, m, r, ql, x, a); } template ll min_left_rec(np c, const F &check, ll l, ll r, ll qr, X &x, A a) { if (qr <= l) return l; if (r <= qr) { X y = MX::op(AM::act(value(c, l, r), a, r - l), x); if (check(y)) { x = y; return l; } } if (r - l == 1) return r; ll m = (l + r) / 2; if (c) a = MA::op(c->lazy, a); if (m < qr) { ll k = min_left_rec(c ? c->r : nullptr, check, m, r, qr, x, a); if (m < k) return k; } return min_left_rec(c ? c->l : nullptr, check, l, m, qr, x, a); } }; // END: ds/segtree/dynamic_lazy_segtree.hpp // BEGIN: alg/acted_monoid/sum_assign.hpp // BEGIN: alg/monoid/add.hpp #line 2 "alg/monoid/add.hpp" template struct Monoid_Add { using X = E; using value_type = X; static constexpr X op(const X &x, const X &y) noexcept { return x + y; } static constexpr X inverse(const X &x) noexcept { return -x; } static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; } static constexpr X id() { return X(0); } static constexpr bool commute = true; }; // END: alg/monoid/add.hpp // BEGIN: alg/monoid/assign.hpp #line 2 "alg/monoid/assign.hpp" template struct Monoid_Assign { static_assert(is_same_v || is_same_v); static constexpr X none_val = numeric_limits::max(); using value_type = X; static X op(X x, X y) { return (y == none_val ? x : y); } static constexpr X id() { return none_val; } static constexpr bool commute = false; }; // END: alg/monoid/assign.hpp #line 4 "alg/acted_monoid/sum_assign.hpp" template struct ActedMonoid_Sum_Assign { static_assert(is_same_v || is_same_v); using Monoid_X = Monoid_Add; using Monoid_A = Monoid_Assign; using X = typename Monoid_X::value_type; using A = typename Monoid_A::value_type; static constexpr X act(const X &x, const A &a, const ll &size) { if (a == Monoid_A::id()) return x; return a * E(size); } }; // END: alg/acted_monoid/sum_assign.hpp #line 6 "main.cpp" void solve() { /* x と x+1 が接続:dat[x] = 0 */ LL(N, Q); using AM = ActedMonoid_Sum_Assign; auto fun = [&](ll l, ll r) -> int { return r - l; }; Dynamic_Lazy_SegTree seg(0, N + 10, fun); auto root = seg.new_root(); FOR(Q) { LL(t); if (t == 1) { LL(L, R); if (L == R) continue; root = seg.apply(root, L, R, 0); } if (t == 2) { LL(L, R); if (L == R) continue; root = seg.apply(root, L, R, 1); } if (t == 3) { LL(L, R); if (L > R) swap(L, R); ll x = (L == R ? 0 : seg.prod(root, L, R)); if (x == 0) print(1); else print(0); } if (t == 4) { LL(v); ll r = seg.max_right(root, [&](auto e) -> bool { return e == 0; }, v); ll l = seg.min_left(root, [&](auto e) -> bool { return e == 0; }, v); print(r - l + 1); } } } signed main() { int T = 1; // INT(T); FOR(T) solve(); return 0; }// END: main.cpp