Tifa's CP Library

:heavy_check_mark: fhq_treap_w (src/code/ds/fhq_treap_w.hpp)

Depends on

Verified with

Code

#ifndef TIFALIBS_DS_FHQ_TREAP_W
#define TIFALIBS_DS_FHQ_TREAP_W

#include "../rand/gen.hpp"

namespace tifa_libs::ds {

template <class T, auto op, auto e, class F, auto mapping, auto composition, auto id,
          bool reverse_ = false, bool recovery = false>
requires requires(T val, T val_l, T val_r, F tag, F tag_l, F tag_r) {
  { e() } -> std::same_as<T>;
  { id() } -> std::same_as<F>;
  { op(val_l, val_r) } -> std::same_as<T>;
  { mapping(tag, val) } -> std::same_as<T>;
  { composition(tag_l, tag_r) } -> std::same_as<F>;
}
class fhq_treap_w {
  //! initial cnt = 1;
  struct YYZ {
    T w, val;
    F sign;
    u32 sz;
    arr<u32, 2> son{};
    i32 rad;
    bool rev;
    constexpr YYZ(T W = e(), T VAL = e(), u32 SZ = 0, i32 RAD = 0, F SIGN = id()) : w(W), val(VAL), sign(SIGN), sz(SZ), rad(RAD), rev(0) {}
  };
  rand::Gen<std::uniform_int_distribution<i32>> gen;
  vec<YYZ> t;
  vecu sta;
  u32 cnt;

  constexpr u32 newnode(T val) {
    u32 ret;
    if (sta.size()) ret = *sta.rbegin(), sta.pop_back(), t[ret] = YYZ(val, val, 1, gen());
    else ret = ++cnt, t[ret] = YYZ(val, val, 1, gen());
    return ret;
  }
  constexpr void rm(u32 u) { sta.push_back(u); }
  constexpr void update(u32 u) {
    t[u].w = op(t[t[u].son[0]].w, op(t[u].val, t[t[u].son[1]].w));
    t[u].sz = t[t[u].son[0]].sz + t[t[u].son[1]].sz + 1;
  }
  constexpr void reverse__(u32 u) {
    static_assert(reverse_);
    std::swap(t[u].son[0], t[u].son[1]), t[u].rev ^= 1;
  }
  constexpr void all_update(u32 u, F f) {
    if (u) t[u].val = mapping(f, t[u].val), t[u].w = mapping(f, t[u].w), t[u].sign = composition(f, t[u].sign);
  }
  constexpr void pushdown(u32 u) {
    if (u) {
      all_update(t[u].son[0], t[u].sign), all_update(t[u].son[1], t[u].sign), t[u].sign = id();
      if constexpr (reverse_)
        if (t[u].rev) reverse__(t[u].son[0]), reverse__(t[u].son[1]), t[u].rev = 0;
    }
  }

 public:
  u32 root;

  explicit constexpr fhq_treap_w(u32 MAX_N) : gen(), t(MAX_N + 1), sta(), cnt(0), root(0) {
    if constexpr (recovery) sta.reserve(MAX_N + 1);
  }

  constexpr void split(u32 u, u32 k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      pushdown(u);
      if (t[t[u].son[0]].sz < k) x = u, split(t[u].son[1], k - t[t[u].son[0]].sz - 1, t[x].son[1], y), update(x);
      else y = u, split(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr u32 merge(u32 x, u32 y) {
    pushdown(x), pushdown(y);
    if (x && y) {
      if (t[x].rad <= t[y].rad) {
        t[x].son[1] = merge(t[x].son[1], y), update(x);
        return x;
      } else {
        t[y].son[0] = merge(x, t[y].son[0]), update(y);
        return y;
      }
    } else return x + y;
  }
  constexpr void insert(T w, u32 id_) {
    u32 x, y;
    split(root, id_, x, y);
    root = merge(merge(x, newnode(w)), y);
  }
  constexpr void insert(T w) { insert(w, cnt); }
  constexpr void erase(u32 id_) {
    u32 x, y, z;
    split(root, id_, x, y);
    split(y, 1, z, y);
    if (recovery) rm(z);
    root = merge(x, y);
  }
  constexpr void update(u32 l, u32 r, F f) {
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    all_update(y, f);
    root = merge(merge(x, y), z);
  }
  constexpr void reverse(u32 l, u32 r) {
    assert(reverse_);
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    reverse__(y);
    root = merge(merge(x, y), z);
  }
  constexpr T query(u32 l, u32 r) {
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    T ret = t[y].w;
    root = merge(merge(x, y), z);
    return ret;
  }
};

}  // namespace tifa_libs::ds

#endif
#line 1 "src/code/ds/fhq_treap_w.hpp"



#line 1 "src/code/rand/gen.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/rand/gen.hpp"

namespace tifa_libs::rand {

template <class Distri>
class Gen {
  std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::mt19937, std::mt19937_64> re;
  Distri dist;

 public:
  using random_engine = decltype(re);
  using distribution = Distri;
  using result_type = typename Distri::result_type;

