0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 13:56:26 +00:00

#104. 普通平衡树

https://loj.ac/s/1515123
This commit is contained in:
Baoshuo Ren 2022-07-15 07:27:47 +08:00
parent 2da6a4f17d
commit 5648f2252f
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -5,274 +5,261 @@ using std::cin;
using std::cout; using std::cout;
const char endl = '\n'; const char endl = '\n';
template <typename T> const int N = 1e5 + 5;
class Splay { class Splay {
private: private:
size_t root, cnt;
struct node { struct node {
T value; size_t l, r, f, c, s;
node *lchild, *rchild, *parent, **root; int v;
std::size_t size, count;
node() node()
: value(0), lchild(nullptr), rchild(nullptr), parent(nullptr), root(nullptr), size(0), count(0) {} : l(0), r(0), f(0), c(0), s(0), v(0) {}
node(const T &_value, node *_parent, node **_root) node(int _v, int _f)
: value(_value), lchild(nullptr), rchild(nullptr), parent(_parent), root(_root), size(1), count(1) {} : l(0), r(0), f(_f), c(1), s(1), v(_v) {}
~node() { size_t &child(unsigned x) {
if (lchild != nullptr) delete lchild; return !x ? l : r;
if (rchild != nullptr) delete rchild;
} }
} tr[N];
node *&child(unsigned int x) { // 上传信息
return !x ? lchild : rchild; void pushup(size_t u) {
tr[u].s = tr[tr[u].l].s + tr[tr[u].r].s + tr[u].c;
}
unsigned relation(size_t u) {
// 如果当前节点是其父亲节点的左儿子则返回 0否则返回 1
return u == tr[tr[u].f].l ? 0 : 1;
}
void rotate(size_t u) {
// 旧的父节点
size_t p = tr[u].f;
// 当前节点与父节点之间的关系
unsigned x = relation(u);
// 当前节点 <-> 父节点的父节点
if (tr[p].f) {
tr[tr[p].f].child(relation(p)) = u;
} }
tr[u].f = tr[p].f;
unsigned int relation() const { // 原先的另一个子节点 <-> 父节点
// 如果当前节点是其父亲节点的左儿子则返回 0否则返回 1 if (tr[u].child(x ^ 1)) {
return this == parent->lchild ? 0 : 1; tr[tr[u].child(x ^ 1)].f = p;
} }
tr[p].child(x) = tr[u].child(x ^ 1);
// 左儿子大小 // 原先的父节点 -> 子节点
std::size_t lsize() const { tr[u].child(x ^ 1) = p;
return lchild == nullptr ? 0 : lchild->size; tr[p].f = u;
}
// 右儿子大小 // 更新节点信息
std::size_t rsize() const { pushup(p);
return rchild == nullptr ? 0 : rchild->size; pushup(u);
} }
// 上传信息 // Splay
void pushup() { //
size = lsize() + count + rsize(); // 旋转到给定的位置target默认行为为旋转为根节点
} void splay(size_t u, size_t t = 0) {
while (tr[u].f != t) {
// 旋转 if (tr[tr[u].f].f == t) {
void rotate() { rotate(u);
node *old = parent; } else if (relation(u) == relation(tr[u].f)) {
unsigned int x = relation(); rotate(tr[u].f);
rotate(u);
if (old->parent != nullptr) {
old->parent->child(old->relation()) = this;
}
parent = old->parent;
old->child(x) = child(x ^ 1);
if (child(x ^ 1) != nullptr) {
child(x ^ 1)->parent = old;
}
child(x ^ 1) = old;
old->parent = this;
old->pushup();
pushup();
if (parent == nullptr) *root = this;
}
// Splay
void splay(node *target = nullptr) {
while (parent != target) {
if (parent->parent == target) { // 父节点是目标节点
rotate();
} else if (relation() == parent->relation()) { // 关系相同
parent->rotate();
rotate();
} else {
rotate();
rotate();
}
}
}
// 前驱:左子树的最右点
node *predecessor() {
node *pred = lchild;
while (pred->rchild != nullptr) {
pred = pred->rchild;
}
return pred;
}
// 后继:右子树的最左点
node *successor() {
node *succ = rchild;
while (succ->lchild != nullptr) {
succ = succ->lchild;
}
return succ;
}
} * root;
// 插入(内部函数)
node *_insert(const T &value) {
node **target = &root, *parent = nullptr;
while (*target != nullptr && (*target)->value != value) {
parent = *target;
parent->size++;
// 根据大小向左右子树迭代
if (value < parent->value) {
target = &parent->lchild;
} else { } else {
target = &parent->rchild; rotate(u);
rotate(u);
} }
} }
if (*target == nullptr) { // 更新根节点
*target = new node(value, parent, &root); if (!t) root = u;
} else { }
(*target)->count++;
(*target)->size++; // 前驱
//
// 左子树的最右点
size_t _predecessor(size_t u) {
size_t cur = tr[u].l;
while (tr[cur].r) {
cur = tr[cur].r;
} }
(*target)->splay(); return cur;
}
// 后继
//
// 右子树的最左点
size_t _successor(size_t u) {
size_t cur = tr[u].r;
while (tr[cur].l) {
cur = tr[cur].l;
}
return cur;
}
size_t _find(const int &v) {
size_t u = root;
while (u && tr[u].v != v) {
// 根据数值大小向左右子树迭代
u = v < tr[u].v ? tr[u].l : tr[u].r;
}
if (u) splay(u);
return u;
}
size_t _insert(const int &v) {
size_t u = root, f = 0;
while (u && tr[u].v != v) {
f = u;
// 根据数值大小向左右子树迭代
u = v < tr[u].v ? tr[u].l : tr[u].r;
}
if (u) {
tr[u].c++;
tr[u].s++;
} else {
tr[u = ++cnt] = node(v, f);
if (f) tr[f].child(v > tr[f].v) = u;
}
splay(u);
return root; return root;
} }
// 查找指定的值对应的节点 void _erase(size_t u) {
node *find(const T &value) { if (!u) return;
node *node = root; // 从根节点开始查找
while (node != nullptr && value != node->value) { if (tr[u].c > 1) { // 存在重复的数
if (value < node->value) { splay(u);
node = node->lchild; tr[u].c--;
} else { tr[u].s--;
node = node->rchild;
}
}
if (node != nullptr) {
node->splay();
}
return node;
}
// 删除
void erase(node *u) {
if (u == nullptr) return;
if (u->count > 1) { // 存在重复的数
u->splay();
u->count--;
u->size--;
return; return;
} }
node *pred = u->predecessor(), size_t pred = _predecessor(u),
*succ = u->successor(); succ = _successor(u);
pred->splay(); splay(pred); // 将前驱旋转到根节点
succ->splay(pred); splay(succ, pred); // 将后继旋转到根节点的右儿子
delete succ->lchild; tr[succ].l = 0; // 此时要删的节点为根节点的左儿子且为叶子节点
succ->lchild = nullptr;
succ->pushup(); // 更新节点信息
pred->pushup(); pushup(succ);
pushup(pred);
} }
public: public:
Splay() Splay()
: root(nullptr) { : root(0), cnt(0) {
insert(std::numeric_limits<T>::min()); // 插入哨兵节点
insert(std::numeric_limits<T>::max()); insert(std::numeric_limits<int>::min());
} insert(std::numeric_limits<int>::max());
~Splay() {
delete root;
} }
// 插入 // 插入
void insert(const T &value) { void insert(const int &v) {
_insert(value); _insert(v);
} }
// 删除 // 删除
void erase(const T &value) { void erase(const int &v) {
node *node = find(value); _erase(_find(v));
if (node == nullptr) return;
erase(node);
} }
// 排名 // 排名
unsigned int rank(const T &value) { unsigned rank(const int &v) {
node *node = find(value); size_t u = _find(v);
if (node == nullptr) { if (!u) { // 不存在则插入一个方便查找
node = _insert(value); u = _insert(v);
// 此时 node 已经成为根节点,直接计算即可
int res = node->lsize(); // 由于「哨兵」的存在,此处无需 -1
erase(node);
return res; // 此时 u 已经成为根节点,直接取左子树大小即可
unsigned r = tr[tr[u].l].s;
_erase(u);
return r;
} }
// 此时 node 已经成为根节点,直接计算即可 return tr[tr[u].l].s;
return node->lsize();
} }
// 选择 // 选择
const T &select(int k) { const int &select(unsigned k) {
node *node = root; size_t u = root;
while (k < node->lsize() || k >= node->lsize() + node->count) { while (k < tr[tr[u].l].s || k >= tr[tr[u].l].s + tr[u].c) {
if (k < node->lsize()) { // 所需的节点在左子树中 if (k < tr[tr[u].l].s) {
node = node->lchild; u = tr[u].l;
} else { } else {
k -= node->lsize() + node->count; k -= tr[tr[u].l].s + tr[u].c;
node = node->rchild; u = tr[u].r;
} }
} }
node->splay(); splay(u);
return node->value; return tr[u].v;
} }
// 前驱 // 前驱
const T &predecessor(const T &value) { const int &predecessor(const int &v) {
node *node = find(value); size_t u = _find(v);
if (node == nullptr) { if (!u) { // 不存在则插入一个方便查找
node = _insert(value); u = _insert(v);
const T &result = node->predecessor()->value;
erase(node); const int &r = tr[_predecessor(u)].v;
return result;
_erase(u); // 删除
return r;
} }
return node->predecessor()->value; return tr[_predecessor(u)].v;
} }
// 后继 // 后继
const T &successor(const T &value) { const int &successor(const int &v) {
node *node = find(value); size_t u = _find(v);
if (node == nullptr) { if (!u) { // 不存在则插入一个方便查找
node = _insert(value); u = _insert(v);
const T &result = node->successor()->value;
erase(node); const int &r = tr[_successor(u)].v;
return result;
_erase(u); // 删除
return r;
} }
return node->successor()->value; return tr[_successor(u)].v;
} }
}; };
int n; int n;
Splay<int> tree; Splay tree;
int main() { int main() {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
@ -280,42 +267,23 @@ int main() {
cin >> n; cin >> n;
for (int i = 1; i <= n; i++) { while (n--) {
int op, x; int op, x;
cin >> op >> x; cin >> op >> x;
switch (op) { if (op == 1) {
case 1: { tree.insert(x);
tree.insert(x); } else if (op == 2) {
tree.erase(x);
break; } else if (op == 3) {
} cout << tree.rank(x) << endl;
case 2: { } else if (op == 4) {
tree.erase(x); cout << tree.select(x) << endl;
} else if (op == 5) {
break; cout << tree.predecessor(x) << endl;
} } else { // op == 6
case 3: { cout << tree.successor(x) << endl;
cout << tree.rank(x) << endl;
break;
}
case 4: {
cout << tree.select(x) << endl;
break;
}
case 5: {
cout << tree.predecessor(x) << endl;
break;
}
case 6: {
cout << tree.successor(x) << endl;
break;
}
} }
} }