C++#ifndef FHQTREAP_H
#define FHQTREAP_H
#include <ctime>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <utility>
#include <functional>
template <class T>
class TreapNode {
public:
T val;
TreapNode<T> *left, *right;
long cnt, size, prio;
TreapNode() :
val(T()), left(nullptr), right(nullptr), cnt(1), size(1), prio(std::rand()) {}
TreapNode(const T& v) :
val(v), left(nullptr), right(nullptr), cnt(1), size(1), prio(std::rand()) {}
TreapNode<T>* pushup() {
size = cnt + (left ? left->size : 0) + (right ? right->size : 0);
return this;
}
};
template <class T, class Cmp = std::less<T>>
class FHQTreap {
protected:
using node = TreapNode<T>;
Cmp cmp = Cmp();
node *root;
static node* get_min(node* cur) {
node *x = cur;
while (x && x->left) x = x->left;
return x;
}
static node* get_max(node* cur) {
node *x = cur;
while (x && x->right) x = x->right;
return x;
}
void lower_split(node* cur, const T& val, node*& x, node*& y) {
if (!cur) return (void) (x = nullptr, y = nullptr);
if (!cmp(val, cur->val)) { // cur->val >= val
x = cur, lower_split(x->right, val, x->right, y);
x->pushup();
} else {
y = cur, lower_split(y->left, val, x, y->left);
y->pushup();
}
}
void upper_split(node* cur, const T& val, node*& x, node*& y) {
if (!cur) return (void) (x = nullptr, y = nullptr);
if (val != cur->val && !cmp(val, cur->val)) { // cur->val >= val
x = cur, upper_split(x->right, val, x->right, y);
x->pushup();
} else {
y = cur, upper_split(y->left, val, x, y->left);
y->pushup();
}
}
node* merge(node* x, node* y) {
if (!x || !y) return x ? x : y;
if (x->prio < y->prio) {
x->right = merge(x->right, y);
return x->pushup();
} else {
y->left = merge(x, y->left);
return y->pushup();
}
}
T kth(node* cur, int k) const {
if (!cur) return T();
int left_size = cur->left ? cur->left->size : 0;
if (left_size >= k) return kth(cur->left, k);
if (left_size < k - cur->cnt) return kth(cur->right, k - left_size - cur->cnt);
return cur->val;
}
public:
FHQTreap() : root(nullptr) {std::srand(std::time(NULL));}
FHQTreap(std::vector<T>& arr) : root(nullptr) {
for (const T& v : arr) insert(v);
}
template <typename it>
FHQTreap(const it& begin, const it& end) {
for (auto i = begin; i != end; i++) insert(*i);
}
int size() const {return root ? root->size : 0;}
bool empty() const {return !root;}
std::vector<T> inorder() const {
if (!root) return {};
std::vector<T> res; res.reserve(size());
std::function<void(const node*)> dfs = [&](const node* x) -> void {
if (!x) return;
dfs(x->left);
for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
dfs(x->right);
};
dfs(root);
return res;
}
std::vector<T> preorder() const {
if (!root) return {};
std::vector<T> res; res.reserve(size());
std::function<void(const node*)> dfs = [&](const node* x) -> void {
if (!x) return;
for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
dfs(x->left);
dfs(x->right);
};
dfs(root);
return res;
}
std::vector<T> postorder() const {
if (!root) return {};
std::vector<T> res; res.reserve(size());
std::function<void(const node*)> dfs = [&](const node* x) -> void {
if (!x) return;
dfs(x->left);
dfs(x->right);
for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
};
dfs(root);
return res;
}
std::string prettify(int tabs = 4, char fill = ' ') const {
if (!root) return "";
std::stringstream ss;
ss << root->val << '(' << root->cnt << ")\n";
std::string fill1, fill2;
for (int i = 1; i < tabs; i++) fill1.push_back(fill);
for (int i = 1; i <= tabs; i++) fill2.push_back(fill);
std::function<void(const node*, int, std::vector<int>)> dfs =
[&](const node* x, int depth, std::vector<int> sites) -> void {
int void_num = 0;
for (node* child : {x->left, x->right}) {
if (!child) continue;
int n = depth - void_num - (int) sites.size();
std::vector<std::string> str_list;
str_list.reserve(n << 1);
for (int i = 1; i <= n; i++) str_list.push_back("│" + fill1);
for (int site : sites) {
str_list.insert(str_list.begin() + site, fill2);
}
if (x->right && child != x->right) {
str_list.emplace_back("\u251c\u2500\u2500 ");
} else {
str_list.emplace_back("\u2514\u2500\u2500 ");
void_num++, sites.push_back(depth);
}
std::string out;
for (const std::string& s : str_list) out += s;
ss << out << child->val << '(' << child->cnt << ")\n";
dfs(child, depth + 1, sites);
if (child == x->right) void_num--, sites.pop_back();
}
};
dfs(root, 0, {});
return ss.str();
}
T minimum() {
if (!root) throw -1;
return get_min(root)->val;
}
T maximum() {
if (!root) throw -1;
return get_max(root)->val;
}
void insert(const T& val) {
if (!root) {
root = new node(val);
return;
}
node *x, *y; lower_split(root, val, x, y);
root = merge(merge(x, new node(val)), y);
}
bool remove(const T& val) {
node *x, *y, *z;
lower_split(root, val, x, z);
upper_split(x, val, x, y);
if (!y) return false;
y = merge(y->left, y->right);
root = merge(merge(x, y), z);
return true;
}
int rank(const T& val) {
node *x, *y; upper_split(root, val, x, y);
int rnk = x ? x->size : 0;
root = merge(x, y);
return rnk;
}
T kth(int k) const {return kth(root, k + 1);}
T operator[] (int k) const {return kth(k);}
T lower_bound(const T& val) const {
node *cur = root;
T res = T();
while (cur) {
if (cmp(cur->val, val)) res = cur->val, cur = cur->right;
else cur = cur->left;
}
return res;
}
T upper_bound(const T& val) const {
node *cur = root;
T res = T();
while (cur) {
if (cmp(val, cur->val)) res = cur->val, cur = cur->left;
else cur = cur->right;
}
return res;
}
std::pair<FHQTreap<T, Cmp>, FHQTreap<T, Cmp>> lower_split(const T& val) {
FHQTreap<T, Cmp> a, b;
lower_split(root, val, a.root, b.root);
return {a, b};
}
std::pair<FHQTreap<T, Cmp>, FHQTreap<T, Cmp>> upper_split(const T& val) {
FHQTreap<T, Cmp> a, b;
upper_split(root, val, a.root, b.root);
return {a, b};
}
};
#endif //FHQTREAP_H