跳到正文

算法·理论

用 0/1 Trie 写普通平衡树

Comet387约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟

版本

  1. v1当前

前言

听说有一种用 0/1 Trie 写普通平衡树的方法,并且比 treap 快(?),所以学了一下。

struct trie

C++
int cnt=0,son[5000001][2],exist[5000001],siz[5000001],fa[5000001];

最基本的定义:

  • cnt\text{cnt}:点的个数。
  • sonx,y\text{son}_{x,y}:编号为 xx 的点是否有儿子 yy。
  • fax\text{fa}_{x}:点 xx 的父亲。
  • existx\text{exist}_x:编号为 xx 的点是多少个二进制数的终点。

插入 / 删除

每个数可以按位拆成二进制(e.g. 114514→11011111101010010114514 \rightarrow \texttt{11011111101010010}),所以可以放在一个 trie 里面。

然后考虑怎么删除。可以对于每个点,记录一个 siz\text{siz} 数组,每在第 xx 个点加一个数,就执行 sizx←sizx+1\text{siz}_x\leftarrow \text{siz}_x+1。如果是删除,那就 −1-1。

当然,在按位拆完之后,要把 existx\text{exist}_x 增加 11。

这样就写完了:

C++
inline void emplace(int x,int offset)
// offset:加的 x 的个数
// 例如平衡树板子,offset=1 就插入,offset=-1 就删除
// 对于一次插入 / 删除多个数,也很方便
{
    int pos=0;
    req(i,30,0)
    {
        int digit=(x>>i)&1; // 这一位
        if(son[pos][digit]) pos=son[pos][digit];
        else
        {
            // 没有节点,新建
            ++cnt;
            son[pos][digit]=cnt;
            fa[cnt]=pos;
            pos=cnt;
        }
        siz[pos]+=offset;
    }
    exist[pos]+=offset;
}

x\bm x 的排名

还是按位拆,如果查到了 xxxxxx1\tt{xxxxxx1},那肯定比所有 xxxxxx0xxxxxx...\tt{xxxxxx0xxxxxx...} 要大,直接加上 siz\text{siz} 就行了。如果是 xxxxxx0\tt{xxxxxx0},继续就行了。

注意的是,自己也是一个数,所以最后结果要 −1-1。

C++
inline int order_of_key(int x)
{
    int pos=0,res=0;
    req(i,30,0)
    {
        int digit=(x>>i)&1;
        if(digit) res+=siz[son[pos][0]]; // 这一位是 1,把树左子树的大小加上
        pos=son[pos][digit];
        if(!pos) break;
    }
    return res+1;
}

排名为 x\bm x 的数

跟查询 xx 的排名是一个道理,只是反过来了。

具体来说,如果左子树大小小于排名,就在右子树查,否则在左子树往下找。

C++
inline int find_by_order(int x)
{
    int pos=0,res=0;
    req(i,30,0)
    {
        if(x<=siz[son[pos][0]]) pos=son[pos][0];
        else x-=siz[son[pos][0]],pos=son[pos][1],res|=1<<i;
        if(!pos) break;
    }
    return res;
}

前驱 / 后继

有前面的 order_of_key / find_by_order\tt{order\_of\_key\ /\ find\_by\_order},查前驱 / 后继就很方便了。

我们看一下定义:

  • 前驱定义为小于 xx,且最大的数。
  • 后继定义为大于 xx,且最小的数。

想一想,小于 xx 且最大,就是排名刚好比 xx 小 11。那大于 xx 且最小,就是排名刚好比 xx 大 11 吗?

No!因为可能有多个一样的数,所以查一下 x+1x+1 的排名(就是比 xx 大的数的排名),然后再把这个数查出来就行了。

C++
inline int prev(int x)
{
    return find_by_order(order_of_key(x)-1);
}
inline int next(int x)
{
    return find_by_order(order_of_key(x+1));
}

代码

所以整个 struct trie 就好了:

