bzoj 3224 Tyvj 1728 普通平衡树 [Splay]

本文介绍了一种普通平衡树数据结构的实现方法,该结构能够高效地进行插入、删除、查找等操作,并支持查询元素排名、前驱和后继等功能。

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

普通平衡树

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 8170 Solved: 3442

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.每个数的数据范围:[-1e7,1e7]


就是一道平衡树裸题。。然而细节太多了!!!! MDZZ!!
size 和 cnt 的维护一不小心就出错,然后求pre next 比较简单的方法就是插入再删除,比较好想。。。。
改成了非递归写法,splay的节点方便确定,并且splay过程中自带update功能!

从8.2一直WA到8.4!!!!!还不乏RE CE TLE 怎么不上天呢?
这里写图片描述
中间3223不管。。 然后前面AC的是网上的程序。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 100005;

struct Node
{
    int val;
    int size;
    int cnt;
    int ch[2];
    int fa;
}node[maxn];
int root;
int maxnode;
#define val(x) node[x].val
#define size(x) node[x].size
#define cnt(x) node[x].cnt
#define fa(x) node[x].fa
#define ch(x,d) node[x].ch[d]
inline void update(int x)
{
    size(x) = cnt(x);
    int l=ch(x,0) , r=ch(x,1);
    if(l) size(x) += size(l);
    if(r) size(x) += size(r);
}
inline void rotate(int x,int &to)
{
    int y=fa(x),z=fa(y);
    int l= ch(y,1)==x , r=l^1;
    if(y==to) to = x;
    else ch(z,ch(z,1)==y) = x;
    fa(ch(x,r))=y; fa(y)=x; fa(x)=z;
    ch(y,l)=ch(x,r); ch(x,r)=y;
    update(y); update(x);
}
void splay(int x,int &to)
{
    while(x^to)
    {
        int y=fa(x),z=fa(y);
        if(y^to)
            if(ch(y,0)==x ^ ch(z,0)==y) rotate(x,to);
            else rotate(y,to);
        rotate(x,to);
    }
}
void insert(int key)
{
    if(!root) { root = ++maxnode; val(root)=key; size(root)=cnt(root)=1; return; }
    int x = root , y = 0;
    while(x && val(x)!=key) y=x , x=ch(x,key>val(x));
    if(x) cnt(x)++;
    else
    {
        x = ++maxnode;
        val(x)=key;
        size(x)=cnt(x)=1;
        fa(x)=y;
        if(y) ch(y,key>val(y))=x;
    }
    splay(x,root); // with updating size !!
}
int pre_one(int x) // already at root
{
    int rt = ch(x,0);
    while(ch(rt,1)) rt = ch(rt,1);
    return rt;
}
int next_one(int x)
{
    int rt = ch(x,1);
    while(ch(rt,0)) rt = ch(rt,0);
    return rt;
}
int find(int key) // provided x exists!
{
    int x = root;
    while(val(x)^key) x = ch(x,key>val(x));
    return x;
}
void erase(int key)
{
    int x = find(key);
    splay(x,root);
    int l = pre_one(x) , r = next_one(x);
    splay(l,root); splay(r,ch(l,1));
    size(ch(r,0))--; // take care too !!!
    if(--cnt(ch(r,0))==0) ch(r,0)=0; // take care whether ==0 or not !!!
    update(r); update(l);
}
int rank(int key)
{
    int rt = find(key);
    splay(rt,root);
    return size(ch(rt,0))+1;
}
int kth(int x,int k)
{
    int lsize = size(ch(x,0));
    if(lsize<k && k<=lsize+cnt(x)) return x;
    if(k<=lsize) return kth(ch(x,0),k);
    else return kth(ch(x,1),k-lsize-cnt(x));
}
int pre(int key)
{
    insert(key);
    int rt = pre_one(root);
    erase(key);
    return rt;
}
int next(int key)
{
    insert(key);
    int rt = next_one(root);
    erase(key);
    return rt;
}
int main()
{
    freopen("splay.in","r",stdin);
    freopen("splay.out","w",stdout);
    int n;
    scanf("%d",&n);
    insert(-INF); insert(INF);
    int opt,x;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&opt,&x);
        switch(opt)
        {
            case 1: insert(x); break;
            case 2: erase(x); break;
            case 3: printf("%d\n",rank(x)-1); break;
            case 4: printf("%d\n",val(kth(root,x+1))); break;
            case 5: printf("%d\n",val(pre(x))); break;
            case 6: printf("%d\n",val(next(x))); break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值