平衡树——treap
有旋treap
#include<bits/stdc++.h>
using namespace std;
#define inf 2000000005
int tot=0,root=0;
struct jade
{
int size,val,cnt,rd,son[2];
}t[100010];
void pushup(int x)
{
t[x].size=t[t[x].son[0]].size+t[t[x].son[1]].size+t[x].cnt;
}
void rotate(int &x,int k)
{
int y=t[x].son[k^1];
t[x].son[k^1]=t[y].son[k];
t[y].son[k]=x;
pushup(x);
pushup(y);
x=y;
}
void add(int &ro,int x)
{
if(!ro)
{
tot++;
ro=tot;
t[ro].size=t[ro].cnt=1;
t[ro].val=x;
t[ro].rd=rand();
return ;
}
if(t[ro].val==x)
{
t[ro].cnt++;
t[ro].size++;
return ;
}
int k=(x>t[ro].val);
add(t[ro].son[k],x);
if(t[ro].rd<t[t[ro].son[k]].rd)
{
rotate(ro,k^1);
}
pushup(ro);
}
void del(int &ro,int x)
{
if(!ro)
{
return ;
}
if(x<t[ro].val)
{
del(t[ro].son[0],x);
}
else if(x>t[ro].val)
{
del(t[ro].son[1],x);
}
else
{
if(!t[ro].son[0]&&!t[ro].son[1])
{
t[ro].cnt--;
t[ro].size--;
if(t[ro].cnt==0)
{
ro=0;
}
}
else if(t[ro].son[0]&&!t[ro].son[1])
{
rotate(ro,1);
del(t[ro].son[1],x);
}
else if(!t[ro].son[0]&&t[ro].son[1])
{
rotate(ro,0);
del(t[ro].son[0],x);
}
else if(t[ro].son[0]&&t[ro].son[1])
{
int k=(t[t[ro].son[0]].rd>t[t[ro].son[1]].rd);
rotate(ro,k);
del(t[ro].son[k],x);
}
}
pushup(ro);
}
int read_k(int ro,int x)
{
if(!ro)
{
return 1;
}
if(t[ro].val==x)
{
return t[t[ro].son[0]].size+1;
}
if(t[ro].val<x)
{
return t[t[ro].son[0]].size+t[ro].cnt+read_k(t[ro].son[1],x);
}
if(t[ro].val>x)
{
return read_k(t[ro].son[0],x);
}
return 0;
}
int kth(int ro,int x)
{
if(!ro)
{
return 1;
}
if(t[t[ro].son[0]].size>=x)
{
return kth(t[ro].son[0],x);
}
else if(t[t[ro].son[0]].size+t[ro].cnt<x)
{
return kth(t[ro].son[1],x-t[ro].cnt-t[t[ro].son[0]].size);
}
else
{
return t[ro].val;
}
}
int qq(int ro,int x)
{
if(!ro)
{
return -inf;
}
if(t[ro].val>=x)
{
return qq(t[ro].son[0],x);
}
else
{
return max(t[ro].val,qq(t[ro].son[1],x));
}
}
int hj(int ro,int x)
{
if(!ro)
{
return inf;
}
if(t[ro].val<=x)
{
return hj(t[ro].son[1],x);
}
else
{
return min(t[ro].val,hj(t[ro].son[0],x));
}
}
void work()
{
int opt,x;
cin>>opt>>x;
if(1==2)
{
}
else if(opt==1)
{
add(root,x);
}
else if(opt==2)
{
del(root,x);
}
else if(opt==3)
{
cout<<read_k(root,x)<<endl;
}
else if(opt==4)
{
cout<<kth(root,x)<<endl;
}
else if(opt==5)
{
cout<<qq(root,x)<<endl;
}
else if(opt==6)
{
cout<<hj(root,x)<<endl;
}
return ;
}
int main()
{
int n;
cin>>n;
while(n--)
{
work();
}
return 0;
}
无旋treap
#include<bits/stdc++.h>
using namespace std;
mt19937 zyx(20100219);
struct jade
{
int size,val,key,l,r;
}t[1100010];
int n,root,tot;
int init(int v)
{
tot++;
t[tot].key=zyx();
t[tot].size=1;
t[tot].val=v;
return tot;
}
void pushup(int ro)
{
t[ro].size=1+t[t[ro].l].size+t[t[ro].r].size;
}
void split(int ro,int v,int &x,int &y)
{
if(!ro)
{
x=0;
y=0;
return ;
}
if(t[ro].val<=v)
{
x=ro;
split(t[x].r,v,t[x].r,y);
}
else
{
y=ro;
split(t[y].l,v,x,t[y].l);
}
pushup(ro);
}
int merge(int x,int y)
{
if(!x||!y)
{
return x+y;
}
if(t[x].key<t[y].key)
{
t[x].r=merge(t[x].r,y);
pushup(x);
return x;
}
else
{
t[y].l=merge(x,t[y].l);
pushup(y);
return y;
}
}
void add(int v)
{
int x,y,z;
split(root,v,x,y);
z=init(v);
root=merge(merge(x,z),y);
}
void del(int v)
{
int x,y,z;
split(root,v,x,y);
split(x,v-1,x,z);
z=merge(t[z].l,t[z].r);
root=merge(merge(x,z),y);
}
int get_k(int ro,int k)
{
if(k<=t[t[ro].l].size)
{
return get_k(t[ro].l,k);
}
else if(k>t[t[ro].l].size+1)
{
return get_k(t[ro].r,k-t[t[ro].l].size-1);
}
else
{
return ro;
}
}
int get_k_pre(int v)
{
int x,y;
split(root,v-1,x,y);
int res=get_k(x,t[x].size);
root=merge(x,y);
return res;
}
int get_k_suc(int v)
{
int x,y;
split(root,v,x,y);
int res=get_k(y,1);
root=merge(x,y);
return res;
}
int kth(int k)
{
int ro=get_k(root,k);
return t[ro].val;
}
int read_k(int v)
{
int x,y;
split(root,v-1,x,y);
int res=t[x].size+1;
root=merge(x,y);
return res;
}
void solve()
{
int op,x;
cin>>op>>x;
if(op==1)
{
add(x);
}
if(op==2)
{
del(x);
}
if(op==3)
{
add(x);
cout<<read_k(x)<<'\n';
del(x);
}
if(op==4)
{
cout<<kth(x)<<'\n';
}
if(op==5)
{
cout<<t[get_k_pre(x)].val<<'\n';
}
if(op==6)
{
cout<<t[get_k_suc(x)].val<<'\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
while(n--)
{
solve();
}
return 0;
}
Zvelig1205的FHQ-treap
万万没想到的FHQ-treap
sea-and-sky的FHQ-treap
序列操作:
P3391 【模板】文艺平衡树
#include<bits/stdc++.h>
using namespace std;
int n,m,root,tot;
struct jade
{
int rd,size,val,son[2],tag;
}t[100010];
void pushup(int x)
{
t[x].size=t[t[x].son[0]].size+t[t[x].son[1]].size+1;
}
int build(int v)
{
tot++;
int x=tot;
t[x].val=v;
t[x].size=1;
t[x].rd=rand();
return tot;
}
void pushdown(int x)
{
if(t[x].tag!=0)
{
swap(t[x].son[0],t[x].son[1]);
if(t[x].son[0])
{
t[t[x].son[0]].tag^=1;
}
if(t[x].son[1])
{
t[t[x].son[1]].tag^=1;
}
t[x].tag=0;
}
}
int merge(int x,int y)
{
if(!x||!y)
{
return x+y;
}
if(t[x].rd<t[y].rd)
{
pushdown(x);
t[x].son[1]=merge(t[x].son[1],y);
pushup(x);
return x;
}
pushdown(y);
t[y].son[0]=merge(x,t[y].son[0]);
pushup(y);
return y;
}
void split(int i,int k,int &x,int &y)
{
if(!i)
{
x=y=0;
return ;
}
pushdown(i);
if(t[t[i].son[0]].size<k)
{
x=i;
split(t[i].son[1],k-t[t[i].son[0]].size-1,t[i].son[1],y);
}
else
{
y=i;
split(t[i].son[0],k,x,t[i].son[0]);
}
pushup(i);
}
void cout_t(int i)
{
if(!i)
{
return ;
}
pushdown(i);
cout_t(t[i].son[0]);
cout<<t[i].val<<" ";
cout_t(t[i].son[1]);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
root=merge(root,build(i));
}
for(int i=1;i<=m;i++)
{
int l,r,a,b,c;
cin>>l>>r;
split(root,l-1,a,b);
split(b,r-l+1,b,c);
t[b].tag^=1;
root=merge(a,merge(b,c));
}
cout_t(root);
return 0;
}
以下是签名
${\scr {jade }}$ ${\scr {seek }}$
本文来自博客园,作者:BIxuan—玉寻,转载请注明原文链接:https://www.cnblogs.com/zhangyuxun100219/p/18979248

浙公网安备 33010602011771号