proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Online cumulative sum (tools/online_cumsum.hpp)

It is a cumulative sum, but allows you to construct it by online.

License

Author

Constructor

(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);

Constraints

Time Complexity

size

int a.size();

It returns $n$.

Constraints

Time Complexity

operator[]

(see below) a.operator[](int i);

It returns the reference to $a_i$.

The return type is as follows.

Constraints

Time Complexity

prod

(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.

Constraints

Time Complexity

sum

T a.sum(int l, int r);

It is an alias for a.prod(l, r).

Constraints

Time Complexity

Depends on

Required by

Verified with

Code

#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);
    }
  };
}


Back to top page