0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2025-01-11 23:12:00 +00:00

P3369 【模板】普通平衡树

R69538002
This commit is contained in:
Baoshuo Ren 2022-02-17 16:19:39 +08:00
parent 97ba4f11fc
commit 02391450c0
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -13,21 +13,12 @@ class Treap {
node *left, *right; node *left, *right;
int size, val, key; int size, val, key;
node() node();
: left(nullptr), right(nullptr), size(1), val(0), key(rand()) {} node(int);
node(int _val) ~node();
: left(nullptr), right(nullptr), size(1), val(_val), key(rand()) {}
~node() { void pushup();
delete left, right; } * root;
}
inline void pushup() {
this->size = 1;
if (this->left != nullptr) this->size += this->left->size;
if (this->right != nullptr) this->size += this->right->size;
}
} *root = nullptr;
int getNodeSize(node *); int getNodeSize(node *);
node *find(node *, int); node *find(node *, int);
@ -37,8 +28,8 @@ class Treap {
int _getRank(node *, int); int _getRank(node *, int);
public: public:
Treap() Treap();
: root(nullptr) {} ~Treap();
void insert(int); void insert(int);
void erase(int); void erase(int);
@ -46,6 +37,33 @@ class Treap {
int getKth(int); int getKth(int);
} tree; } tree;
// struct Treap::node
Treap::node::node()
: left(nullptr), right(nullptr), size(1), val(0), key(rand()) {}
Treap::node::node(int _val)
: left(nullptr), right(nullptr), size(1), val(_val), key(rand()) {}
Treap::node::~node() {
delete this->left, this->right;
}
inline void Treap::node::pushup() {
this->size = 1;
if (this->left != nullptr) this->size += this->left->size;
if (this->right != nullptr) this->size += this->right->size;
}
// class Treap
Treap::Treap()
: 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;
} }