BZOJ3224:Tyvj 1728 普通平衡树(splay)

本文介绍了一种使用Splay树实现的动态数据结构,该结构能够高效地完成插入、删除、查询等操作,并提供了具体实现代码。适用于需要频繁进行元素增删查的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598

Sample Output

106465
84185
492737

HINT

 

1.n的数据范围:n<=100000

 

2.每个数的数据范围:[-2e9,2e9]

思路:练习splay,代码比较长,用栈来回收节点节省时间,splay感觉跟普通的二叉查找树差不多,就是多了一些花式的旋转来达到缩减复杂度。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cstdlib>
using namespace std;
const int maxn = 1e5+30;
const int inf = 2e9+7;
int ans;
struct node{
    int num, val, siz;
    node *fa, *son[2];
}*root, tree[maxn], *stk[maxn];
int top;
node* newnode(int val, node* fa){//新建节点
    node* t = stk[top--];
    t->num = t->siz = 1;
    t->val = val;
    t->fa = fa;
    t->son[0] = t->son[1] = NULL;
    return t;
}
void freenode(node* t){//释放节点
    stk[++top] = t;
}
int size(node* t){//返回子树大小
    return t==NULL?0:t->siz;
}
void update(node* t){//更新子树大小
    if(t != NULL){
        t->siz = t->num;
        t->siz += size(t->son[0]);
        t->siz += size(t->son[1]);
    }
}
bool son(node* f, node* s){//判断s是f的哪个儿子
    if(f == NULL) return 0;
    return f->son[1] == s;
}
void connect(node* f, node* s, bool k){//将s将到f的k儿子上
    if(f != NULL) f->son[k] = s;
    else root = s;
    if(s != NULL) s->fa = f;
}
void rotate(node* s){//将s转到它父亲的位置
    node* f = s->fa;
    node* g = f->fa;
    bool a = son(f, s), b = !a;
    connect(f, s->son[b], a);
    connect(g, s, son(g, f));
    connect(s, f, b);
    update(f);
    update(s);
}
void splay(node* s, node* p){//将s成p的儿子
    if(s == NULL) return;
    while(s->fa != p){
        node* f = s->fa;
        node* g = f->fa;
        if(g == p) rotate(s);
        else{
            if(son(g,f) ^ son(f,s)) rotate(f), rotate(s);
            else rotate(s), rotate(s);
        }
    }
}
node* find(int val){//查找某个数所在节点
    node* res = root;
    while(res != NULL && res->val != val)
        res = res->son[val>=res->val];
    return splay(res, NULL), res;//将res转到根节点
}
node* insert(node* t, int val){//插入一个新的数
    node* res = t->son[val>=t->val];
    if(res == NULL) res = t->son[val>=t->val] = newnode(val, t);
    else res = insert(res, val);
    return res;
}
void insert(int val){//插入一个数
    if(root == NULL) root = newnode(val, NULL);
    else if(find(val) != NULL) ++root->num, update(root);
    else splay(insert(root, val), NULL);
}
void erase(node *t) {//删除根节点
    if (t->son[0] == NULL){
        root = t->son[1];
        if(root != NULL) root->fa = NULL;
    }
    else {
        node *p = t->son[0];
        while (p->son[1] != NULL) p = p->son[1];
        splay(p, t);
        root = p;
        root->fa = NULL;
        p->son[1] = t->son[1];
        if (p->son[1] != NULL) p->son[1]->fa = p;
        update(root);
    }
    freenode(t);
}
void erase(node* t, int k){//t节点数量减去k
    t->num -= k;
    if(t->num <= 0) erase(t);
    else update(t);
}
void erase(int val){//删除一个数
    if(find(val) != NULL)
        erase(root, 1);
}
int rnk(int val){//查询val排在第几
    if(find(val) == NULL) return 0;
    return size(root->son[0])+1;
}
int qry(int th, node* t){//查询排在第th的是哪个数
    if(size(root) < th) return 0;
    if(th > size(t->son[0])) th -= size(t->son[0]);
    else return qry(th, t->son[0]);
    if(th <= t->num) return t->val;
    return qry(th-t->num, t->son[1]);
}
void pre(int val, node* t){
    if(t == NULL) return;
    if(t->val < val) ans=max(ans, t->val);
    if(t->val >= val) pre(val, t->son[0]);
    else pre(val, t->son[1]);
}
void nxt(int val, node* t){
    if(t == NULL) return;
    if(t->val > val) ans=min(ans, t->val);
    if(t->val <= val) nxt(val, t->son[1]);
    else nxt(val, t->son[0]);
}
int main(){
    int n, op, x;
    scanf("%d",&n);
    top = n+10;
    for(int i=1; i<=top; ++i) stk[i] = tree+i;//将节点放进栈里面
    while(n--){
        scanf("%d%d",&op, &x);
        if(op == 1) insert(x);
        else if(op == 2) erase(x);
        else if(op == 3) printf("%d\n",rnk(x));
        else if(op == 4) printf("%d\n",qry(x, root));
        else if(op == 5){
            ans = -inf;
            pre(x, root);
            printf("%d\n",ans);
        }
        else{
            ans = inf;
            nxt(x, root);
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值