C++
struct trie
{
    int cnt=0,son[5000001][2],exist[5000001],siz[5000001],fa[5000001];
    inline void emplace(int x,int offset)
    {
        int pos=0;
        req(i,30,0)
        {
            int digit=(x>>i)&1;
            if(son[pos][digit]) pos=son[pos][digit];
            else
            {
                ++cnt;
                son[pos][digit]=cnt;
                fa[cnt]=pos;
                pos=cnt;
            }
            siz[pos]+=offset;
        }
        exist[pos]+=offset;
    }
    inline int order_of_key(int x)
    {
        int pos=0,res=0;
        req(i,30,0)
        {
            int digit=(x>>i)&1;
            if(digit) res+=siz[son[pos][0]];
            pos=son[pos][digit];
            if(!pos) break;
        }
        return res+1;
    }
    inline int find_by_order(int x)
    {
        int pos=0,res=0;
        req(i,30,0)
        {
            if(x<=siz[son[pos][0]]) pos=son[pos][0];
            else x-=siz[son[pos][0]],pos=son[pos][1],res|=1<<i;
            if(!pos) break;
        }
        return res;
    }
    inline int prev(int x)
    {
        return find_by_order(order_of_key(x)-1);
    }
    inline int next(int x)
    {
        return find_by_order(order_of_key(x+1));
    }
};
trie tr;

P3369 的代码也很好实现,但是因为 x≤∣107∣x\leq|10^7|,所以 xx 可能为负数,可以统一加 10710^7 处理,然后输出的时候再减 10710^7 就行了:

C++
#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf,ubuf[1<<23],*u=ubuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
int n,q,opt,x,ans;
struct trie
{
    int cnt=0,son[5000001][2],exist[5000001],siz[5000001],fa[5000001];
    inline void emplace(int x,int offset)
    {
        int pos=0;
        req(i,30,0)
        {
            int digit=(x>>i)&1;
            if(son[pos][digit]) pos=son[pos][digit];
            else
            {
                ++cnt;
                son[pos][digit]=cnt;
                fa[cnt]=pos;
                pos=cnt;
            }
            siz[pos]+=offset;
        }
        exist[pos]+=offset;
    }
    inline int order_of_key(int x)
    {
        int pos=0,res=0;
        req(i,30,0)
        {
            int digit=(x>>i)&1;
            if(digit) res+=siz[son[pos][0]];
            pos=son[pos][digit];
            if(!pos) break;
        }
        return res+1;
    }
    inline int find_by_order(int x)
    {
        int pos=0,res=0;
        req(i,30,0)
        {
            if(x<=siz[son[pos][0]]) pos=son[pos][0];
            else x-=siz[son[pos][0]],pos=son[pos][1],res|=1<<i;
            if(!pos) break;
        }
        return res;
    }
    inline int prev(int x)
    {
        return find_by_order(order_of_key(x)-1);
    }
    inline int next(int x)
    {
        return find_by_order(order_of_key(x+1));
    }
};
trie tr;
signed main()
{
    read(q);
    while(q--)
    {
        read(opt,x);
        x+=10000000;
        if(opt==1) tr.emplace(x,1);
        if(opt==2) tr.emplace(x,-1);
        if(opt==3) writeln(tr.order_of_key(x));
        if(opt==4) writeln(tr.find_by_order(x-10000000)-10000000);
        if(opt==5) writeln(tr.prev(x)-10000000);
        if(opt==6) writeln(tr.next(x)-10000000);
    }
	return 0;
}

复杂度

TL:要枚举每一位,qq 次询问,复杂度 O(qlog⁡(max⁡i∈number_seti))\mathcal O(q\log (\max\limits_{i\in\text{number\_set}}i))。

ML:cnt\text{cnt} 最多是 min⁡{值域log⁡值域,nlog⁡值域}\min\{\small{值域}\normalsize\log\small{值域}\normalsize{,n\log \small{值域}}\} 个(每个数最多有 log⁡值域\log\small{值域} 个 0/1 Trie 节点),空间复杂度 O(cnt)\mathcal O(\text{cnt})。所以卡了空间的普通平衡树加强版过不了。

原文