#include <cstdio>
#include <cstdlib>
#define M 1024
using namespace std;
struct Treap{
int l,r,key,fix;
}T[M];
int size,root;
void L(int &x){
int y=T[x].r;
T[x].r=T[y].l;
T[y].l=x;
x=y;
}//×óÐý
void R(int &x){
int y=T[x].l;
T[x].l=T[y].r;
T[y].r=x;
x=y;
}//ÓÒÐý
void insert(int &k,int tkey){
if(k==-1){
k=size++;
T[k].l=T[k].r=-1;
T[k].key=tkey;
T[k].fix=rand();
}
else
if(tkey<T[k].key){
insert(T[k].l,tkey);
if(T[T[k].l].fix>T[k].fix) R(k);
}else{
insert(T[k].r,tkey);
if(T[T[k].r].fix>T[k].fix) L(k);
}
} //insert
void remove(int &k,int tkey){
if(k==-1) return;
if(tkey<T[k].key) remove(T[k].l,tkey);
else
if(tkey>T[k].key) remove(T[k].r,tkey);
else{
if(T[k].l==-1&&T[k].r==-1) k=-1;
else if(T[k].l==-1) k=T[k].r;
else if(T[k].r==-1) k=T[k].l;
else
if(T[T[k].l].fix<T[T[k].r].fix){
L(k);
remove(T[k].l,tkey);
}
else{
R(k);
remove(T[k].r,tkey);
}
}
}//remove
int main(){
return 0;
}
参考:
http://www.nocow.cn/index.php/Treap
http://www.nocow.cn/index.php/Treap_C%2B%2B