跳到正文

剪贴板

剪贴板 8n0a665t

原文
剪贴板信息

剪贴板信息

收录
更新

有旋 Treap 实现

C++
#ifndef TREAP_H
#define TREAP_H

#include <ctime>
#include <vector>
#include <cstdlib>
#include <sstream>
#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 Treap {
protected:
	using node = TreapNode<T>;
	Cmp cmp = Cmp();

	node *root;

	static node* left_rotate(node* p) {
		node* q = p->right;
		p->right = q->left, q->left = p, p = q;
		p->left->pushup();
		return p->pushup();
	}
	static node* right_rotate(node* p) {
		node* q = p->left;
		p->left = q->right, q->right = p, p = q;
		p->right->pushup();
		return p->pushup();
	}
	static long get_prio(node* cur) {return cur ? cur->prio : 0;}

	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 insert(node*& cur, const T& val) {
		if (!cur) {
			cur = new node(val);
			return;
		}
		if (val == cur->val) {
			cur->cnt++, cur->pushup();
			return;
		}
		if (cmp(val, cur->val)) {
			insert(cur->left, val), cur->pushup();
			if (get_prio(cur->left) < get_prio(cur)) cur = right_rotate(cur);
		} else {
			insert(cur->right, val);
			if (get_prio(cur->right) < get_prio(cur)) cur = left_rotate(cur);
		}
		cur->pushup();
	}

	bool remove_node(node*& cur) {
		if (!cur) return false;
		if (cur->cnt > 1) {
			cur->cnt--, cur->pushup();
			return true;
		}
		if (cur->left || cur->right) {
			T val = cur->val;
			if (!cur->right || get_prio(cur->left) > get_prio(cur->right)) {
				cur = right_rotate(cur);
				remove(cur->right, val);
			} else {
				cur = left_rotate(cur);
				remove(cur->left, val);
			}
			return cur->pushup(), true;
		}
		return cur = nullptr, true;
	}
	bool remove(node*& cur, const T& val) {
		if (!cur) return false;
		if (val == cur->val) return remove_node(cur);
		bool res;
		if (cmp(val, cur->val)) res = remove(cur->left, val);
		else res = remove(cur->right, val);
		if (cur) cur->pushup();
		return res;
	}

	int rank(node* cur, const T& val) const {
		if (!cur) return 1;
		int left_size = cur->left ? cur->left->size : 0;
		if (val == cur->val) return left_size + 1;
		if (cmp(val, cur->val)) return rank(cur->left, val);
		return rank(cur->right, val) + left_size + cur->cnt;
	}
	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;
	}

	node* find(const T& val) const {
		node* cur = root;
		while (cur && val != cur->val) {
			cur = cmp(val, cur->val) ? cur->left : cur->right;
		}
		return cur;
	}

public:
	Treap() : root(nullptr) {std::srand(std::time(NULL));}
	Treap(std::vector<T>& arr) : root(nullptr) {
		for (const T& v : arr) insert(v);
	}
	template <typename it>
	Treap(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) {insert(root, val);}
	bool remove(const T& val) {return remove(root, val);}

	int rank(const T& val) const {return rank(root, val) - 1;}

	T kth(int k) const {return kth(root, k + 1);}
	T operator[] (int k) const {return kth(k);}

	int count(const T& val) const {
		node* x = find(root, val);
		return x ? x->cnt : 0;
	}

	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;
	}
};

#endif //TREAP_H