This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "tools/online_cumsum.hpp"
It is a cumulative sum, but allows you to construct it by online.
(1) online_cumsum<T, Forward = true> a(int n = 0);
(2) online_cumsum<M, Forward = true> a(int n = 0);
(3) online_cumsum<G, Forward = true> a(int n = 0);
online_cumsum<tools::group::plus<T>, Forward> a(n);
M
, it creates an array of type typename M::T
and length $n$ filled with indeterminate values.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 set $a_i$ in the order $i = 0, 1, \ldots, {n - 1}$.Forward
is false
, you can set $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 indeterminate values.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 set $a_i$ in the order $i = 0, 1, \ldots, {n - 1}$.Forward
is false
, you can set $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
, $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
, $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 reference to $a_i$.
The return type 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
): $r^\ast \leq i < n$ where $r^\ast$ is the largest $r$ in a.sum(l, r)
called so far or $0$ (if you have not called it so far).Forward
is false
): $0 \leq i < l^\ast$ where $l^\ast$ is the smallest $l$ in a.sum(l, r)
called so far or $n$ (if you have not called it so far).(see below) a.prod(int l, int r);
It returns $a_l \cdot a_{l + 1} \cdot \ldots \cdot a_{r - 1}$. Note that the multiplication is defined by the template parameter.
The return type 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
): For all $0 \leq i < r$, $a_i$ is not indeterminate.Forward
is false
): For all $l \leq i < n$, $a_i$ is not indeterminate.a
is constructed by the constructor (2) and Forward
is true
): $l = 0$a
is constructed by the constructor (2) and Forward
is false
): $r = n$T a.sum(int l, int r);
It is an alias for a.prod(l, r)
.
a
is constructed by the constructor (1).Forward
is true
): For all $0 \leq i < r$, $a_i$ is not indeterminate.Forward
is false
): For all $l \leq i < n$, $a_i$ is not indeterminate.#ifndef TOOLS_ONLINE_CUMSUM_HPP
#define TOOLS_ONLINE_CUMSUM_HPP
#include <cassert>
#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_cumsum {
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_cumsum;
int m_processed;
public:
online_cumsum() : online_cumsum(0) {
}
online_cumsum(const int n) : m_vector(n, M::e()), m_cumsum(n + 1, M::e()), m_processed(Forward ? 0 : n) {
}
int size() const {
return this->m_vector.size();
}
T& operator[](const int i) {
assert(0 <= i && i < this->size());
return this->m_vector[i];
}
auto begin() { return this->m_vector.begin(); }
auto begin() const { return this->m_vector.begin(); }
auto cbegin() const { return this->m_vector.cbegin(); }
auto end() { return this->m_vector.end(); }
auto end() const { return this->m_vector.end(); }
auto cend() const { return this->m_vector.cend(); }
auto rbegin() { return this->m_vector.rbegin(); }
auto rbegin() const { return this->m_vector.rbegin(); }
auto crbegin() const { return this->m_vector.crbegin(); }
auto rend() { return this->m_vector.rend(); }
auto rend() const { return this->m_vector.rend(); }
auto crend() const { return this->m_vector.crend(); }
T prod(const int l, const int r) {
assert(0 <= l && l <= r && r <= this->size());
if constexpr (Forward) {
for (; this->m_processed < r; ++this->m_processed) {
this->m_cumsum[this->m_processed + 1] = M::op(this->m_cumsum[this->m_processed], this->m_vector[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(M::inv(this->m_cumsum[l]), this->m_cumsum[r]);
} else {
assert(l == 0);
return this->m_cumsum[r];
}
} else {
for (; this->m_processed > l; --this->m_processed) {
this->m_cumsum[this->m_processed - 1] = M::op(this->m_vector[this->m_processed - 1], this->m_cumsum[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(this->m_cumsum[l], M::inv(this->m_cumsum[r]));
} else {
assert(r == this->size());
return this->m_cumsum[l];
}
}
}
template <typename Y = X> requires (!::tools::is_monoid_v<Y>)
T sum(const int l, const int r) {
return this->prod(l, r);
}
};
}
#endif
#line 1 "tools/online_cumsum.hpp"
#include <cassert>
#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 10 "tools/online_cumsum.hpp"
namespace tools {
template <typename X, bool Forward = true>
class online_cumsum {
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_cumsum;
int m_processed;
public:
online_cumsum() : online_cumsum(0) {
}
online_cumsum(const int n) : m_vector(n, M::e()), m_cumsum(n + 1, M::e()), m_processed(Forward ? 0 : n) {
}
int size() const {
return this->m_vector.size();
}
T& operator[](const int i) {
assert(0 <= i && i < this->size());
return this->m_vector[i];
}
auto begin() { return this->m_vector.begin(); }
auto begin() const { return this->m_vector.begin(); }
auto cbegin() const { return this->m_vector.cbegin(); }
auto end() { return this->m_vector.end(); }
auto end() const { return this->m_vector.end(); }
auto cend() const { return this->m_vector.cend(); }
auto rbegin() { return this->m_vector.rbegin(); }
auto rbegin() const { return this->m_vector.rbegin(); }
auto crbegin() const { return this->m_vector.crbegin(); }
auto rend() { return this->m_vector.rend(); }
auto rend() const { return this->m_vector.rend(); }
auto crend() const { return this->m_vector.crend(); }
T prod(const int l, const int r) {
assert(0 <= l && l <= r && r <= this->size());
if constexpr (Forward) {
for (; this->m_processed < r; ++this->m_processed) {
this->m_cumsum[this->m_processed + 1] = M::op(this->m_cumsum[this->m_processed], this->m_vector[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(M::inv(this->m_cumsum[l]), this->m_cumsum[r]);
} else {
assert(l == 0);
return this->m_cumsum[r];
}
} else {
for (; this->m_processed > l; --this->m_processed) {
this->m_cumsum[this->m_processed - 1] = M::op(this->m_vector[this->m_processed - 1], this->m_cumsum[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(this->m_cumsum[l], M::inv(this->m_cumsum[r]));
} else {
assert(r == this->size());
return this->m_cumsum[l];
}
}
}
template <typename Y = X> requires (!::tools::is_monoid_v<Y>)
T sum(const int l, const int r) {
return this->prod(l, r);
}
};
}