无旋Treap
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int o=2222222;
struct Treap{
struct node{
int l,r;
int size;
int val;
int k;
}t[o];
int tot,root;
int add(int x){
tot++;
t[tot].val=x;
t[tot].k=rand();
t[tot].size=1;
return tot;
}
void pushup(int x){
t[x].size=t[t[x].l].size+t[t[x].r].size+1;
}
void split(int x,int &l,int &r,int v){
if(x==0){
l=r=0;
return ;
}
if(v<t[x].val){
split(t[x].l,l,t[x].l,v);
r=x;
pushup(r);
return;
}
else{
split(t[x].r,t[x].r,r,v);
l=x;
pushup(l);
return;
}
}
int merge(int x,int y){
if(!y||!x){
return x|y;
}
if(t[x].k<t[y].k){
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 pop(int x){
int l,m,r;
split(root,l,r,x);
split(l,l,m,x-1);
m=merge(t[m].l,t[m].r);
r=merge(merge(l,m),r);
}
int kthval(int x,int id){
if(t[t[x].l].size+1==id){
return t[x].val;
}
if(t[t[x].l].size>=id){
return kthval(t[x].l,id);
}
return kthval(t[x].r,id-t[t[x].l].size-1);
}
int rank(int x){
int l=0,r=0,ans=0;
split(root,l,r,x-1);
ans=t[l].size+1;
root=merge(l,r);
return ans;
}
int pre(int x){
int l=0,r=0,ans=0;
split(root,l,r,x-1);
ans=kthval(l,t[l].size);
root=merge(l,r);
return ans;
}
int next(int x){
int l=0,r=0,ans=0;
split(root,l,r,x-1);
ans=kthval(r,1);
root=merge(l,r);
return ans;
}
}T;
int main(){
return 0;
}
Splay
点击查看代码
int root,N,tot;
struct SplayTree{
struct node{
int ch[2];
int fa;
int cnt;
int val;
int son;
}t[o];
void pushup(int x){//infomation
t[x].son=t[t[x].ch[1]].son+t[t[x].ch[0]].son+t[x].cnt;
}
void rotate(int x){//
int y=t[x].fa;
int z=t[y].fa;
int k=t[y].ch[1]==x;
t[z].ch[t[z].ch[1]==y]=x;
t[x].fa=z;
t[y].ch[k]=t[x].ch[k^1];
t[t[x].ch[k^1]].fa=y;
t[x].ch[k^1]=y;
t[y].fa=x;
/*info*/
pushup(y);
pushup(x);
}
void splay(int x,int to){
while(t[x].fa!=to){
int y=t[x].fa;
int z=t[y].fa;
if(z!=to){
if((t[y].ch[0]==x)^(t[z].ch[0]==y)){
rotate(x);
}
else{
rotate(y);
}
}
rotate(x);
}
if(to==0){
root=x;
}
}
void add(int x){
int u=root,fa=0;
while(u&&t[u].val!=x){
fa=u;
u=t[u].ch[x>t[u].val];
}
if(u){
t[u].cnt++;
}
else{
u=++tot;
if(fa){
t[fa].ch[x>t[fa].val]=u;
}
t[tot].ch[0]=0;
t[tot].ch[1]=0;
t[tot].fa=fa;
t[tot].val=x;
t[tot].cnt=1;
t[tot].son=1;
}
splay(u,0);
}
void find(int x){
int u=root;
if(!u){
return ;
}
while(t[u].ch[x>t[u].val]&&x!=t[u].val){
u=t[u].ch[x>t[u].val];
}
splay(u,0);
// return t[t[root].ch[0]].son;
}
int next(int x,int f){
find(x);
int u=root;
if((t[u].val>x&&f)||(t[u].val<x&&!f)){
return u;
}
u=t[u].ch[f];
while(t[u].ch[f^1]){
u=t[u].ch[f^1];
}
return u;
//return t[u].val;
}
void pop(int x){
int l=next(x,0);
int n=next(x,1);
splay(l,0);
splay(n,l);
int del=t[n].ch[0];
if(t[del].cnt>1){
t[del].cnt--;
splay(del,0);
}
else{
t[n].ch[0]=0;
}
}
int rank(int x){
int u=root;
if(t[u].son<x){
return -1;
}
while(1){
int y=t[u].ch[0];
if(x>t[y].son+t[u].cnt){
x-=t[y].son+t[u].cnt;
u=t[u].ch[1];
}
else{
if(t[y].son>=x){
u=y;
}
else{
return t[u].val;
}
}
}
}
}S;
标签:ch,val,int,tot,fa,平衡,root 来源: https://www.cnblogs.com/22222222STL/p/16313036.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。