This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: STANDALONE
#include <iostream>
#include "tools/assert_that.hpp"
#include "tools/group.hpp"
#include "tools/monoid.hpp"
#include "tools/online_imos.hpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
{
tools::online_imos<int> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
a.add(0, 0, 2);
}
{
tools::online_imos<int> a(1);
assert_that(a.size() == 1);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(1, 1, 8);
a.add(1, 1, 16);
assert_that(a[0] == 4);
}
{
tools::online_imos<int> a(2);
assert_that(a.size() == 2);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(2, 2, 4);
a.apply(0, 1, 8);
a.apply(1, 2, 16);
a.apply(0, 2, 32);
assert_that(a[0] == 40);
a.apply(1, 1, 64);
a.apply(2, 2, 128);
a.apply(1, 2, 256);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
a.apply(2, 2, 512);
a.add(2, 2, 1024);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
}
{
tools::online_imos<int, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
a.add(0, 0, 2);
}
{
tools::online_imos<int, false> a(1);
assert_that(a.size() == 1);
a.apply(1, 1, 1);
a.apply(0, 0, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(0, 0, 8);
a.add(0, 0, 16);
assert_that(a[0] == 4);
}
{
tools::online_imos<int, false> a(2);
assert_that(a.size() == 2);
a.apply(2, 2, 1);
a.apply(1, 1, 2);
a.apply(0, 0, 4);
a.apply(1, 2, 8);
a.apply(0, 1, 16);
a.apply(0, 2, 32);
assert_that(a[1] == 40);
a.apply(1, 1, 64);
a.apply(0, 0, 128);
a.apply(0, 1, 256);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
a.apply(0, 0, 512);
a.add(0, 0, 1024);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
}
{
tools::online_imos<tools::monoid::max<int>> a;
assert_that(a.size() == 0);
a.apply(0, 0, 0);
}
{
tools::online_imos<tools::monoid::max<int>> a(1);
assert_that(a.size() == 1);
a.apply(0, 1, 0);
a.apply(1, 1, 1);
assert_that(a[0] == 0);
a.apply(1, 1, 2);
assert_that(a[0] == 0);
}
{
tools::online_imos<tools::monoid::max<int>> a(2);
assert_that(a.size() == 2);
a.apply(0, 2, 0);
a.apply(1, 2, 1);
a.apply(2, 2, 2);
assert_that(a[0] == 0);
a.apply(1, 2, 3);
a.apply(2, 2, 4);
assert_that(a[0] == 0);
assert_that(a[1] == 3);
a.apply(2, 2, 5);
assert_that(a[0] == 0);
assert_that(a[1] == 3);
}
{
tools::online_imos<tools::monoid::max<int>, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 0);
}
{
tools::online_imos<tools::monoid::max<int>, false> a(1);
assert_that(a.size() == 1);
a.apply(0, 1, 0);
a.apply(0, 0, 1);
assert_that(a[0] == 0);
a.apply(0, 0, 2);
assert_that(a[0] == 0);
}
{
tools::online_imos<tools::monoid::max<int>, false> a(2);
assert_that(a.size() == 2);
a.apply(0, 2, 0);
a.apply(0, 1, 1);
a.apply(0, 0, 2);
assert_that(a[1] == 0);
a.apply(0, 1, 3);
a.apply(0, 0, 4);
assert_that(a[1] == 0);
assert_that(a[0] == 3);
a.apply(0, 0, 5);
assert_that(a[1] == 0);
assert_that(a[0] == 3);
}
{
tools::online_imos<tools::group::bit_xor<int>> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
}
{
tools::online_imos<tools::group::bit_xor<int>> a(1);
assert_that(a.size() == 1);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(1, 1, 8);
assert_that(a[0] == 4);
}
{
tools::online_imos<tools::group::bit_xor<int>> a(2);
assert_that(a.size() == 2);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(2, 2, 4);
a.apply(0, 1, 8);
a.apply(1, 2, 16);
a.apply(0, 2, 32);
assert_that(a[0] == 40);
a.apply(1, 1, 64);
a.apply(2, 2, 128);
a.apply(1, 2, 256);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
a.apply(2, 2, 512);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a(1);
assert_that(a.size() == 1);
a.apply(1, 1, 1);
a.apply(0, 0, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(0, 0, 8);
assert_that(a[0] == 4);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a(2);
assert_that(a.size() == 2);
a.apply(2, 2, 1);
a.apply(1, 1, 2);
a.apply(0, 0, 4);
a.apply(1, 2, 8);
a.apply(0, 1, 16);
a.apply(0, 2, 32);
assert_that(a[1] == 40);
a.apply(1, 1, 64);
a.apply(0, 0, 128);
a.apply(0, 1, 256);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
a.apply(0, 0, 512);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
}
return 0;
}
#line 1 "tests/online_imos.test.cpp"
// competitive-verifier: STANDALONE
#include <iostream>
#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/group.hpp"
namespace tools {
namespace group {
template <typename G>
struct plus {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs + rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return -v;
}
};
template <typename G>
struct multiplies {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
static T inv(const T& v) {
return e() / v;
}
};
template <typename G>
struct bit_xor {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs ^ rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return v;
}
};
}
}
#line 1 "tools/monoid.hpp"
#include <type_traits>
#include <algorithm>
#include <limits>
#include <cassert>
#line 1 "tools/gcd.hpp"
#line 5 "tools/gcd.hpp"
#include <numeric>
namespace tools {
template <typename M, typename N>
constexpr ::std::common_type_t<M, N> gcd(const M m, const N n) {
return ::std::gcd(m, n);
}
}
#line 9 "tools/monoid.hpp"
namespace tools {
namespace monoid {
template <typename M, M ...dummy>
struct max;
template <typename M>
struct max<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::max(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::min();
} else {
return -::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct max<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(E <= lhs);
assert(E <= rhs);
return ::std::max(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M, M ...dummy>
struct min;
template <typename M>
struct min<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::min(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::max();
} else {
return ::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct min<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(lhs <= E);
assert(rhs <= E);
return ::std::min(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M>
struct multiplies {
private:
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
};
template <>
struct multiplies<bool> {
using T = bool;
static T op(const bool lhs, const bool rhs) {
return lhs && rhs;
}
static T e() {
return true;
}
};
template <typename M>
struct gcd {
private:
static_assert(!::std::is_arithmetic_v<M> || (::std::is_integral_v<M> && !::std::is_same_v<M, bool>), "If M is a built-in arithmetic type, it must be integral except for bool.");
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return ::tools::gcd(lhs, rhs);
}
static T e() {
return T(0);
}
};
template <typename M, M E>
struct update {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
return lhs == E ? rhs : lhs;
}
static T e() {
return E;
}
};
}
}
#line 1 "tools/online_imos.hpp"
#line 5 "tools/online_imos.hpp"
#include <cstddef>
#include <iterator>
#line 8 "tools/online_imos.hpp"
#include <vector>
#line 1 "tools/is_monoid.hpp"
#line 5 "tools/is_monoid.hpp"
#include <utility>
namespace tools {
template <typename M, typename = void>
struct is_monoid : ::std::false_type {};
template <typename M>
struct is_monoid<M, ::std::enable_if_t<
::std::is_same_v<typename M::T, decltype(M::op(::std::declval<typename M::T>(), ::std::declval<typename M::T>()))> &&
::std::is_same_v<typename M::T, decltype(M::e())>
, void>> : ::std::true_type {};
template <typename M>
inline constexpr bool is_monoid_v = ::tools::is_monoid<M>::value;
}
#line 1 "tools/is_group.hpp"
#line 6 "tools/is_group.hpp"
namespace tools {
template <typename G, typename = void>
struct is_group : ::std::false_type {};
template <typename G>
struct is_group<G, ::std::enable_if_t<
::std::is_same_v<typename G::T, decltype(G::op(::std::declval<typename G::T>(), ::std::declval<typename G::T>()))> &&
::std::is_same_v<typename G::T, decltype(G::e())> &&
::std::is_same_v<typename G::T, decltype(G::inv(::std::declval<typename G::T>()))>
, void>> : ::std::true_type {};
template <typename G>
inline constexpr bool is_group_v = ::tools::is_group<G>::value;
}
#line 12 "tools/online_imos.hpp"
namespace tools {
template <typename X, bool Forward = true>
class online_imos {
using M = ::std::conditional_t<::tools::is_monoid_v<X>, X, ::tools::group::plus<X>>;
using T = typename M::T;
::std::vector<T> m_vector;
::std::vector<T> m_diffs;
int m_processed;
public:
class iterator {
const online_imos<X, Forward> *m_parent;
::std::size_t m_i;
public:
using reference = const T&;
using value_type = T;
using difference_type = ::std::ptrdiff_t;
using pointer = const T*;
using iterator_category = ::std::random_access_iterator_tag;
iterator() = default;
iterator(const online_imos<X, Forward> * const parent, const ::std::size_t i) : m_parent(parent), m_i(i) {
}
reference operator*() const {
return (*this->m_parent)[this->m_i];
}
pointer operator->() const {
return &(*(*this));
}
iterator& operator++() {
++this->m_i;
return *this;
}
iterator operator++(int) {
const auto self = *this;
++*this;
return self;
}
iterator& operator--() {
--this->m_i;
return *this;
}
iterator operator--(int) {
const auto self = *this;
--*this;
return self;
}
iterator& operator+=(const difference_type n) {
this->m_i += n;
return *this;
}
iterator& operator-=(const difference_type n) {
this->m_i -= n;
return *this;
}
friend iterator operator+(const iterator self, const difference_type n) {
return iterator(self.m_parent, self.m_i + n);
}
friend iterator operator+(const difference_type n, const iterator self) {
return self + n;
}
friend iterator operator-(const iterator self, const difference_type n) {
return iterator(self.m_parent, self.m_i - n);
}
friend difference_type operator-(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return static_cast<difference_type>(lhs.m_i) - static_cast<difference_type>(rhs.m_i);
}
reference operator[](const difference_type n) const {
return *(*this + n);
}
friend bool operator==(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i == rhs.m_i;
}
friend bool operator!=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i != rhs.m_i;
}
friend bool operator<(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i < rhs.m_i;
}
friend bool operator<=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i <= rhs.m_i;
}
friend bool operator>(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i > rhs.m_i;
}
friend bool operator>=(const iterator lhs, const iterator rhs) {
assert(lhs.m_parent == rhs.m_parent);
return lhs.m_i >= rhs.m_i;
}
};
online_imos() : online_imos(0) {
}
online_imos(const int n) : m_vector(n, M::e()), m_diffs(n + 1, M::e()), m_processed(Forward ? 0 : n) {
}
int size() const {
return this->m_vector.size();
}
const T& operator[](const int i) {
assert(0 <= i && i < this->size());
if constexpr (Forward) {
for (; this->m_processed <= i; ++this->m_processed) {
this->m_vector[this->m_processed] = this->m_processed == 0
? this->m_diffs[this->m_processed]
: M::op(this->m_vector[this->m_processed - 1], this->m_diffs[this->m_processed]);
}
} else {
for (; this->m_processed > i; --this->m_processed) {
this->m_vector[this->m_processed - 1] = this->m_processed == this->size()
? this->m_diffs[this->m_processed]
: M::op(this->m_diffs[this->m_processed], this->m_vector[this->m_processed]);
}
}
return this->m_vector[i];
}
iterator begin() const { return iterator(this, 0); }
iterator cbegin() const { return this->begin(); }
iterator end() const { return iterator(this, this->size()); }
iterator cend() const { return this->end(); }
::std::reverse_iterator<iterator> rbegin() const { return ::std::make_reverse_iterator(this->end()); }
::std::reverse_iterator<iterator> crbegin() const { return this->rbegin(); }
::std::reverse_iterator<iterator> rend() const { return ::std::make_reverse_iterator(this->begin()); }
::std::reverse_iterator<iterator> crend() const { return this->rend(); }
void apply(const int l, const int r, const T& x) {
assert(0 <= l && l <= r && r <= this->size());
if constexpr (Forward) {
assert(this->m_processed <= l);
this->m_diffs[l] = M::op(x, this->m_diffs[l]);
if constexpr (::tools::is_group_v<M>) {
this->m_diffs[r] = M::op(M::inv(x), this->m_diffs[r]);
} else {
assert(r == this->size());
}
} else {
assert(r <= this->m_processed);
this->m_diffs[r] = M::op(x, this->m_diffs[r]);
if constexpr (::tools::is_group_v<M>) {
this->m_diffs[l] = M::op(M::inv(x), this->m_diffs[l]);
} else {
assert(l == 0);
}
}
}
template <typename Y = X> requires (!::tools::is_monoid_v<Y>)
void add(const int l, const int r, const T& x) {
this->apply(l, r, x);
}
};
}
#line 8 "tests/online_imos.test.cpp"
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
{
tools::online_imos<int> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
a.add(0, 0, 2);
}
{
tools::online_imos<int> a(1);
assert_that(a.size() == 1);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(1, 1, 8);
a.add(1, 1, 16);
assert_that(a[0] == 4);
}
{
tools::online_imos<int> a(2);
assert_that(a.size() == 2);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(2, 2, 4);
a.apply(0, 1, 8);
a.apply(1, 2, 16);
a.apply(0, 2, 32);
assert_that(a[0] == 40);
a.apply(1, 1, 64);
a.apply(2, 2, 128);
a.apply(1, 2, 256);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
a.apply(2, 2, 512);
a.add(2, 2, 1024);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
}
{
tools::online_imos<int, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
a.add(0, 0, 2);
}
{
tools::online_imos<int, false> a(1);
assert_that(a.size() == 1);
a.apply(1, 1, 1);
a.apply(0, 0, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(0, 0, 8);
a.add(0, 0, 16);
assert_that(a[0] == 4);
}
{
tools::online_imos<int, false> a(2);
assert_that(a.size() == 2);
a.apply(2, 2, 1);
a.apply(1, 1, 2);
a.apply(0, 0, 4);
a.apply(1, 2, 8);
a.apply(0, 1, 16);
a.apply(0, 2, 32);
assert_that(a[1] == 40);
a.apply(1, 1, 64);
a.apply(0, 0, 128);
a.apply(0, 1, 256);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
a.apply(0, 0, 512);
a.add(0, 0, 1024);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
}
{
tools::online_imos<tools::monoid::max<int>> a;
assert_that(a.size() == 0);
a.apply(0, 0, 0);
}
{
tools::online_imos<tools::monoid::max<int>> a(1);
assert_that(a.size() == 1);
a.apply(0, 1, 0);
a.apply(1, 1, 1);
assert_that(a[0] == 0);
a.apply(1, 1, 2);
assert_that(a[0] == 0);
}
{
tools::online_imos<tools::monoid::max<int>> a(2);
assert_that(a.size() == 2);
a.apply(0, 2, 0);
a.apply(1, 2, 1);
a.apply(2, 2, 2);
assert_that(a[0] == 0);
a.apply(1, 2, 3);
a.apply(2, 2, 4);
assert_that(a[0] == 0);
assert_that(a[1] == 3);
a.apply(2, 2, 5);
assert_that(a[0] == 0);
assert_that(a[1] == 3);
}
{
tools::online_imos<tools::monoid::max<int>, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 0);
}
{
tools::online_imos<tools::monoid::max<int>, false> a(1);
assert_that(a.size() == 1);
a.apply(0, 1, 0);
a.apply(0, 0, 1);
assert_that(a[0] == 0);
a.apply(0, 0, 2);
assert_that(a[0] == 0);
}
{
tools::online_imos<tools::monoid::max<int>, false> a(2);
assert_that(a.size() == 2);
a.apply(0, 2, 0);
a.apply(0, 1, 1);
a.apply(0, 0, 2);
assert_that(a[1] == 0);
a.apply(0, 1, 3);
a.apply(0, 0, 4);
assert_that(a[1] == 0);
assert_that(a[0] == 3);
a.apply(0, 0, 5);
assert_that(a[1] == 0);
assert_that(a[0] == 3);
}
{
tools::online_imos<tools::group::bit_xor<int>> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
}
{
tools::online_imos<tools::group::bit_xor<int>> a(1);
assert_that(a.size() == 1);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(1, 1, 8);
assert_that(a[0] == 4);
}
{
tools::online_imos<tools::group::bit_xor<int>> a(2);
assert_that(a.size() == 2);
a.apply(0, 0, 1);
a.apply(1, 1, 2);
a.apply(2, 2, 4);
a.apply(0, 1, 8);
a.apply(1, 2, 16);
a.apply(0, 2, 32);
assert_that(a[0] == 40);
a.apply(1, 1, 64);
a.apply(2, 2, 128);
a.apply(1, 2, 256);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
a.apply(2, 2, 512);
assert_that(a[0] == 40);
assert_that(a[1] == 304);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a;
assert_that(a.size() == 0);
a.apply(0, 0, 1);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a(1);
assert_that(a.size() == 1);
a.apply(1, 1, 1);
a.apply(0, 0, 2);
a.apply(0, 1, 4);
assert_that(a[0] == 4);
a.apply(0, 0, 8);
assert_that(a[0] == 4);
}
{
tools::online_imos<tools::group::bit_xor<int>, false> a(2);
assert_that(a.size() == 2);
a.apply(2, 2, 1);
a.apply(1, 1, 2);
a.apply(0, 0, 4);
a.apply(1, 2, 8);
a.apply(0, 1, 16);
a.apply(0, 2, 32);
assert_that(a[1] == 40);
a.apply(1, 1, 64);
a.apply(0, 0, 128);
a.apply(0, 1, 256);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
a.apply(0, 0, 512);
assert_that(a[1] == 40);
assert_that(a[0] == 304);
}
return 0;
}