Tifa's CP Library

:warning: fhq_treap (src/code/ds/fhq_treap.hpp)

Depends on

Code

#ifndef TIFALIBS_DS_FHQ_TREAP
#define TIFALIBS_DS_FHQ_TREAP

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

namespace tifa_libs::ds {

template <class KEY, class VAL, bool recovery = false>
class fhq_treap {
  //! initial cnt = 1
  struct YYZ {
    std::pair<KEY, VAL> w;
    u32 sz;
    arr<u32, 2> son{};
    i32 rad;
    constexpr YYZ(std::pair<KEY, VAL> W = {0, 0}, u32 SZ = 0, i32 RAD = 0) : w(W), sz(SZ), rad(RAD) {}
  };
  rand::Gen<std::uniform_int_distribution<i32>> gen;
  vec<YYZ> t;
  vecu sta;
  u32 cnt;

  constexpr u32 newnode(std::pair<KEY, VAL> const& w) {
    u32 ret;
    if (recovery && sta.size()) ret = *sta.rbegin(), sta.pop_back(), t[ret] = YYZ(w, 1, gen());
    else ret = ++cnt, t.push_back(YYZ(w, 1, gen()));
    return ret;
  }
  constexpr void preorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    ret.push_back(t[u].w);
    preorder(t[u].son[0], ret);
    preorder(t[u].son[1], ret);
  }
  constexpr void inorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    inorder(t[u].son[0], ret);
    ret.push_back(t[u].w);
    inorder(t[u].son[1], ret);
  }
  constexpr void postorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    preorder(t[u].son[0], ret);
    preorder(t[u].son[1], ret);
    ret.push_back(t[u].w);
  }
  constexpr void rm(u32 u) { sta.push_back(u); }
  constexpr void update(u32 u) { t[u].sz = t[t[u].son[0]].sz + t[t[u].son[1]].sz + 1; }

 public:
  u32 root;

  explicit constexpr fhq_treap() : gen(), t(1), sta(), cnt(0), root(0) {}

  constexpr void split(u32 u, KEY k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      if (t[u].w.first <= k) x = u, split(t[u].son[1], k, t[u].son[1], y), update(x);
      else y = u, split(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr void split_not_include(u32 u, KEY k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      if (t[u].w.first < k) x = u, split_not_include(t[u].son[1], k, t[u].son[1], y), update(x);
      else y = u, split_not_include(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr u32 merge(u32 x, u32 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(std::pair<KEY, VAL> w) {
    u32 x, y;
    split(root, w.first, x, y);
    root = merge(merge(x, newnode(w)), y);
  }
  constexpr void erase(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    if (recovery) rm(z);
    z = merge(t[z].son[0], t[z].son[1]);
    root = merge(merge(x, z), y);
  }
  constexpr u32 key_req_rk(KEY key) {
    u32 x, y, ret;
    split_not_include(root, key, x, y);
    ret = t[x].sz + 1;
    root = merge(x, y);
    return ret;
  }
  constexpr std::pair<KEY, VAL> rk_req_w(u32 u, u32 k) {
    while (1) {
      if (t[t[u].son[0]].sz >= k) u = t[u].son[0];
      else {
        if (t[t[u].son[0]].sz + 1 >= k) return t[u].w;
        k -= t[t[u].son[0]].sz + 1, u = t[u].son[1];
      }
    }
  }
  constexpr std::pair<KEY, VAL> pre_w(KEY key) {
    u32 x, y;
    split(root, key - 1, x, y);
    std::pair<KEY, VAL> ret = rk_req_w(x, t[x].sz);
    root = merge(x, y);
    return ret;
  }
  constexpr std::pair<KEY, VAL> suf_w(KEY key) {
    u32 x, y;
    split(root, key, x, y);
    std::pair<KEY, VAL> ret = rk_req_w(y, 1);
    root = merge(x, y);
    return ret;
  }
  constexpr bool find(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    bool ret = t[z].sz;
    root = merge(merge(x, z), y);
    return ret;
  }
  constexpr vecp<KEY, VAL> preorder() {
    vecp<KEY, VAL> ret;
    preorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> inorder() {
    vecp<KEY, VAL> ret;
    inorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> postorder() {
    vecp<KEY, VAL> ret;
    postorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> inorder(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    vecp<KEY, VAL> ret;
    inorder(z, ret);
    root = merge(merge(x, z), y);
    return ret;
  }
};

}  // namespace tifa_libs::ds

#endif
#line 1 "src/code/ds/fhq_treap.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.hpp"

namespace tifa_libs::ds {

template <class KEY, class VAL, bool recovery = false>
class fhq_treap {
  //! initial cnt = 1
  struct YYZ {
    std::pair<KEY, VAL> w;
    u32 sz;
    arr<u32, 2> son{};
    i32 rad;
    constexpr YYZ(std::pair<KEY, VAL> W = {0, 0}, u32 SZ = 0, i32 RAD = 0) : w(W), sz(SZ), rad(RAD) {}
  };
  rand::Gen<std::uniform_int_distribution<i32>> gen;
  vec<YYZ> t;
  vecu sta;
  u32 cnt;

  constexpr u32 newnode(std::pair<KEY, VAL> const& w) {
    u32 ret;
    if (recovery && sta.size()) ret = *sta.rbegin(), sta.pop_back(), t[ret] = YYZ(w, 1, gen());
    else ret = ++cnt, t.push_back(YYZ(w, 1, gen()));
    return ret;
  }
  constexpr void preorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    ret.push_back(t[u].w);
    preorder(t[u].son[0], ret);
    preorder(t[u].son[1], ret);
  }
  constexpr void inorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    inorder(t[u].son[0], ret);
    ret.push_back(t[u].w);
    inorder(t[u].son[1], ret);
  }
  constexpr void postorder(u32 u, vecp<KEY, VAL>& ret) {
    if (!u) return;
    preorder(t[u].son[0], ret);
    preorder(t[u].son[1], ret);
    ret.push_back(t[u].w);
  }
  constexpr void rm(u32 u) { sta.push_back(u); }
  constexpr void update(u32 u) { t[u].sz = t[t[u].son[0]].sz + t[t[u].son[1]].sz + 1; }

 public:
  u32 root;

  explicit constexpr fhq_treap() : gen(), t(1), sta(), cnt(0), root(0) {}

  constexpr void split(u32 u, KEY k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      if (t[u].w.first <= k) x = u, split(t[u].son[1], k, t[u].son[1], y), update(x);
      else y = u, split(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr void split_not_include(u32 u, KEY k, u32& x, u32& y) {
    if (!u) x = y = 0;
    else {
      if (t[u].w.first < k) x = u, split_not_include(t[u].son[1], k, t[u].son[1], y), update(x);
      else y = u, split_not_include(t[u].son[0], k, x, t[y].son[0]), update(y);
    }
  }
  constexpr u32 merge(u32 x, u32 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(std::pair<KEY, VAL> w) {
    u32 x, y;
    split(root, w.first, x, y);
    root = merge(merge(x, newnode(w)), y);
  }
  constexpr void erase(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    if (recovery) rm(z);
    z = merge(t[z].son[0], t[z].son[1]);
    root = merge(merge(x, z), y);
  }
  constexpr u32 key_req_rk(KEY key) {
    u32 x, y, ret;
    split_not_include(root, key, x, y);
    ret = t[x].sz + 1;
    root = merge(x, y);
    return ret;
  }
  constexpr std::pair<KEY, VAL> rk_req_w(u32 u, u32 k) {
    while (1) {
      if (t[t[u].son[0]].sz >= k) u = t[u].son[0];
      else {
        if (t[t[u].son[0]].sz + 1 >= k) return t[u].w;
        k -= t[t[u].son[0]].sz + 1, u = t[u].son[1];
      }
    }
  }
  constexpr std::pair<KEY, VAL> pre_w(KEY key) {
    u32 x, y;
    split(root, key - 1, x, y);
    std::pair<KEY, VAL> ret = rk_req_w(x, t[x].sz);
    root = merge(x, y);
    return ret;
  }
  constexpr std::pair<KEY, VAL> suf_w(KEY key) {
    u32 x, y;
    split(root, key, x, y);
    std::pair<KEY, VAL> ret = rk_req_w(y, 1);
    root = merge(x, y);
    return ret;
  }
  constexpr bool find(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    bool ret = t[z].sz;
    root = merge(merge(x, z), y);
    return ret;
  }
  constexpr vecp<KEY, VAL> preorder() {
    vecp<KEY, VAL> ret;
    preorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> inorder() {
    vecp<KEY, VAL> ret;
    inorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> postorder() {
    vecp<KEY, VAL> ret;
    postorder(root, ret);
    return ret;
  }
  constexpr vecp<KEY, VAL> inorder(KEY key) {
    u32 x, y, z;
    split(root, key, x, y);
    split_not_include(x, key, x, z);
    vecp<KEY, VAL> ret;
    inorder(z, ret);
    root = merge(merge(x, z), y);
    return ret;
  }
};

}  // namespace tifa_libs::ds


Back to top page