  constexpr Gen() : re(std::random_device{}()), dist() {}
  constexpr Gen(result_type a, result_type b) : re(std::random_device{}()), dist(a, b) {}

  constexpr void set_range(result_type a, result_type b) { dist = Distri(a, b); }
  constexpr random_engine& rand_eng() { return re; }
  constexpr Distri& distrib() { return dist; }

  void reset_seed() { re.seed((result_type)std::chrono::duration_cast<std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::chrono::seconds, std::chrono::nanoseconds>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
  constexpr result_type operator()() { return dist(re); }
  result_type next() { return dist(re); }
};

}  // namespace tifa_libs::rand


#line 5 "src/code/ds/fhq_treap_w.hpp"

namespace tifa_libs::ds {

template <class T, auto op, auto e, class F, auto mapping, auto composition, auto id,
          bool reverse_ = false, bool recovery = false>
requires requires(T val, T val_l, T val_r, F tag, F tag_l, F tag_r) {
  { e() } -> std::same_as<T>;
  { id() } -> std::same_as<F>;
  { op(val_l, val_r) } -> std::same_as<T>;
  { mapping(tag, val) } -> std::same_as<T>;
  { composition(tag_l, tag_r) } -> std::same_as<F>;
}
class fhq_treap_w {
  //! initial cnt = 1;
  struct YYZ {
    T w, val;
    F sign;
    u32 sz;
    arr<u32, 2> son{};
    i32 rad;
    bool rev;
    constexpr YYZ(T W = e(), T VAL = e(), u32 SZ = 0, i32 RAD = 0, F SIGN = id()) : w(W), val(VAL), sign(SIGN), sz(SZ), rad(RAD), rev(0) {}
  };
  rand::Gen<std::uniform_int_distribution<i32>> gen;
  vec<YYZ> t;
  vecu sta;
  u32 cnt;

  constexpr u32 newnode(T val) {
    u32 ret;
    if (sta.size()) ret = *sta.rbegin(), sta.pop_back(), t[ret] = YYZ(val, val, 1, gen());
    else ret = ++cnt, t[ret] = YYZ(val, val, 1, gen());
    return ret;
  }
  constexpr void rm(u32 u) { sta.push_back(u); }
  constexpr void update(u32 u) {
    t[u].w = op(t[t[u].son[0]].w, op(t[u].val, t[t[u].son[1]].w));
    t[u].sz = t[t[u].son[0]].sz + t[t[u].son[1]].sz + 1;
  }
  constexpr void reverse__(u32 u) {
    static_assert(reverse_);
    std::swap(t[u].son[0], t[u].son[1]), t[u].rev ^= 1;
  }
  constexpr void all_update(u32 u, F f) {
    if (u) t[u].val = mapping(f, t[u].val), t[u].w = mapping(f, t[u].w), t[u].sign = composition(f, t[u].sign);
  }
  constexpr void pushdown(u32 u) {
    if (u) {
      all_update(t[u].son[0], t[u].sign), all_update(t[u].son[1], t[u].sign), t[u].sign = id();
      if constexpr (reverse_)
        if (t[u].rev) reverse__(t[u].son[0]), reverse__(t[u].son[1]), t[u].rev = 0;
    }
  }

 public:
  u32 root;

  explicit constexpr fhq_treap_w(u32 MAX_N) : gen(), t(MAX_N + 1), sta(), cnt(0), root(0) {
    if constexpr (recovery) sta.reserve(MAX_N + 1);
  }

  constexpr void split(u32 u, u32 k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      pushdown(u);
      if (t[t[u].son[0]].sz < k) x = u, split(t[u].son[1], k - t[t[u].son[0]].sz - 1, t[x].son[1], y), update(x);
      else y = u, split(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr u32 merge(u32 x, u32 y) {
    pushdown(x), pushdown(y);
    if (x && y) {
      if (t[x].rad <= t[y].rad) {
        t[x].son[1] = merge(t[x].son[1], y), update(x);
        return x;
      } else {
        t[y].son[0] = merge(x, t[y].son[0]), update(y);
        return y;
      }
    } else return x + y;
  }
  constexpr void insert(T w, u32 id_) {
    u32 x, y;
    split(root, id_, x, y);
    root = merge(merge(x, newnode(w)), y);
  }
  constexpr void insert(T w) { insert(w, cnt); }
  constexpr void erase(u32 id_) {
    u32 x, y, z;
    split(root, id_, x, y);
    split(y, 1, z, y);
    if (recovery) rm(z);
    root = merge(x, y);
  }
  constexpr void update(u32 l, u32 r, F f) {
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    all_update(y, f);
    root = merge(merge(x, y), z);
  }
  constexpr void reverse(u32 l, u32 r) {
    assert(reverse_);
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    reverse__(y);
    root = merge(merge(x, y), z);
  }
  constexpr T query(u32 l, u32 r) {
    u32 x, y, z;
    split(root, r + 1, x, z);
    split(x, l, x, y);
    T ret = t[y].w;
    root = merge(merge(x, y), z);
    return ret;
  }
};

}  // namespace tifa_libs::ds


Back to top page