mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2025-01-12 12:11:59 +00:00
P3369 【模板】普通平衡树
R69586144
This commit is contained in:
parent
9680921911
commit
886c3ea0db
@ -25,11 +25,9 @@ class Treap {
|
|||||||
std::pair<node *, node *> split(node *, int);
|
std::pair<node *, node *> split(node *, int);
|
||||||
std::pair<node *, node *> splitByValue(node *, int);
|
std::pair<node *, node *> splitByValue(node *, int);
|
||||||
node *merge(node *, node *);
|
node *merge(node *, node *);
|
||||||
int _getRank(node *, int);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Treap();
|
Treap();
|
||||||
~Treap();
|
|
||||||
|
|
||||||
void insert(int);
|
void insert(int);
|
||||||
void erase(int);
|
void erase(int);
|
||||||
@ -60,10 +58,6 @@ inline void Treap::node::pushup() {
|
|||||||
Treap::Treap()
|
Treap::Treap()
|
||||||
: root(nullptr) {}
|
: root(nullptr) {}
|
||||||
|
|
||||||
Treap::~Treap() {
|
|
||||||
delete root;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int Treap::getNodeSize(Treap::node *node) {
|
inline int Treap::getNodeSize(Treap::node *node) {
|
||||||
return node == nullptr ? 0 : node->size;
|
return node == nullptr ? 0 : node->size;
|
||||||
}
|
}
|
||||||
@ -138,14 +132,11 @@ void Treap::erase(int val) {
|
|||||||
this->root = this->merge(o.first, t.second);
|
this->root = this->merge(o.first, t.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Treap::_getRank(Treap::node *p, int val) {
|
int Treap::getRank(int val) {
|
||||||
if (p == nullptr) return 1;
|
auto x = this->splitByValue(this->root, val);
|
||||||
if (val <= p->val) return this->_getRank(p->left, val);
|
int r = this->getNodeSize(x.first) + 1;
|
||||||
return this->getNodeSize(p->left) + 1 + this->_getRank(p->right, val);
|
this->root = merge(x.first, x.second);
|
||||||
}
|
return r;
|
||||||
|
|
||||||
inline int Treap::getRank(int val) {
|
|
||||||
return this->_getRank(this->root, val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Treap::getKth(int k) {
|
int Treap::getKth(int k) {
|
||||||
|
Loading…
Reference in New Issue
Block a user