前言
听说有一种用 0/1 Trie 写普通平衡树的方法,并且比 treap 快(?),所以学了一下。
struct trie
int cnt=0,son[5000001][2],exist[5000001],siz[5000001],fa[5000001];最基本的定义:
- :点的个数。
- :编号为 的点是否有儿子 。
- :点 的父亲。
- :编号为 的点是多少个二进制数的终点。
插入 / 删除
每个数可以按位拆成二进制(e.g. ),所以可以放在一个 trie 里面。
然后考虑怎么删除。可以对于每个点,记录一个 数组,每在第 个点加一个数,就执行 。如果是删除,那就 。
当然,在按位拆完之后,要把 增加 。
这样就写完了:
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;
}的排名
还是按位拆,如果查到了 ,那肯定比所有 要大,直接加上 就行了。如果是 ,继续就行了。
注意的是,自己也是一个数,所以最后结果要 。
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;
}排名为 的数
跟查询 的排名是一个道理,只是反过来了。
具体来说,如果左子树大小小于排名,就在右子树查,否则在左子树往下找。
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;
}前驱 / 后继
有前面的 ,查前驱 / 后继就很方便了。
我们看一下定义:
- 前驱定义为小于 ,且最大的数。
- 后继定义为大于 ,且最小的数。
想一想,小于 且最大,就是排名刚好比 小 。那大于 且最小,就是排名刚好比 大 吗?
No!因为可能有多个一样的数,所以查一下 的排名(就是比 大的数的排名),然后再把这个数查出来就行了。
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 就好了:
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 的代码也很好实现,但是因为 ,所以 可能为负数,可以统一加 处理,然后输出的时候再减 就行了:
#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:要枚举每一位, 次询问,复杂度 。
ML: 最多是 个(每个数最多有 个 0/1 Trie 节点),空间复杂度 。所以卡了空间的普通平衡树加强版过不了。