Treap

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct T{
	int l,r,f;
	int w,z;
}a[100000+10];
int root,cnt;
void r_rot(int x,int y){
	int t=a[y].f;
	a[y].l=a[x].r;
	a[a[x].r].f=y;
	a[x].r=y;
	a[y].f=x;
	if(a[t].l==y)a[t].l=x;
	else a[t].r=x;
	a[x].f=t;
}
void l_rot(int x,int y){
	int t=a[y].f;
	a[y].r=a[x].l;
	a[a[x].l].f=y;
	a[x].l=y;
	a[y].f=x;
	if(a[t].l==y)a[t].l=x;
	else a[t].r=x;
	a[x].f=t;
}
void insert(int u,int k){
	int p;
	if(!u)return ;
	if(a[u].z<k){
		if(!a[u].r){
			a[++cnt].f=u;
			a[cnt].w=rand();
			a[u].r=cnt;
			a[cnt].z=k;
			p=cnt;
			while(a[p].f!=0&&a[p].w>=a[a[p].f].w){
				if(p==a[a[p].f].l)r_rot(p,a[p].f);
				else l_rot(p,a[p].f);
			}
			if(a[p].f==0)root=p;
		}
		else insert(a[u].r,k);
	}
	else if(a[u].z>=k){
		if(!a[u].l){
			a[++cnt].f=u;
			a[cnt].w=rand();
			a[u].l=cnt;
			a[cnt].z=k;
			p=cnt;
			while(a[p].f!=0&&a[p].w>=a[a[p].f].w){
				if(p==a[a[p].f].l)r_rot(p,a[p].f);
				else l_rot(p,a[p].f);
			}
			if(a[p].f==0)root=p;
		}
		else insert(a[u].l,k);
	}
}
void out(int n){
	if(!n)return ;
	out(a[n].l);
	printf("%d ",a[n].z);
	out(a[n].r);
}
int main(){
	int i,j,k,m,n;
	scanf("%d",&n);
	scanf("%d",&k);
	srand(4225);
	a[1].z=k;
	a[1].f=0;
	a[1].w=rand();
	root=1;
	cnt=1;
	for(i=2;i<=n;i++){
		scanf("%d",&k);
		insert(root,k);
	}
	out(root);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是Treap的代码: ``` #include <bits/stdc++.h> using namespace std; struct Node { int key, priority; Node *left, *right; Node(int key, int priority) : key(key), priority(priority), left(NULL), right(NULL) {} }; struct Treap { Node *root; Treap() : root(NULL) {} void rotateRight(Node *&p) { Node *q = p->left; p->left = q->right; q->right = p; p = q; } void rotateLeft(Node *&p) { Node *q = p->right; p->right = q->left; q->left = p; p = q; } void insert(Node *&p, int key, int priority) { if (p == NULL) { p = new Node(key, priority); return; } if (key < p->key) { insert(p->left, key, priority); if (p->priority < p->left->priority) { rotateRight(p); } } else { insert(p->right, key, priority); if (p->priority < p->right->priority) { rotateLeft(p); } } } void remove(Node *&p, int key) { if (p == NULL) { return; } if (key == p->key) { if (p->left == NULL || p->right == NULL) { Node *q = p; if (p->left == NULL) { p = p->right; } else { p = p->left; } delete q; } else { if (p->left->priority > p->right->priority) { rotateRight(p); remove(p->right, key); } else { rotateLeft(p); remove(p->left, key); } } } else if (key < p->key) { remove(p->left, key); } else { remove(p->right, key); } } bool search(Node *p, int key) { if (p == NULL) { return false; } if (key == p->key) { return true; } if (key < p->key) { return search(p->left, key); } else { return search(p->right, key); } } }; int main() { Treap t; t.insert(t.root, 5, rand()); t.insert(t.root, 3, rand()); t.insert(t.root, 8, rand()); t.insert(t.root, 1, rand()); t.insert(t.root, 4, rand()); t.insert(t.root, 6, rand()); t.insert(t.root, 9, rand());

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值