#pragma once
#include "../../../util/alias/others/lib.hpp"
#include "../../../util/traits/math/lib.hpp"
namespace tifa_libs {
//! 0-based
template <class T, class TT = T>
requires(sizeof(T) <= sizeof(TT))
struct segbeats_ca_ms {
static CEXP T INF = inf_v<T>;
struct TIFA {
TT sum = 0;
T max = 0, min = 0, max2 = -INF, min2 = INF;
u32 cmax = 1, cmin = 1;
T add = 0;
};
vec<TIFA> v;
u32 n, lbn;
CEXP segbeats_ca_ms() = default;
CEXPE segbeats_ca_ms(u32 _n) NE : segbeats_ca_ms(vec<T>(_n)) {}
CEXPE segbeats_ca_ms(spn<T> a) NE { reset(a); }
CEXP void reset(spn<T> a) NE {
if (a.empty()) {
n = lbn = 0, v.clear();
return;
}
lbn = (u32)std::bit_width(a.size() - 1), n = 1_u32 << lbn, v.resize(2 * n);
flt_ (u32, i, 0, (u32)a.size()) v[i + n].sum = v[i + n].max = v[i + n].min = a[i];
for (u32 i = n - 1; i; --i) pushup(i);
}
CEXP void chmin(u32 l, u32 r, T x) NE { update_<1>(l, r, x); }
CEXP void chmax(u32 l, u32 r, T x) NE { update_<2>(l, r, x); }
CEXP void add(u32 l, u32 r, T x) NE { update_<3>(l, r, x); }
CEXP void set(u32 l, u32 r, T x) NE { update_<4>(l, r, x); }
CEXP T query_min(u32 l, u32 r) NE { return query_<1>(l, r); }
CEXP T query_max(u32 l, u32 r) NE { return query_<2>(l, r); }
CEXP TT query_sum(u32 l, u32 r) NE { return query_<3>(l, r); }
private:
template <int tp>
CEXP auto e() NE {
if CEXP (tp == 1) return INF;
if CEXP (tp == 2) return -INF;
if CEXP (tp == 3) return (TT)0;
}
template <int tp>
CEXP void op(auto& a, TIFA CR b) NE {
if CEXP (tp == 1) a = min(a, b.min);
if CEXP (tp == 2) a = max(a, b.max);
if CEXP (tp == 3) a += b.sum;
}
template <int tp>
CEXP void apply_(u32 k, T x) NE {
if CEXP (tp == 1) chmins_(k, x);
if CEXP (tp == 2) chmaxs_(k, x);
if CEXP (tp == 3) pdadd_(k, x);
if CEXP (tp == 4) chmins_(k, x), chmaxs_(k, x);
}
CEXP void chmins_(u32 k, T x) NE {
if (v[k].max <= x) return;
if (v[k].max2 < x) return pdmin_(k, x);
pushdown(k), chmins_(k * 2, x), chmins_(k * 2 + 1, x), pushup(k);
}
CEXP void chmaxs_(u32 k, T x) NE {
if (x <= v[k].min) return;
if (x < v[k].min2) return pdmax_(k, x);
pushdown(k), chmaxs_(k * 2, x), chmaxs_(k * 2 + 1, x), pushup(k);
}
CEXP void pushup(u32 k) NE {
TIFA &p = v[k], &l = v[k * 2], &r = v[k * 2 + 1];
p.sum = l.sum + r.sum;
if (l.max == r.max) {
p.max = l.max;
p.max2 = max(l.max2, r.max2);
p.cmax = l.cmax + r.cmax;
} else {
bool f = l.max > r.max;
p.max = f ? l.max : r.max;
p.cmax = f ? l.cmax : r.cmax;
p.max2 = max(f ? r.max : l.max, f ? l.max2 : r.max2);
}
if (l.min == r.min) {
p.min = l.min;
p.min2 = min(l.min2, r.min2);
p.cmin = l.cmin + r.cmin;
} else {
bool f = l.min < r.min;
p.min = f ? l.min : r.min;
p.cmin = f ? l.cmin : r.cmin;
p.min2 = min(f ? r.min : l.min, f ? l.min2 : r.min2);
}
}
CEXP void pushdown(u32 k) NE {
TIFA& p = v[k];
if (p.add) pdadd_(k * 2, p.add), pdadd_(k * 2 + 1, p.add), p.add = 0;
if (p.max < v[k * 2].max) pdmin_(k * 2, p.max);
if (p.min > v[k * 2].min) pdmax_(k * 2, p.min);
if (p.max < v[k * 2 + 1].max) pdmin_(k * 2 + 1, p.max);
if (p.min > v[k * 2 + 1].min) pdmax_(k * 2 + 1, p.min);
}
CEXP void pdadd_(u32 k, T x) NE {
TIFA& p = v[k];
p.sum += (TT)x << ((int)lbn + std::countl_zero(k) - 31);
p.max += x, p.min += x;
if (p.max2 != -INF) p.max2 += x;
if (p.min2 != INF) p.min2 += x;
p.add += x;
}
void pdmin_(u32 k, T x) NE {
TIFA& p = v[k];
if (p.sum += ((TT)x - p.max) * p.cmax; p.min == p.max) p.min = x;
if (p.min2 == p.max) p.min2 = x;
p.max = x;
}
void pdmax_(u32 k, T x) NE {
TIFA& p = v[k];
if (p.sum += ((TT)x - p.min) * p.cmin; p.max == p.min) p.max = x;
if (p.max2 == p.min) p.max2 = x;
p.min = x;
}
template <int tp>
CEXP void update_(u32 l, u32 r, T x) NE {
if (assert(l <= r); l == r) return;
l += n, r += n;
cu32 zl = (u32)std::countr_zero(l), zr = (u32)std::countr_zero(r), zm = min(zl, zr);
for (u32 i = lbn; i >= 1; i--) {
if (((l >> i) << i) != l) pushdown(l >> i);
if (((r >> i) << i) != r) pushdown((r - 1) >> i);
}
u32 l2 = l, r2 = r;
while (l2 < r2) {
if (l2 & 1) apply_<tp>(l2++, x);
if (r2 & 1) apply_<tp>(--r2, x);
l2 /= 2, r2 /= 2;
}
flt_ (u32, i, zm + 1, lbn + 1) {
if (zl < i) pushup(l >> i);
if (zr < i) pushup((r - 1) >> i);
}
}
template <int tp>
CEXP auto query_(u32 l, u32 r) NE {
if (l == r) return e<tp>();
l += n, r += n;
cu32 zl = (u32)std::countr_zero(l), zr = (u32)std::countr_zero(r), ie = (u32)max(1, (i32)min(zl, zr));
for (u32 i = lbn; i >= ie; --i) {
if (zl < i) pushdown(l >> i);
if (zr < i) pushdown((r - 1) >> i);
}
auto ql = e<tp>(), qr = e<tp>();
while (l < r) {
if (l & 1) op<tp>(ql, v[l++]);
if (r & 1) op<tp>(qr, v[--r]);
l /= 2, r /= 2;
}
if CEXP (tp == 1) return min(ql, qr);
if CEXP (tp == 2) return max(ql, qr);
if CEXP (tp == 3) return ql + qr;
}
};
} // namespace tifa_libs
#line 2 "src/ds/segtree/segbeats_ca_ms/lib.hpp"
#line 2 "src/util/alias/others/lib.hpp"
#line 2 "src/util/consts/lib.hpp"
#line 2 "src/util/alias/num/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();
inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/others/lib.hpp"
namespace tifa_libs {
template <class T>
struct chash {
CEXP static u64 C = u64(pi_v<f128> * 2e18) | 71;
CEXP u64 operator()(T x) CNE { return __builtin_bswap64(((u64)x ^ TIME) * C); }
};
// clang-format off
#define mk_(w, t) using w = t; using c##w = const t;
mk_(strn, std::string) mk_(strnv, std::string_view)
#undef mk_
template <class T> struct edge_t { T w; u32 u, v; CEXP auto operator<=>(edge_t CR) const = default; }; template <class T> using cedge_t = const edge_t<T>;
template <class T> struct pt3 { T _0, _1, _2; CEXP auto operator<=>(pt3 CR) const = default; }; template <class T> using cpt3 = const pt3<T>;
template <class T> struct pt4 { T _0, _1, _2, _3; CEXP auto operator<=>(pt4 CR) const = default; }; template <class T> using cpt4 = const pt4<T>;
#define mkT_(w, t, ...) template <class T> using w = t __VA_OPT__(, ) __VA_ARGS__; template <class T> using c##w = const t __VA_OPT__(, ) __VA_ARGS__;
mkT_(ptt, std::pair<T, T>) mkT_(alc, std::pmr::polymorphic_allocator<T>) mkT_(vec, std::vector<T>) mkT_(vvec, vec<vec<T>>) mkT_(v3ec, vvec<vec<T>>) mkT_(vecpt, vec<ptt<T>>) mkT_(vvecpt, vvec<ptt<T>>) mkT_(ptvec, ptt<vec<T>>) mkT_(ptvvec, ptt<vvec<T>>)
#undef mkT_
template <class T> using itl = std ::initializer_list<T>;
template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>; template <class T, usz N> using carr = std::array<const T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>; template <class U, class T> using vvecp = vvec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>; template <class U, class T> using vvvecp = vvec<vvec<std::pair<U, T>>>;
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class T, class C = std::less<T>> using set = __gnu_pbds::tree<T, __gnu_pbds::null_type, C>;
template <class K, class V, class C = std::less<K>> using map = __gnu_pbds::tree<K, V, C>;
// hset<u64> s({}, {}, {}, {}, {1<<16});
template <class T, class HF = chash<T>> using hset = __gnu_pbds::gp_hash_table<T, __gnu_pbds::null_type, HF>;
// hmap<u64, int> s({}, {}, {}, {}, {1<<16});
template <class K, class V, class HF = chash<K>> using hmap = __gnu_pbds::gp_hash_table<K, V, HF>;
#else
using std::set, std::map;
template <class T, class HF = chash<T>> using hset = std::unordered_set<T, HF>;
template <class K, class V, class HF = chash<K>> using hmap = std::unordered_map<K, V, HF>;
#endif
#ifdef PB_DS_PRIORITY_QUEUE_HPP
template <class T, class C = std::less<T>> using pq = __gnu_pbds::priority_queue<T, C>;
#else
template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
#endif
template <class T> using pqg = pq<T, std::greater<T>>;
// clang-format on
#define mk1_(V, A, T) using V##A = V<T>;
#define mk_(V, A, T) mk1_(V, A, T) mk1_(c##V, A, T)
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk1_(spn, A, T) mk1_(itl, A, T)
mk(b, bool) mk(c, chr) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64) mk(t, isz) mk(z, usz) mk(f, f32) mk(d, f64) mk(s, strn);
#undef mk
#undef mk_
#undef mk1_
} // namespace tifa_libs
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#line 4 "src/util/traits/math/lib.hpp"
namespace tifa_libs {
template <class T> concept char_c = std::same_as<T, char> || std::same_as<T, signed char> || std::same_as<T, unsigned char>;
#pragma GCC diagnostic ignored "-Wpedantic"
template <class T> concept s128_c = std::same_as<T, __int128_t> || std::same_as<T, __int128>;
template <class T> concept u128_c = std::same_as<T, __uint128_t> || std::same_as<T, unsigned __int128>;
template <class T> concept i128_c = s128_c<T> || u128_c<T>;
#pragma GCC diagnostic warning "-Wpedantic"
template <class T> concept imost64_c = std::integral<T> && sizeof(T) * __CHAR_BIT__ <= 64;
template <class T> concept smost64_c = imost64_c<T> && std::signed_integral<T>;
template <class T> concept umost64_c = imost64_c<T> && std::unsigned_integral<T>;
template <class T> concept int_c = i128_c<T> || imost64_c<T>;
template <class T> concept sint_c = s128_c<T> || smost64_c<T>;
template <class T> concept uint_c = u128_c<T> || umost64_c<T>;
template <class T> concept arithm_c = std::is_arithmetic_v<T> || int_c<T>;
template <class T> concept mint_c = requires(T x) { {x.mod()} -> uint_c; {x.val()} -> uint_c; };
template <class T> concept dft_c = requires(T x, std::vector<TPN T::data_t> v, u32 n) { {x.size()} -> std::same_as<u32>; x.bzr(n); x.dif(v, n); x.dit(v, n); };
template <class T> concept ntt_c = dft_c<T> && requires(T x) { T::max_size; T::G; };
template <class T> struct to_sint : std::make_signed<T> {};
template <> struct to_sint<u128> { using type = i128; };
template <> struct to_sint<i128> { using type = i128; };
template <class T> using to_sint_t = TPN to_sint<T>::type;
template <class T> struct to_uint : std::make_unsigned<T> {};
template <> struct to_uint<u128> { using type = u128; };
template <> struct to_uint<i128> { using type = u128; };
template <class T> using to_uint_t = TPN to_uint<T>::type;
template <arithm_c T> struct to_bigger : std::make_unsigned<T> {};
#define _(w,ww) template <> struct to_bigger<w> { using type = ww; }
#define _2(w,ww) _(i##w,i##ww); _(u##w,u##ww);
_2(8, 16); _2(16, 32); _2(32, 64); _2(64, 128); _(f32, f64); _(f64, f128);
#undef _2
#undef _
template <class T> using to_bigger_t = TPN to_bigger<T>::type;
template <arithm_c T> CEXP T inf_v = [] {
if CEXP(sint_c<T>) return T(to_uint_t<T>(-1) / 4 - 1);
else if CEXP(uint_c<T>) return T(-1) / 2 - 1;
else return std::numeric_limits<T>::max() / 2 - 1;
}();
} // namespace tifa_libs
// clang-format on
#line 5 "src/ds/segtree/segbeats_ca_ms/lib.hpp"
namespace tifa_libs {
//! 0-based
template <class T, class TT = T>
requires(sizeof(T) <= sizeof(TT))
struct segbeats_ca_ms {
static CEXP T INF = inf_v<T>;
struct TIFA {
TT sum = 0;
T max = 0, min = 0, max2 = -INF, min2 = INF;
u32 cmax = 1, cmin = 1;
T add = 0;
};
vec<TIFA> v;
u32 n, lbn;
CEXP segbeats_ca_ms() = default;
CEXPE segbeats_ca_ms(u32 _n) NE : segbeats_ca_ms(vec<T>(_n)) {}
CEXPE segbeats_ca_ms(spn<T> a) NE { reset(a); }
CEXP void reset(spn<T> a) NE {
if (a.empty()) {
n = lbn = 0, v.clear();
return;
}
lbn = (u32)std::bit_width(a.size() - 1), n = 1_u32 << lbn, v.resize(2 * n);
flt_ (u32, i, 0, (u32)a.size()) v[i + n].sum = v[i + n].max = v[i + n].min = a[i];
for (u32 i = n - 1; i; --i) pushup(i);
}
CEXP void chmin(u32 l, u32 r, T x) NE { update_<1>(l, r, x); }
CEXP void chmax(u32 l, u32 r, T x) NE { update_<2>(l, r, x); }
CEXP void add(u32 l, u32 r, T x) NE { update_<3>(l, r, x); }
CEXP void set(u32 l, u32 r, T x) NE { update_<4>(l, r, x); }
CEXP T query_min(u32 l, u32 r) NE { return query_<1>(l, r); }
CEXP T query_max(u32 l, u32 r) NE { return query_<2>(l, r); }
CEXP TT query_sum(u32 l, u32 r) NE { return query_<3>(l, r); }
private:
template <int tp>
CEXP auto e() NE {
if CEXP (tp == 1) return INF;
if CEXP (tp == 2) return -INF;
if CEXP (tp == 3) return (TT)0;
}
template <int tp>
CEXP void op(auto& a, TIFA CR b) NE {
if CEXP (tp == 1) a = min(a, b.min);
if CEXP (tp == 2) a = max(a, b.max);
if CEXP (tp == 3) a += b.sum;
}
template <int tp>
CEXP void apply_(u32 k, T x) NE {
if CEXP (tp == 1) chmins_(k, x);
if CEXP (tp == 2) chmaxs_(k, x);
if CEXP (tp == 3) pdadd_(k, x);
if CEXP (tp == 4) chmins_(k, x), chmaxs_(k, x);
}
CEXP void chmins_(u32 k, T x) NE {
if (v[k].max <= x) return;
if (v[k].max2 < x) return pdmin_(k, x);
pushdown(k), chmins_(k * 2, x), chmins_(k * 2 + 1, x), pushup(k);
}
CEXP void chmaxs_(u32 k, T x) NE {
if (x <= v[k].min) return;
if (x < v[k].min2) return pdmax_(k, x);
pushdown(k), chmaxs_(k * 2, x), chmaxs_(k * 2 + 1, x), pushup(k);
}
CEXP void pushup(u32 k) NE {
TIFA &p = v[k], &l = v[k * 2], &r = v[k * 2 + 1];
p.sum = l.sum + r.sum;
if (l.max == r.max) {
p.max = l.max;
p.max2 = max(l.max2, r.max2);
p.cmax = l.cmax + r.cmax;
} else {
bool f = l.max > r.max;
p.max = f ? l.max : r.max;
p.cmax = f ? l.cmax : r.cmax;
p.max2 = max(f ? r.max : l.max, f ? l.max2 : r.max2);
}
if (l.min == r.min) {
p.min = l.min;
p.min2 = min(l.min2, r.min2);
p.cmin = l.cmin + r.cmin;
} else {
bool f = l.min < r.min;
p.min = f ? l.min : r.min;
p.cmin = f ? l.cmin : r.cmin;
p.min2 = min(f ? r.min : l.min, f ? l.min2 : r.min2);
}
}
CEXP void pushdown(u32 k) NE {
TIFA& p = v[k];
if (p.add) pdadd_(k * 2, p.add), pdadd_(k * 2 + 1, p.add), p.add = 0;
if (p.max < v[k * 2].max) pdmin_(k * 2, p.max);
if (p.min > v[k * 2].min) pdmax_(k * 2, p.min);
if (p.max < v[k * 2 + 1].max) pdmin_(k * 2 + 1, p.max);
if (p.min > v[k * 2 + 1].min) pdmax_(k * 2 + 1, p.min);
}
CEXP void pdadd_(u32 k, T x) NE {
TIFA& p = v[k];
p.sum += (TT)x << ((int)lbn + std::countl_zero(k) - 31);
p.max += x, p.min += x;
if (p.max2 != -INF) p.max2 += x;
if (p.min2 != INF) p.min2 += x;
p.add += x;
}
void pdmin_(u32 k, T x) NE {
TIFA& p = v[k];
if (p.sum += ((TT)x - p.max) * p.cmax; p.min == p.max) p.min = x;
if (p.min2 == p.max) p.min2 = x;
p.max = x;
}
void pdmax_(u32 k, T x) NE {
TIFA& p = v[k];
if (p.sum += ((TT)x - p.min) * p.cmin; p.max == p.min) p.max = x;
if (p.max2 == p.min) p.max2 = x;
p.min = x;
}
template <int tp>
CEXP void update_(u32 l, u32 r, T x) NE {
if (assert(l <= r); l == r) return;
l += n, r += n;
cu32 zl = (u32)std::countr_zero(l), zr = (u32)std::countr_zero(r), zm = min(zl, zr);
for (u32 i = lbn; i >= 1; i--) {
if (((l >> i) << i) != l) pushdown(l >> i);
if (((r >> i) << i) != r) pushdown((r - 1) >> i);
}
u32 l2 = l, r2 = r;
while (l2 < r2) {
if (l2 & 1) apply_<tp>(l2++, x);
if (r2 & 1) apply_<tp>(--r2, x);
l2 /= 2, r2 /= 2;
}
flt_ (u32, i, zm + 1, lbn + 1) {
if (zl < i) pushup(l >> i);
if (zr < i) pushup((r - 1) >> i);
}
}
template <int tp>
CEXP auto query_(u32 l, u32 r) NE {
if (l == r) return e<tp>();
l += n, r += n;
cu32 zl = (u32)std::countr_zero(l), zr = (u32)std::countr_zero(r), ie = (u32)max(1, (i32)min(zl, zr));
for (u32 i = lbn; i >= ie; --i) {
if (zl < i) pushdown(l >> i);
if (zr < i) pushdown((r - 1) >> i);
}
auto ql = e<tp>(), qr = e<tp>();
while (l < r) {
if (l & 1) op<tp>(ql, v[l++]);
if (r & 1) op<tp>(qr, v[--r]);
l /= 2, r /= 2;
}
if CEXP (tp == 1) return min(ql, qr);
if CEXP (tp == 2) return max(ql, qr);
if CEXP (tp == 3) return ql + qr;
}
};
} // namespace tifa_libs