This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: STANDALONE
#include <iostream>
#include <random>
#include <tuple>
#include <utility>
#include <vector>
#include "tools/assert_that.hpp"
#include "tools/inversion_number.hpp"
#include "tools/lazy_sparse_segtree.hpp"
struct SM {
using T = std::tuple<int, int, int>;
static T op(const T& x, const T& y) {
return {std::get<0>(x) + std::get<0>(y), std::get<1>(x) + std::get<1>(y), std::get<2>(x) + std::get<2>(y) + std::get<1>(x) * std::get<0>(y)};
}
static T e() {
return {0, 0, 0};
}
};
using S = typename SM::T;
struct FM {
using T = bool;
static T op(const T f, const T g) {
return f ^ g;
}
static T e() {
return false;
}
};
using F = typename FM::T;
S mapping(const F f, const S& e) {
return f ? std::make_tuple(std::get<1>(e), std::get<0>(e), std::get<0>(e) * std::get<1>(e) - std::get<2>(e)) : e;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
std::uniform_int_distribution<int> t_dist(0, 2);
for (int N = 0; N < 50; ++N) {
std::vector<int> a(N, 0);
tools::lazy_sparse_segtree<SM, FM, mapping> seg(0, N, {1, 0, 0});
std::vector<std::pair<int, int>> lr;
for (int l = 0; l <= N; ++l) {
for (int r = l; r <= N; ++r) {
lr.emplace_back(l, r);
}
}
std::uniform_int_distribution<int> p_dist(0, N);
std::uniform_int_distribution<int> lr_dist(0, (N + 1) * (N + 2) / 2 - 1);
for (int q = 0; q < (N + 1) * (N + 2) * 3; ++q) {
const auto t = t_dist(engine);
if (t == 0) {
const auto [l, r] = lr[lr_dist(engine)];
for (int i = l; i < r; ++i) a[i] ^= 1;
seg.apply(l, r, true);
} else if (t == 1) {
const auto l = p_dist(engine);
const auto x = std::uniform_int_distribution<int>(0, (N - l) * (N - l - 1) / 2)(engine);
auto ok = l;
auto ng = N + 1;
while (ng - ok > 1) {
const auto mid = (ok + ng) / 2;
if (tools::inversion_number(std::ranges::subrange(a.begin() + l, a.begin() + mid)) <= x) {
ok = mid;
} else {
ng = mid;
}
}
assert_that(seg.max_right(l, [&](const S& e) { return std::get<2>(e) <= x; }) == ok);
} else {
const auto r = p_dist(engine);
const auto x = std::uniform_int_distribution<int>(0, r * (r - 1) / 2)(engine);
auto ok = r;
auto ng = -1;
while (ok - ng > 1) {
const auto mid = (ok + ng) / 2;
if (tools::inversion_number(std::ranges::subrange(a.begin() + mid, a.begin() + r)) <= x) {
ok = mid;
} else {
ng = mid;
}
}
assert_that(seg.min_left(r, [&](const S& e) { return std::get<2>(e) <= x; }) == ok);
}
}
}
return 0;
}
#line 1 "tests/lazy_sparse_segtree/binary_search.test.cpp"
// competitive-verifier: STANDALONE
#include <iostream>
#include <random>
#include <tuple>
#include <utility>
#include <vector>
#line 1 "tools/assert_that.hpp"
#line 5 "tools/assert_that.hpp"
#include <cstdlib>
#define assert_that_impl(cond, file, line, func) do {\
if (!cond) {\
::std::cerr << file << ':' << line << ": " << func << ": Assertion `" << #cond << "' failed." << '\n';\
::std::exit(EXIT_FAILURE);\
}\
} while (false)
#define assert_that(...) assert_that_impl((__VA_ARGS__), __FILE__, __LINE__, __func__)
#line 1 "tools/inversion_number.hpp"
#include <algorithm>
#include <iterator>
#include <ranges>
#line 1 "lib/ac-library/atcoder/fenwicktree.hpp"
#include <cassert>
#line 6 "lib/ac-library/atcoder/fenwicktree.hpp"
#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"
#line 5 "lib/ac-library/atcoder/internal_type_traits.hpp"
#include <numeric>
#include <type_traits>
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#line 8 "lib/ac-library/atcoder/fenwicktree.hpp"
namespace atcoder {
// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
using U = internal::to_unsigned_t<T>;
public:
fenwick_tree() : _n(0) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}
void add(int p, T x) {
assert(0 <= p && p < _n);
p++;
while (p <= _n) {
data[p - 1] += U(x);
p += p & -p;
}
}
T sum(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
return sum(r) - sum(l);
}
private:
int _n;
std::vector<U> data;
U sum(int r) {
U s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
} // namespace atcoder
#line 1 "tools/compress.hpp"
#line 1 "tools/lower_bound.hpp"
#line 6 "tools/lower_bound.hpp"
namespace tools {
template <class ForwardIterator, class T>
auto lower_bound(ForwardIterator first, ForwardIterator last, const T& value) {
return ::std::distance(first, ::std::lower_bound(first, last, value));
}
template <class ForwardIterator, class T, class Compare>
auto lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) {
return ::std::distance(first, ::std::lower_bound(first, last, value, comp));
}
}
#line 8 "tools/compress.hpp"
namespace tools {
template <::std::ranges::range R, typename OutputIterator>
void compress(R&& a, OutputIterator result) {
using T = typename ::std::ranges::range_value_t<R>;
if constexpr (::std::ranges::forward_range<R>) {
::std::vector<T> sorted(::std::ranges::begin(a), ::std::ranges::end(a));
::std::ranges::sort(sorted);
sorted.erase(::std::unique(sorted.begin(), sorted.end()), sorted.end());
for (auto it = ::std::ranges::begin(a); it != ::std::ranges::end(a); ++it, ++result) {
*result = ::tools::lower_bound(sorted.begin(), sorted.end(), *it);
}
} else {
::tools::compress(::std::vector<T>(::std::ranges::begin(a), ::std::ranges::end(a)), result);
}
}
}
#line 10 "tools/inversion_number.hpp"
namespace tools {
template <::std::ranges::range R>
long long inversion_number(R&& a) {
::std::vector<int> compressed;
::tools::compress(a, ::std::back_inserter(compressed));
if (compressed.empty()) return 0;
const auto max = *::std::ranges::max_element(compressed);
::atcoder::fenwick_tree<int> fw(max + 1);
long long res = 0;
for (const auto x : compressed) {
res += fw.sum(x + 1, max + 1);
fw.add(x, 1);
}
return res;
}
}
#line 1 "tools/lazy_sparse_segtree.hpp"
#line 5 "tools/lazy_sparse_segtree.hpp"
#include <array>
#line 7 "tools/lazy_sparse_segtree.hpp"
#include <functional>
#line 11 "tools/lazy_sparse_segtree.hpp"
#include <variant>
#line 1 "tools/ceil_log2.hpp"
#line 1 "tools/bit_width.hpp"
#include <bit>
#line 1 "tools/is_integral.hpp"
#line 5 "tools/is_integral.hpp"
namespace tools {
template <typename T>
struct is_integral : ::std::is_integral<T> {};
template <typename T>
inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}
#line 1 "tools/is_signed.hpp"
#line 5 "tools/is_signed.hpp"
namespace tools {
template <typename T>
struct is_signed : ::std::is_signed<T> {};
template <typename T>
inline constexpr bool is_signed_v = ::tools::is_signed<T>::value;
}
#line 1 "tools/make_unsigned.hpp"
#line 5 "tools/make_unsigned.hpp"
namespace tools {
template <typename T>
struct make_unsigned : ::std::make_unsigned<T> {};
template <typename T>
using make_unsigned_t = typename ::tools::make_unsigned<T>::type;
}
#line 10 "tools/bit_width.hpp"
namespace tools {
template <typename T>
constexpr int bit_width(T) noexcept;
template <typename T>
constexpr int bit_width(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_width<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_width(x);
}
}
}
#line 6 "tools/ceil_log2.hpp"
namespace tools {
template <typename T>
constexpr T ceil_log2(T x) noexcept {
assert(x > 0);
return ::tools::bit_width(x - 1);
}
}
#line 1 "tools/fix.hpp"
#line 6 "tools/fix.hpp"
namespace tools {
template <typename F>
struct fix : F {
template <typename G>
fix(G&& g) : F({::std::forward<G>(g)}) {
}
template <typename... Args>
decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, ::std::forward<Args>(args)...);
}
};
template <typename F>
fix(F&&) -> fix<::std::decay_t<F>>;
}
#line 1 "tools/nop_monoid.hpp"
#line 5 "tools/nop_monoid.hpp"
namespace tools {
struct nop_monoid {
using T = ::std::monostate;
static T op(T, T) {
return {};
}
static T e() {
return {};
}
};
}
#line 16 "tools/lazy_sparse_segtree.hpp"
namespace tools {
template <typename SM, typename FM, auto mapping>
class lazy_sparse_segtree {
using S = typename SM::T;
using F = typename FM::T;
static_assert(
::std::is_convertible_v<decltype(mapping), ::std::function<S(F, S)>>,
"mapping must work as S(F, S)");
template <typename T>
static constexpr bool has_data = !::std::is_same_v<T, ::tools::nop_monoid>;
template <typename T>
static constexpr bool has_lazy = !::std::is_same_v<T, ::tools::nop_monoid>;
struct regular_node {
S data;
::std::array<int, 2> children;
};
struct dual_node {
F lazy;
::std::array<int, 2> children;
};
struct lazy_node {
S data;
F lazy;
::std::array<int, 2> children;
};
using node = ::std::conditional_t<has_data<SM>, ::std::conditional_t<has_lazy<FM>, lazy_node, regular_node>, dual_node>;
::std::vector<node> m_nodes;
long long m_offset;
long long m_size;
int m_height;
int m_root;
long long capacity() const {
return 1LL << this->m_height;
}
bool is_mutable(const int k) const {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
return this->m_height < k;
}
void make_mutable() {
if (this->is_mutable(this->m_root)) return;
this->m_nodes.push_back(this->m_nodes[this->m_root]);
this->m_root = this->m_nodes.size() - 1;
}
void make_mutable(const int k, const int d) {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
assert(0 <= d && d < 2);
assert(!this->is_leaf(k));
if (this->is_mutable(this->m_nodes[k].children[d])) return;
this->m_nodes.push_back(this->m_nodes[this->m_nodes[k].children[d]]);
this->m_nodes[k].children[d] = this->m_nodes.size() - 1;
}
bool is_leaf(const int k) const {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
const auto& node = this->m_nodes[k];
return node.children[0] < 0 && node.children[1] < 0;
}
template <typename SFINAE = FM> requires (has_lazy<SFINAE>)
void push(const int k) {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
assert(this->is_mutable(k));
assert(!this->is_leaf(k));
auto& node = this->m_nodes[k];
this->all_apply(node.children[0], node.lazy);
this->all_apply(node.children[1], node.lazy);
node.lazy = FM::e();
}
template <typename SFINAE = FM> requires (has_lazy<SFINAE>)
void all_apply(const int k, const F& f) {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
assert(this->is_mutable(k));
auto& node = this->m_nodes[k];
if constexpr (has_data<SM>) {
node.data = mapping(f, node.data);
}
node.lazy = FM::op(f, node.lazy);
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
void update(const int k) {
assert(0 <= k && ::std::cmp_less(k, this->m_nodes.size()));
assert(this->is_mutable(k));
assert(!this->is_leaf(k));
auto& node = this->m_nodes[k];
node.data = SM::op(this->m_nodes[node.children[0]].data, this->m_nodes[node.children[1]].data);
}
public:
lazy_sparse_segtree() = default;
template <typename SFINAE = SM> requires (has_data<SFINAE>)
lazy_sparse_segtree(const long long l_star, const long long r_star) : lazy_sparse_segtree(l_star, r_star, SM::e()) {
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
lazy_sparse_segtree(const long long l_star, const long long r_star, const S& x) :
m_offset(l_star), m_size(r_star - l_star), m_height(::tools::ceil_log2(::std::max(1LL, r_star - l_star))) {
assert(l_star <= r_star);
if constexpr (has_lazy<FM>) {
this->m_nodes.push_back({x, FM::e(), {-1, -1}});
for (int k = 1; k <= this->m_height; ++k) {
this->m_nodes.push_back({SM::op(this->m_nodes.back().data, this->m_nodes.back().data), FM::e(), {k - 1, k - 1}});
}
} else {
this->m_nodes.push_back({x, {-1, -1}});
for (int k = 1; k <= this->m_height; ++k) {
this->m_nodes.push_back({SM::op(this->m_nodes.back().data, this->m_nodes.back().data), {k - 1, k - 1}});
}
}
this->m_root = this->m_height;
}
template <typename SFINAE = SM> requires (!has_data<SFINAE>)
lazy_sparse_segtree(const long long l_star, const long long r_star) :
m_offset(l_star), m_size(r_star - l_star), m_height(::tools::ceil_log2(::std::max(1LL, r_star - l_star))) {
assert(l_star <= r_star);
this->m_nodes.push_back({FM::e(), {-1, -1}});
for (int k = 1; k <= this->m_height; ++k) {
this->m_nodes.push_back({FM::e(), {k - 1, k - 1}});
}
this->m_root = this->m_height;
}
long long lower_bound() const {
return this->m_offset;
}
long long upper_bound() const {
return this->m_offset + this->m_size;
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
void set(long long p, const S& x) {
assert(this->lower_bound() <= p && p < this->upper_bound());
p -= this->m_offset;
this->make_mutable();
::tools::fix([&](auto&& dfs, const int h, const int k) -> void {
assert(this->is_mutable(k));
if (h > 0) {
assert(!this->is_leaf(k));
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
} else {
this->make_mutable(k, ((this->capacity() + p) >> (h - 1)) & 1);
}
dfs(h - 1, this->m_nodes[k].children[((this->capacity() + p) >> (h - 1)) & 1]);
this->update(k);
} else {
assert(this->is_leaf(k));
this->m_nodes[k].data = x;
}
})(this->m_height, this->m_root);
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
S get(const long long p) {
return this->prod(p, p + 1);
}
template <typename SFINAE = SM> requires (!has_data<SFINAE>)
F get(long long p) {
assert(this->lower_bound() <= p && p < this->upper_bound());
p -= this->m_offset;
this->make_mutable();
return ::tools::fix([&](auto&& dfs, const int h, const int k) -> F {
assert(this->is_mutable(k));
if (h > 0) {
assert(!this->is_leaf(k));
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
return dfs(h - 1, this->m_nodes[k].children[((this->capacity() + p) >> (h - 1)) & 1]);
} else {
assert(this->is_leaf(k));
return this->m_nodes[k].lazy;
}
})(this->m_height, this->m_root);
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
S prod(long long l, long long r) {
assert(this->lower_bound() <= l && l <= r && r <= this->upper_bound());
if (l == r) return SM::e();
l -= this->m_offset;
r -= this->m_offset;
if constexpr (has_lazy<FM>) {
this->make_mutable();
}
return ::tools::fix([&](auto&& dfs, const int k, const long long kl, const long long kr) -> S {
assert(kl < kr);
if (l <= kl && kr <= r) return this->m_nodes[k].data;
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
}
const auto km = ::std::midpoint(kl, kr);
S res = SM::e();
if (l < km) res = SM::op(res, dfs(this->m_nodes[k].children[0], kl, km));
if (km < r) res = SM::op(res, dfs(this->m_nodes[k].children[1], km, kr));
return res;
})(this->m_root, 0, this->capacity());
}
template <typename SFINAE = SM> requires (has_data<SFINAE>)
S all_prod() const {
return this->m_nodes[this->m_root].data;
}
template <typename SFINAE = FM> requires (has_lazy<SFINAE>)
void apply(const long long p, const F& f) {
this->apply(p, p + 1, f);
}
template <typename SFINAE = FM> requires (has_lazy<SFINAE>)
void apply(long long l, long long r, const F& f) {
assert(this->lower_bound() <= l && l <= r && r <= this->upper_bound());
if (l == r) return;
l -= this->m_offset;
r -= this->m_offset;
this->make_mutable();
::tools::fix([&](auto&& dfs, const int k, const long long kl, const long long kr) -> void {
assert(kl < kr);
if (l <= kl && kr <= r) {
this->all_apply(k, f);
return;
}
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
const auto km = ::std::midpoint(kl, kr);
if (l < km) dfs(this->m_nodes[k].children[0], kl, km);
if (km < r) dfs(this->m_nodes[k].children[1], km, kr);
if constexpr (has_data<SM>) {
this->update(k);
}
})(this->m_root, 0, this->capacity());
}
template <typename G, typename SFINAE = SM> requires (has_data<SFINAE>)
long long max_right(long long l, const G& g) {
assert(this->lower_bound() <= l && l <= this->upper_bound());
assert(g(SM::e()));
if (l == this->upper_bound()) return l;
l -= this->m_offset;
if constexpr (has_lazy<FM>) {
this->make_mutable();
}
return this->m_offset + ::std::min(::tools::fix([&](auto&& dfs, const S& c, const int k, const long long kl, const long long kr) -> ::std::pair<S, long long> {
assert(kl < kr);
if (kl < l) {
assert(kl < l && l < kr);
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
}
const auto km = ::std::midpoint(kl, kr);
if (l < km) {
const auto [hc, hr] = dfs(c, this->m_nodes[k].children[0], kl, km);
assert(l <= hr && hr <= km);
if (hr < km) return {hc, hr};
return dfs(hc, this->m_nodes[k].children[1], km, kr);
} else {
return dfs(c, this->m_nodes[k].children[1], km, kr);
}
} else {
if (const auto wc = SM::op(c, this->m_nodes[k].data); g(wc)) return {wc, kr};
if (kr - kl == 1) return {c, kl};
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
}
const auto km = ::std::midpoint(kl, kr);
const auto [hc, hr] = dfs(c, this->m_nodes[k].children[0], kl, km);
assert(l <= hr && hr <= km);
if (hr < km) return {hc, hr};
return dfs(hc, this->m_nodes[k].children[1], km, kr);
}
})(SM::e(), this->m_root, 0, this->capacity()).second, this->m_size);
}
template <typename G, typename SFINAE = SM> requires (has_data<SFINAE>)
long long min_left(long long r, const G& g) {
assert(this->lower_bound() <= r && r <= this->upper_bound());
assert(g(SM::e()));
if (r == this->lower_bound()) return r;
r -= this->m_offset;
if constexpr (has_lazy<FM>) {
this->make_mutable();
}
return this->m_offset + ::tools::fix([&](auto&& dfs, const S& c, const int k, const long long kl, const long long kr) -> ::std::pair<S, long long> {
assert(kl < kr);
if (r < kr) {
assert(kl < r && r < kr);
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
}
const auto km = ::std::midpoint(kl, kr);
if (km < r) {
const auto [hc, hl] = dfs(c, this->m_nodes[k].children[1], km, kr);
assert(km <= hl && hl <= r);
if (km < hl) return {hc, hl};
return dfs(hc, this->m_nodes[k].children[0], kl, km);
} else {
return dfs(c, this->m_nodes[k].children[0], kl, km);
}
} else {
if (const auto wc = SM::op(this->m_nodes[k].data, c); g(wc)) return {wc, kl};
if (kr - kl == 1) return {c, kr};
if constexpr (has_lazy<FM>) {
this->make_mutable(k, 0);
this->make_mutable(k, 1);
this->push(k);
}
const auto km = ::std::midpoint(kl, kr);
const auto [hc, hl] = dfs(c, this->m_nodes[k].children[1], km, kr);
assert(km <= hl && hl <= r);
if (km < hl) return {hc, hl};
return dfs(hc, this->m_nodes[k].children[0], kl, km);
}
})(SM::e(), this->m_root, 0, this->capacity()).second;
}
};
}
#line 11 "tests/lazy_sparse_segtree/binary_search.test.cpp"
struct SM {
using T = std::tuple<int, int, int>;
static T op(const T& x, const T& y) {
return {std::get<0>(x) + std::get<0>(y), std::get<1>(x) + std::get<1>(y), std::get<2>(x) + std::get<2>(y) + std::get<1>(x) * std::get<0>(y)};
}
static T e() {
return {0, 0, 0};
}
};
using S = typename SM::T;
struct FM {
using T = bool;
static T op(const T f, const T g) {
return f ^ g;
}
static T e() {
return false;
}
};
using F = typename FM::T;
S mapping(const F f, const S& e) {
return f ? std::make_tuple(std::get<1>(e), std::get<0>(e), std::get<0>(e) * std::get<1>(e) - std::get<2>(e)) : e;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
std::uniform_int_distribution<int> t_dist(0, 2);
for (int N = 0; N < 50; ++N) {
std::vector<int> a(N, 0);
tools::lazy_sparse_segtree<SM, FM, mapping> seg(0, N, {1, 0, 0});
std::vector<std::pair<int, int>> lr;
for (int l = 0; l <= N; ++l) {
for (int r = l; r <= N; ++r) {
lr.emplace_back(l, r);
}
}
std::uniform_int_distribution<int> p_dist(0, N);
std::uniform_int_distribution<int> lr_dist(0, (N + 1) * (N + 2) / 2 - 1);
for (int q = 0; q < (N + 1) * (N + 2) * 3; ++q) {
const auto t = t_dist(engine);
if (t == 0) {
const auto [l, r] = lr[lr_dist(engine)];
for (int i = l; i < r; ++i) a[i] ^= 1;
seg.apply(l, r, true);
} else if (t == 1) {
const auto l = p_dist(engine);
const auto x = std::uniform_int_distribution<int>(0, (N - l) * (N - l - 1) / 2)(engine);
auto ok = l;
auto ng = N + 1;
while (ng - ok > 1) {
const auto mid = (ok + ng) / 2;
if (tools::inversion_number(std::ranges::subrange(a.begin() + l, a.begin() + mid)) <= x) {
ok = mid;
} else {
ng = mid;
}
}
assert_that(seg.max_right(l, [&](const S& e) { return std::get<2>(e) <= x; }) == ok);
} else {
const auto r = p_dist(engine);
const auto x = std::uniform_int_distribution<int>(0, r * (r - 1) / 2)(engine);
auto ok = r;
auto ng = -1;
while (ok - ng > 1) {
const auto mid = (ok + ng) / 2;
if (tools::inversion_number(std::ranges::subrange(a.begin() + mid, a.begin() + r)) <= x) {
ok = mid;
} else {
ng = mid;
}
}
assert_that(seg.min_left(r, [&](const S& e) { return std::get<2>(e) <= x; }) == ok);
}
}
}
return 0;
}