平衡树——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
无旋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号