P3369 【模板】普通平衡树
C++#include<bits/stdc++.h>
using namespace std;
int root = 0, cur = 0; // root: 树根, cur: 当前使用的节点编号
// 节点结构体定义
struct S {
int ch[2]; // ch[0]: 左孩子, ch[1]: 右孩子
int fa = 0; // 父节点,0表示空
int size = 0; // 子树大小(包含重复计数)
int v = 0; // 节点值
int cnt = 0; // 相同值的出现次数
} tr[1000086]; // 节点数组
// 清空节点x
void clear(int x) {
tr[x].ch[0] = 0, tr[x].ch[1] = 0, tr[x].fa = 0;
tr[x].size = 0, tr[x].v = 0, tr[x].cnt = 0;
}
// 更新节点id的子树
void update(int id) {
if (id == 0) return; // 空节点不更新
tr[id].size = tr[id].cnt; // 先加上当前节点的计数
// 加上左子树大小(如果存在)
if (tr[id].ch[0]) tr[id].size += tr[tr[id].ch[0]].size;
// 加上右子树大小(如果存在)
if (tr[id].ch[1]) tr[id].size += tr[tr[id].ch[1]].size;
}
// 旋转操作:将节点x向上旋转一层
void rotate(int x) {
int y = tr[x].fa; // x的父节点
int z = tr[y].fa; // x的祖父节点
int k1 = (x == tr[y].ch[1]); // x是y的右孩子为1,左孩子为0
int k2 = (y == tr[z].ch[1]); // y是z的右孩子为1,左孩子为0
// 1. 将x的对应孩子接到y上
tr[y].ch[k1] = tr[x].ch[k1 ^ 1];
if (tr[x].ch[k1 ^ 1]) tr[tr[x].ch[k1 ^ 1]].fa = y;
// 2. 将y接到x上
tr[x].ch[k1 ^ 1] = y;
tr[y].fa = x;
// 3. 更新祖父节点连接
tr[x].fa = z;
if (z) tr[z].ch[k2] = x; // 如果祖父节点存在,更新其孩子指针
// 4. 更新节点大小(自底向上)
update(y);
update(x);
}
// Splay操作:将节点x伸展到goal的孩子位置(goal=0表示伸展到根)
void splay(int x, int goal) {
while (tr[x].fa != goal) { // 持续旋转直到x的父节点是goal
int y = tr[x].fa; // 父节点
int z = tr[y].fa; // 祖父节点
if (z != goal) { // 如果祖父节点不是goal,需要双旋
// 判断是之字形还是一字形
if ((tr[y].ch[0] == x) ^ (tr[z].ch[0] == y))
rotate(x); // 之字形:先转x
else
rotate(y); // 一字形:先转y
}
rotate(x); // 最后总是要旋转x
}
if (goal == 0) root = x; // 如果goal是0,更新根节点
}
// 插入操作:插入值x到Splay树中
void insert(int x) {
if (!root) { // 空树情况
cur++;
root = cur;
tr[root].size = 1;
tr[root].v = x;
tr[root].cnt = 1;
return;
}
int now = root, fa = 0;
while (true) {
if (x == tr[now].v) { // 值已存在
tr[now].cnt++; // 增加计数
update(now); // 更新当前节点
if (fa) update(fa); // 更新父节点
splay(now, 0); // 伸展到根
break;
}
fa = now; // 记录父节点
now = tr[now].ch[x > tr[now].v]; // 根据值大小选择方向
if (!now) { // 到达叶子位置,创建新节点
cur++;
tr[cur].size = 1;
tr[cur].v = x;
tr[cur].cnt = 1;
tr[cur].fa = fa;
tr[fa].ch[x > tr[fa].v] = cur; // 连接到父节点
update(fa); // 更新父节点
splay(cur, 0); // 伸展新节点到根
break;
}
}
}
// 查找排名:返回值x的排名(第几小)
int find_rank(int x) {
int ans = 0, now = root;
while (true) {
if (x >= tr[now].v) { // x大于等于当前节点值
ans += tr[tr[now].ch[0]].size; // 加上左子树所有元素
if (x == tr[now].v) { // 找到目标值
splay(now, 0);
return ans + 1; // 返回排名
}
ans += tr[now].cnt; // 加上当前节点计数
now = tr[now].ch[1]; // 进入右子树
} else { // x小于当前节点值
now = tr[now].ch[0]; // 进入左子树
}
}
}
// 查找第k小:返回第x小的值
int find_kth(int x) {
int now = root;
while (true) {
// 如果左子树存在且第x小在左子树中
if (tr[now].ch[0] && x <= tr[tr[now].ch[0]].size) {
now = tr[now].ch[0]; // 进入左子树
} else {
// 计算左子树大小 + 当前节点计数
int temp = (tr[now].ch[0] ? tr[tr[now].ch[0]].size : 0) + tr[now].cnt;
// 如果第x小在当前节点范围内
if (x <= temp) {
return tr[now].v; // 返回当前节点值
}
// 否则进入右子树继续查找
x -= temp;
now = tr[now].ch[1];
}
}
}
// 查找前驱:返回根节点的前驱节点编号
int pre() {
int now = tr[root].ch[0]; // 进入左子树
// 在左子树中一直向右走,找到最大值
while (tr[now].ch[1] && now) {
now = tr[now].ch[1];
}
return now; // 返回前驱节点编号(可能为0)
}
// 查找后继:返回根节点的后继节点编号
int nxt() {
int now = tr[root].ch[1]; // 进入右子树
// 在右子树中一直向左走,找到最小值
while (tr[now].ch[0] && now) {
now = tr[now].ch[0];
}
return now; // 返回后继节点编号(可能为0)
}
// 删除操作:从树中删除值x
void del(int x) {
int temp = find_rank(x); // 将值为x的节点伸展到根
// 情况1:有重复值,只减少计数
if (tr[root].cnt > 1) {
tr[root].cnt--, tr[root].size--;
return;
}
int now = root;
// 情况2:叶子节点
if (!tr[now].ch[0] && !tr[now].ch[1]) {
clear(now);
root = 0;
return;
}
// 情况3:只有右子树
else if (!tr[now].ch[0]) {
root = tr[now].ch[1];
clear(now);
tr[root].fa = 0;
return;
}
// 情况4:只有左子树
else if (!tr[now].ch[1]) {
root = tr[now].ch[0];
clear(now);
tr[root].fa = 0;
return;
}
// 情况5:左右子树都存在
int oldroot = root;
// 找到前驱并伸展到根
int temp2 = find_rank(tr[pre()].v);
// 将原根的右子树接到新根上
tr[root].ch[1] = tr[oldroot].ch[1];
tr[tr[oldroot].ch[1]].fa = root;
// 清空原根节点
clear(oldroot);
update(root); // 更新新根的大小
}
// 主函数
int main() {
int n;
cin >> n;
while (n--) {
int opt, x;
cin >> opt >> x;
// 操作1:插入x
if (opt == 1) insert(x);
// 操作2:删除x
if (opt == 2) del(x);
// 操作3:查询x的排名
if (opt == 3) {
insert(x);
printf("%d\n", find_rank(x));
del(x);
}
// 操作4:查询第x小的值
if (opt == 4) printf("%d\n", find_kth(x));
// 操作5:查询x的前驱
if (opt == 5) {
insert(x);
printf("%d\n", tr[pre()].v);
del(x);
}
// 操作6:查询x的后继
if (opt == 6) {
insert(x);
printf("%d\n", tr[nxt()].v);
del(x);
}
}
return 0;
}
干净的版本
C++#include<bits/stdc++.h>
using namespace std;
int root = 0,cur = 0;
struct S{
int ch[2],fa = 0;
int size = 0,v = 0,cnt = 0;
}tr[1000086];
void clear(int x){
tr[x].ch[0] = 0,tr[x].ch[1] = 0,tr[x].fa = 0,tr[x].size = 0,
tr[x].v = 0,tr[x].cnt = 0;
}
void update(int id){
tr[id].size = tr[id].cnt;
tr[id].size += tr[tr[id].ch[0]].size;
tr[id].size += tr[tr[id].ch[1]].size;
}
void rotate(int x){
int y = tr[x].fa,z = tr[y].fa;
int k1 = (x == tr[y].ch[1]),
k2 = (y == tr[z].ch[1]);
tr[y].ch[k1] = tr[x].ch[k1^1];
tr[tr[x].ch[k1^1]].fa = y;
tr[x].ch[k1^1] = y;
tr[z].ch[k2] = x;
tr[y].fa = x,tr[x].fa = z;
update(y);
update(x);
}
void splay(int x,int goal){
while(tr[x].fa!=goal){
int y = tr[x].fa;
int z = tr[y].fa;
if(z!=goal){
if ((tr[y].ch[0] == x)^(tr[z].ch[0] == y))
rotate(x);
else
rotate(y);
}
rotate(x);
}
if(goal==0) root = x;
}
void insert(int x){
if(!root){//空树
cur++;
root = cur;
tr[root].size = 1;
tr[root].v = x;
tr[root].cnt = 1;
return;
}
int now = root,fa = 0;
while(true){
if(x==tr[now].v){
tr[now].cnt++;
update(now);
if(fa) update(fa);
splay(now,0);
break;
}
fa = now;
now = tr[now].ch[x>tr[now].v];
if(!now){
cur++;
tr[cur].size = 1;
tr[cur].v = x;
tr[cur].cnt = 1;
tr[cur].fa = fa;
tr[fa].ch[x>tr[fa].v] = cur;
update(fa);
splay(cur,0);
break;
}
}
}
int find_rank(int x){
int ans = 0,now = root;
while(true){
if(x>=tr[now].v){
ans+=tr[tr[now].ch[0]].size;
if(x==tr[now].v){
splay(now,0);
return ans+1;
}
ans+=tr[now].cnt;
now = tr[now].ch[1];
}
else
now = tr[now].ch[0];
}
}
int find_kth(int x){
int now = root;
while(true){
if(tr[now].ch[0]&&x<=tr[tr[now].ch[0]].size)
now = tr[now].ch[0];
else{
int temp = (tr[now].ch[0]?tr[tr[now].ch[0]].size:0)+tr[now].cnt;
if(x<=temp)return tr[now].v;
x-=temp;
now=tr[now].ch[1];
}
}
}
int pre(){
int now = tr[root].ch[0];
while(tr[now].ch[1]&&now)
now = tr[now].ch[1];
return now;
}
int nxt(){
int now = tr[root].ch[1];
while(tr[now].ch[0]&&now)
now = tr[now].ch[0];
return now;
}
void del(int x){
int temp = find_rank(x);
if(tr[root].cnt>1){
tr[root].cnt--,tr[root].size--;
return;
}
int now = root;
if (!tr[now].ch[0] && !tr[now].ch[1]) {
clear(now);
root = 0;
return;
}
else if (!tr[now].ch[0]) {
root = tr[now].ch[1];
clear(now);
tr[root].fa = 0;
return;
}
else if (!tr[now].ch[1]) {
root = tr[now].ch[0];
clear(now);
tr[root].fa = 0;
return;
}
int oldroot = root;
int temp2 = find_rank(tr[pre()].v);
tr[root].ch[1] = tr[oldroot].ch[1];
tr[tr[oldroot].ch[1]].fa = root;
clear(oldroot);
update(root);
}
int main(){
int n;
cin>>n;
while(n--){
int opt,x;
cin>>opt>>x;
if(opt==1) insert(x);
if(opt==2) del(x);
if(opt==3){
insert(x);
printf("%d\n",find_rank(x));
del(x);
}
if(opt==4) printf("%d\n",find_kth(x));
if(opt==5){
insert(x);
printf("%d\n",tr[pre()].v);
del(x);
}
if(opt==6){
insert(x);
printf("%d\n",tr[nxt()].v);
del(x);
}
}
}
P3391 【模板】文艺平衡树