cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: ds/dual_segtree.hpp

Depends on

Verified with

Code

#pragma once
#include "algebra.hpp"
#include "bit/ctz.hpp"
#include "bit/ilog2.hpp"

template <class T, class A, class F>
class dual_segment_tree {
 public:
  using value_type = T;
  using actor_type = typename A::type;
  dual_segment_tree(vector<value_type> v, A a = A(), F f = F())
      : a(a), f(f), data(move(v)), lazy(data.size(), a.unit()) {}
  template <class Iter>
  dual_segment_tree(Iter first, Iter last, A a = A(), F f = F())
      : dual_segment_tree({first, last}, a, f) {}
  dual_segment_tree(int n = 0, T e = T(), A a = A(), F f = F())
      : a(a), f(f), data(n, e), lazy(n, a.unit()) {}

  int size() const { return data.size(); }
  value_type get(int i) {
    flush(i + size());
    return data[i];
  }
  void apply(int l, int r, actor_type x) {
    if (x == a.unit()) return;
    flush(trunc(l + size()));
    flush(trunc(r + size()) - 1);
    for (int L = l + size(), R = r + size(); L < R; L >>= 1, R >>= 1) {
      if (L & 1) apply(L++, x);
      if (R & 1) apply(--R, x);
    }
  }
  void mul(int i, value_type v) {
    exec(i, [=](value_type& e) { e = e.op(e, v); });
  }
  void set(int i, value_type v) {
    exec(i, [=](value_type& e) { e = v; });
  }
  template <class G>
  void exec(int i, G f) {
    flush(trunc(i + size()));
    f(data[i]);
  }

 private:
  A a;
  F f;
  vector<value_type> data;
  vector<actor_type> lazy;
  static int trunc(int a) { return a >> ctz(a); }
  void apply(int i, actor_type x) {
    if (i < size())
      lazy[i] = a.op(lazy[i], x);
    else
      data[i - size()] = f(data[i - size()], x);
  }
  void push(int i) {
    if (lazy[i] != a.unit()) {
      apply(i << 1, lazy[i]);
      apply(i << 1 | 1, lazy[i]);
      lazy[i] = a.unit();
    }
  }
  void flush(int i) {
    if (i)
      for (int s = ilog2(i); s > 0; s--) push(i >> s);
  }
};
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "algebra.hpp"

#define CONST(val) [=] { return val; }
#define WRAP_FN(func) \
  [](auto&&... args) { return func(forward<decltype(args)>(args)...); }

template <class Unit, class Op>
struct monoid : private Unit, private Op {
  using type = decltype(declval<Unit>()());
  monoid(Unit unit, Op op) : Unit(unit), Op(op) {}
  type unit() const { return Unit::operator()(); }
  type op(type a, type b) const { return Op::operator()(a, b); }
};

template <class Unit, class Op, class Inv>
struct group : monoid<Unit, Op>, private Inv {
  using type = typename monoid<Unit, Op>::type;
  group(Unit unit, Op op, Inv inv) : monoid<Unit, Op>(unit, op), Inv(inv) {}
  type inv(type a) const { return Inv::operator()(a); }
};

template <class T>
struct addition {
  using type = T;
  type unit() const { return 0; }
  type op(type a, type b) const { return a + b; }
  type inv(type a) const { return -a; }
};

template <class T>
struct maximum {
  using type = T;
  type unit() const { return numeric_limits<T>::min(); }
  type op(type a, type b) const { return a > b ? a : b; }
};

template <class T>
struct minimum {
  using type = T;
  type unit() const { return numeric_limits<T>::max(); }
  type op(type a, type b) const { return a > b ? b : a; }
};

template <class T, T nul = -1>
struct assign {
  using type = T;
  type unit() const { return nul; }
  type op(type a, type b) const { return b == nul ? a : b; }
};
#line 3 "bit/ctz.hpp"

#pragma GCC target("bmi")

template <class T>
int ctz(T x) {
  if (!x) return sizeof(T) * 8;
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_ctz((unsigned)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
    return __builtin_ctzll((unsigned long long)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
    unsigned long long y = x;
    return y ? ctz(y)
             : sizeof(y) * 8 + ctz((unsigned long long)(x >> sizeof(y) * 8));
  }
}
#line 3 "bit/clz.hpp"

#pragma GCC target("lzcnt")

template <class T>
int clz(T x) {
    if (!x) return sizeof(T) * 8;
    if constexpr (sizeof(T) <= sizeof(unsigned)) {
        return __builtin_clz((unsigned)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
        return __builtin_clzll((unsigned long long)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
        int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
        return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
    }
}
#line 4 "bit/ilog2.hpp"

template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }

template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 5 "ds/dual_segtree.hpp"

template <class T, class A, class F>
class dual_segment_tree {
 public:
  using value_type = T;
  using actor_type = typename A::type;
  dual_segment_tree(vector<value_type> v, A a = A(), F f = F())
      : a(a), f(f), data(move(v)), lazy(data.size(), a.unit()) {}
  template <class Iter>
  dual_segment_tree(Iter first, Iter last, A a = A(), F f = F())
      : dual_segment_tree({first, last}, a, f) {}
  dual_segment_tree(int n = 0, T e = T(), A a = A(), F f = F())
      : a(a), f(f), data(n, e), lazy(n, a.unit()) {}

  int size() const { return data.size(); }
  value_type get(int i) {
    flush(i + size());
    return data[i];
  }
  void apply(int l, int r, actor_type x) {
    if (x == a.unit()) return;
    flush(trunc(l + size()));
    flush(trunc(r + size()) - 1);
    for (int L = l + size(), R = r + size(); L < R; L >>= 1, R >>= 1) {
      if (L & 1) apply(L++, x);
      if (R & 1) apply(--R, x);
    }
  }
  void mul(int i, value_type v) {
    exec(i, [=](value_type& e) { e = e.op(e, v); });
  }
  void set(int i, value_type v) {
    exec(i, [=](value_type& e) { e = v; });
  }
  template <class G>
  void exec(int i, G f) {
    flush(trunc(i + size()));
    f(data[i]);
  }

 private:
  A a;
  F f;
  vector<value_type> data;
  vector<actor_type> lazy;
  static int trunc(int a) { return a >> ctz(a); }
  void apply(int i, actor_type x) {
    if (i < size())
      lazy[i] = a.op(lazy[i], x);
    else
      data[i - size()] = f(data[i - size()], x);
  }
  void push(int i) {
    if (lazy[i] != a.unit()) {
      apply(i << 1, lazy[i]);
      apply(i << 1 | 1, lazy[i]);
      lazy[i] = a.unit();
    }
  }
  void flush(int i) {
    if (i)
      for (int s = ilog2(i); s > 0; s--) push(i >> s);
  }
};
Back to top page