proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: Online difference array (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.

License

Author

Constructor

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

Constraints

Time Complexity

size

int a.size();

It returns $n$.

Constraints

Time Complexity

operator[]

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

It returns the const reference to $a_i$.

The return type is as follows.

Constraints

Time Complexity

apply

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.

Constraints

Time Complexity

add

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.

Constraints

Time Complexity

Depends on

Verified with

Code

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


Back to top page