This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "tools/online_imos.hpp"
It is an array with an auxiliary difference array to perform range update queries, but allows you to construct the final array by online.
(1) online_imos<T, Forward = true> a(int n = 0);
(2) online_imos<M, Forward = true> a(int n = 0);
(3) online_imos<G, Forward = true> a(int n = 0);
online_imos<tools::group::plus<T>, Forward> a(n);
M
, it creates an array of type typename M::T
and length $n$ filled with M::e()
.M
. In other words, $x \cdot y$ is defined by M::op(x, y)
and the identity is defined by M::e()
.Forward
is true
, you can get $a_i$ in the order $i = 0, 1, \ldots, {n - 1}$.Forward
is false
, you can get $a_i$ in the order $i = n - 1, n - 2, \ldots, 0$.G
, it creates an array of type typename G::T
and length $n$ filled with G::e()
.G
. In other words, $x \cdot y$ is defined by G::op(x, y)
, the identity is defined by G::e()
and $x^{-1}$ is defined by G::inv(x)
.Forward
is true
, you can get $a_i$ in the order $i = 0, 1, \ldots, {n - 1}$.Forward
is false
, you can get $a_i$ in the order $i = n - 1, n - 2, \ldots, 0$.tools::is_monoid_v<T>
does not hold.tools::is_monoid_v<M>
holds, but tools::is_group_v<M>
does not hold.typename M::T
and $y$ in typename M::T
, M::op(x, y)
$=$ M::op(y, x)
.typename M::T
, $y$ in typename M::T
and $z$ in typename M::T
, M::op(M::op(x, y), z)
$=$ M::op(x, M::op(y, z))
.typename M::T
, M::op(M::e(), x)
$=$ M::op(x, M::e())
$= x$.tools::is_group_v<G>
holds.typename G::T
and $y$ in typename G::T
, G::op(x, y)
$=$ G::op(y, x)
.typename G::T
, $y$ in typename G::T
and $z$ in typename G::T
, G::op(G::op(x, y), z)
$=$ G::op(x, G::op(y, z))
.typename G::T
, G::op(G::e(), x)
$=$ G::op(x, G::e())
$= x$.typename G::T
, G::op(G::inv(x), x)
$=$ G::op(x, G::inv(x))
$=$ G::e()
.int a.size();
It returns $n$.
(see below) a.operator[](int i);
It returns the const reference to $a_i$.
The return type is as follows.
a
is constructed by the constructor (1)): const T&
a
is constructed by the constructor (2)): const typename M::T&
a
is constructed by the constructor (3)): const typename G::T&
void a.apply(int l, int r, (see below) x);
It updates $a_i$ to $x \cdot a_i$ for $i$ such that $l \leq i < r$. Note that the multiplication is defined by the template parameter.
The type of $x$ is as follows.
a
is constructed by the constructor (1)): T
a
is constructed by the constructor (2)): typename M::T
a
is constructed by the constructor (3)): typename G::T
Forward
is true
): $i^\ast < l$ where $i^\ast$ is the largest $i$ in a[i]
called so far or $-1$ (if you have not called it so far).Forward
is false
): $r \leq i^\ast$ where $i^\ast$ is the smallest $i$ in a[i]
called so far or $n$ (if you have not called it so far).a
is constructed by the constructor (2) and Forward
is true
): $r = n$a
is constructed by the constructor (2) and Forward
is false
): $l = 0$void a.add(int l, int r, (see below) x);
It is an alias for a.apply(l, r, x)
.
The type of $x$ is as follows.
a
is constructed by the constructor (1)): T
a
is constructed by the constructor (2)): typename M::T
a
is constructed by the constructor (3)): typename G::T
a
is constructed by the constructor (1).Forward
is true
): $i^\ast < l$ where $i^\ast$ is the largest $i$ in a[i]
called so far or $-1$ (if you have not called it so far).Forward
is false
): $r \leq i^\ast$ where $i^\ast$ is the smallest $i$ in a[i]
called so far or $n$ (if you have not called it so far).#ifndef TOOLS_ONLINE_IMOS_HPP
#define TOOLS_ONLINE_IMOS_HPP
#include <cassert>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <vector>
#include "tools/group.hpp"
#include "tools/is_monoid.hpp"
#include "tools/is_group.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);
}
};
}
#endif
#line 1 "tools/online_imos.hpp"
#include <cassert>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <vector>
#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/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);
}
};
}