HYSBZ 3224 Tyvj 1728 普通平衡树 splay模版

先学了splay写的 以后有空再学treap和sbt版

参考: http://blog.csdn.net/clove_unique/article/details/50630280

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define lson i<<1
#define rson i<<1|1
using namespace std;
const int N=1e5+5;
int f[N],ch[N][2],sz[N],cnt[N],key[N];
int tot,root;
inline void Clear(int x)
{
    ch[x][0]=ch[x][1]=f[x]=sz[x]=cnt[x]=key[x]=0;
}
inline int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    if (x)
    {
        sz[x]=cnt[x];
        if (ch[x][0]) sz[x]+=sz[ch[x][0]];
        if (ch[x][1]) sz[x]+=sz[ch[x][1]];
    }
}
void Rotate(int x)
{
    int fa=f[x],ff=f[fa],kind=get(x);
    ch[fa][kind]=ch[x][kind^1];f[ch[x][kind^1]]=fa;
    ch[x][kind^1]=fa;f[fa]=x;
    f[x]=ff;
    if (ff)
        ch[ff][ch[ff][1]==fa]=x;
    update(fa);
    update(x);
}
void splay(int x)
{
    for(int fa;(fa=f[x]);Rotate(x))
        if (f[fa])
            Rotate((get(x)==get(fa)?fa:x));
    root=x;
}
void Insert(int x)
{
    if (root==0)
    {
        tot++;
        ch[tot][0]=ch[tot][1]=f[tot]=0;
        sz[tot]=cnt[tot]=1;
        key[tot]=x;
        root=tot;
        return;
    }
    int now=root,fa=0;
    while(1)
    {
        if (x==key[now])
        {
            cnt[now]++;
            update(now);
            update(fa);
            splay(now);
            return;
        }
        fa=now;
        now=ch[now][key[now]<x];
        if (now==0)
        {
            tot++;
            ch[tot][0]=ch[tot][1]=0;
            f[tot]=fa;
            sz[tot]=cnt[tot]=1;
            ch[fa][key[fa]<x]=tot;
            key[tot]=x;
            update(fa);
            splay(tot);
            return;
        }
    }
}
int Find(int x)
{
    int now=root,ans=0;
    while(1)
    {
        if (x<key[now])
            now=ch[now][0];
        else
        {
            ans+=(ch[now][0])?sz[ch[now][0]]:0;
            if (x==key[now])
            {
                splay(now);
                return ans+1;
            }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
int Findx(int x)
{
    int now=root;
    while(1)
    {
        if (ch[now][0]&&x<=sz[ch[now][0]])
            now=ch[now][0];
        else
        {
            int tem=ch[now][0]?sz[ch[now][0]]:0;
            tem+=cnt[now];
            if (x<=tem) return key[now];
            x-=tem;now=ch[now][1];
        }
    }
}
int pre()
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}
int nextt()
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}
void del(int x)
{
    Find(x);
    if (cnt[root]>1)
    {
        cnt[root]--;
        return;
    }
    if (!ch[root][0]&&!ch[root][1])
    {
        Clear(root);
        root=0;
        return;
    }
    if (!ch[root][0])
    {
        int oldroot=root;
        root=ch[root][1];
        f[root]=0;
        Clear(oldroot);
        return;
    }
    else if (!ch[root][1])
    {
        int oldroot=root;
        root=ch[root][0];
        f[root]=0;
        Clear(oldroot);
        return;
    }
    int leftbig=pre(),oldroot=root;
    splay(leftbig);
    f[ch[oldroot][1]]=root;//因为提上来的是最靠近的数,所以不会产生左子树
    ch[root][1]=ch[oldroot][1];
    Clear(oldroot);
    update(root);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        tot=root=0;
        int opt,x;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&opt,&x);
            switch(opt)
            {
                case 1:Insert(x);break;
                case 2:del(x);break;
                case 3:printf("%d\n",Find(x));break;
                case 4:printf("%d\n",Findx(x));break;
                case 5:Insert(x);printf("%d\n",key[pre()]);del(x);break;
                case 6:Insert(x);printf("%d\n",key[nextt()]);del(x);break;
            }
        }
    }
    return 0;
}

  

时间: 2024-08-24 12:00:43

HYSBZ 3224 Tyvj 1728 普通平衡树 splay模版的相关文章

BZOJ 3224 Tyvj 1728 普通平衡树 | Splay 板子

下面给出Splay的实现方法(复杂度证明什么的知道是 nlogn 就可以啦) 首先对于一颗可爱的二叉查找树,是不能保证最坏nlogn的复杂度(可以想象把一个升序序列插入) 所以我们需要一些非常巧妙的旋转操作 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #define N 100010 6 #define which(x) (ls[fa[(x)

bzoj 3224: Tyvj 1728 普通平衡树.

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 15114  Solved: 6570[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为

3224: Tyvj 1728 普通平衡树(新板子)

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17048  Solved: 7429[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为

BZOJ 3224: Tyvj 1728 普通平衡树 treap

3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为小于x,且最大的数)6. 求x的后继(后继定义为大于x,且最小的数) Input 第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

3224: Tyvj 1728 普通平衡树

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2566  Solved: 1031[Submit][Status] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为小于x,且最大的数)

BZOJ 3224: Tyvj 1728 普通平衡树(BST)

treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cstdlib> #define rep(i,n) for(int i=0;i<n;

BZOJ 题目3224: Tyvj 1728 普通平衡树(SBT)

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4350  Solved: 1769 [Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相同的数,因输出最小的排名) 4. 查询排名为x的数 5. 求x的前驱

【BZOJ3224】Tyvj 1728 普通平衡树 Splay

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每行输

bzoj 3224: Tyvj 1728 普通平衡树 &amp;&amp; loj 104 普通平衡树 (splay树)

题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 思路: splay树模板题: 推荐博客:https://blog.csdn.net/clove_unique/article/details/50630280 b站上splay树的讲解视频也可以看下,讲的很好,推荐看完视频了解了splay的原理再写 实现代码: #include<bits/stdc++.h> using namespace std; #define ll lo