cpp-library

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

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: ds/segtree_binsearch.hpp

Depends on

Verified with

Code

#pragma once
#include "ds/segtree.hpp"

template <class M>
class segment_tree_binsearch : public segment_tree<M> {
 public:
  using segment_tree<M>::segment_tree;
  using value_type = segment_tree<M>::value_type;

  // min r s.t. !f(prod(l, r)) or size()+1 if no such r exists
  template <class F>
  int partition_point(int l, F f) const {
    if (!f(this->m.unit())) return l;
    if (f(this->data[1])) return this->size() + 1;
    if (l < this->size() && !f(this->data[l + this->size()])) return l + 1;
    int r = l + this->size();
    while (r % 2 == 0) r /= 2;
    value_type acc = this->m.unit();
    do {
      value_type acc2 = this->m.op(acc, this->data[r]);
      if (f(acc2)) {
        acc = acc2, r++;
        while (r % 2 == 0) r /= 2;
      } else if (r < this->size()) {
        r *= 2;
      }
    } while (r < this->size());
    if (f(this->m.op(acc, this->data[r]))) r++;
    r = r + 1 - this->size();
    return r <= l ? this->size() + 1 : r;
  }

  // max l s.t. !f(prod(l, r)) or -1 if no such l exists
  template <class F>
  int rpartition_point(int r, F f) const {
    if (!f(this->m.unit())) return r;
    if (f(this->data[1])) return -1;
    if (r > 0 && !f(this->data[r - 1 + this->size()])) return r - 1;
    int l = r + this->size() - 1;
    while (l % 2 == 1 && l > 1) l /= 2;
    value_type acc = this->m.unit();
    do {
      value_type acc2 = this->m.op(this->data[l], acc);
      if (f(acc2)) {
        acc = acc2, l--;
        while (l % 2 == 1 && l > 1) l /= 2;
      } else if (l < this->size()) {
        l = l * 2 + 1;
      }
    } while (l < this->size());
    if (f(this->m.op(this->data[l], acc))) l--;
    l = l - this->size();
    return l >= r ? -1 : l;
  }
  // min r s.t. prod(l, r) >= x
  template <class Comp = less<>>
  int lower_bound(int l, value_type x, Comp comp = Comp()) const {
    return partition_point(l, [&](auto y) { return comp(y, x); });
  }
  // max l s.t. prod(l, r) >= x
  template <class Comp = less<>>
  int rlower_bound(int r, value_type x, Comp comp = Comp()) const {
    return rpartition_point(r, [&](auto y) { return comp(y, x); });
  }
};
#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 "ds/segtree.hpp"

template <class M>
class segment_tree {
 public:
  using value_type = typename M::type;
  template <class Iter>
  segment_tree(Iter f, Iter l, M m = M()) : m(m), data((l - f) * 2) {
    copy(f, l, data.begin() + (l - f));
    init();
  }
  template <class F>
  segment_tree(int n, F f, M m = M()) : m(m), data(n * 2) {
    rep(i, n) data[i + n] = f(i);
    init();
  }
  segment_tree(int n = 0, M m = M()) : m(m), data(n * 2, m.unit()) {}

  int size() const { return data.size() / 2; }
  value_type prod(int l, int r) const {
    value_type accl = m.unit(), accr = m.unit();
    for (l += size(), r += size(); l < r; l >>= 1, r >>= 1) {
      if (l & 1) accl = m.op(accl, data[l++]);
      if (r & 1) accr = m.op(data[--r], accr);
    }
    return m.op(accl, accr);
  }
  void mul(int i, value_type v) {
    exec(i, [&](value_type& e) { e = m.op(e, v); });
  }
  void set(int i, value_type v) {
    exec(i, [&](value_type& e) { e = v; });
  }
  template <class F>
  void exec(int i, F f) {
    f(data[i + size()]);
    for (i += size(); i >>= 1;) data[i] = m.op(data[i << 1], data[i << 1 | 1]);
  }

 protected:
  M m;
  vector<value_type> data;

  void init() {
    repr2(i, 1, size()) data[i] = m.op(data[i << 1], data[i << 1 | 1]);
  }
};
#line 3 "ds/segtree_binsearch.hpp"

template <class M>
class segment_tree_binsearch : public segment_tree<M> {
 public:
  using segment_tree<M>::segment_tree;
  using value_type = segment_tree<M>::value_type;

  // min r s.t. !f(prod(l, r)) or size()+1 if no such r exists
  template <class F>
  int partition_point(int l, F f) const {
    if (!f(this->m.unit())) return l;
    if (f(this->data[1])) return this->size() + 1;
    if (l < this->size() && !f(this->data[l + this->size()])) return l + 1;
    int r = l + this->size();
    while (r % 2 == 0) r /= 2;
    value_type acc = this->m.unit();
    do {
      value_type acc2 = this->m.op(acc, this->data[r]);
      if (f(acc2)) {
        acc = acc2, r++;
        while (r % 2 == 0) r /= 2;
      } else if (r < this->size()) {
        r *= 2;
      }
    } while (r < this->size());
    if (f(this->m.op(acc, this->data[r]))) r++;
    r = r + 1 - this->size();
    return r <= l ? this->size() + 1 : r;
  }

  // max l s.t. !f(prod(l, r)) or -1 if no such l exists
  template <class F>
  int rpartition_point(int r, F f) const {
    if (!f(this->m.unit())) return r;
    if (f(this->data[1])) return -1;
    if (r > 0 && !f(this->data[r - 1 + this->size()])) return r - 1;
    int l = r + this->size() - 1;
    while (l % 2 == 1 && l > 1) l /= 2;
    value_type acc = this->m.unit();
    do {
      value_type acc2 = this->m.op(this->data[l], acc);
      if (f(acc2)) {
        acc = acc2, l--;
        while (l % 2 == 1 && l > 1) l /= 2;
      } else if (l < this->size()) {
        l = l * 2 + 1;
      }
    } while (l < this->size());
    if (f(this->m.op(this->data[l], acc))) l--;
    l = l - this->size();
    return l >= r ? -1 : l;
  }
  // min r s.t. prod(l, r) >= x
  template <class Comp = less<>>
  int lower_bound(int l, value_type x, Comp comp = Comp()) const {
    return partition_point(l, [&](auto y) { return comp(y, x); });
  }
  // max l s.t. prod(l, r) >= x
  template <class Comp = less<>>
  int rlower_bound(int r, value_type x, Comp comp = Comp()) const {
    return rpartition_point(r, [&](auto y) { return comp(y, x); });
  }
};
Back to top page