Treap树模板
来源:互联网 发布:阿里云系统沙发管家 编辑:程序博客网 时间:2024/05/07 20:52
Treap包含了二叉查找树和堆的特性,而且性价比很高!
#include <iostream>#include <algorithm>#include <cstdio>#include <vector>#include <cstring>using namespace std;const int INF = ~0U >> 1;struct node { node *left, *right; int val, fix;};struct node *root;void LeftRotate(node *& a) { node *b = a->right; a->right = b->left; b->left = a; a = b;}void RightRotate(node *& a) { node *b = a->left; a->left = b->right; b->right = a; a = b;}void Insert(node *&p, int val) { if(p == NULL) { p = new node; p->left = p->right = NULL; p->val = val; p->fix = rand(); } else if(p->val > val) { Insert(p->left, val); if(p->fix > p->left->fix) RightRotate(p); } else { Insert(p->right, val); if(p->fix > p->right->fix) LeftRotate(p); }}void Delete(node *&p, int val) { if(p->val == val) { if(!p->left || !p->right) { node *t = p; if(!p->left) p = p->right; else p = p->left; delete t; } else { if(p->right->fix > p->left->fix) { RightRotate(p); Delete(p->right, val); } else { LeftRotate(p); Delete(p->left, val); } } } else if(p->val > val) { Delete(p->left, val); } else Delete(p->right, val);}void print(node *a) { if(a->left) print(a->left); if(a->right) print(a->right); printf("%d ", a->val); return ;}int main() { int m, n, x; scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", &x); Insert(root, x); print(root); printf("\n"); } scanf("%d", &m); for(int i = 0; i < m; ++i) { scanf("%d", &x); Delete(root, x); print(root); printf("\n"); }}
0 0
- Treap树模板
- [平衡树模板]Treap
- [模板]平衡树treap
- 【模板】普通平衡树 Treap
- 【Treap】Treap模板
- Treap模板+Treap介绍。。。
- Treap模板
- 【Treap模板】
- (模板)treap
- treap 模板
- Treap模板
- treap 模板
- treap模板
- Treap 模板
- 【模板】Treap
- treap模板
- Treap模板
- Treap 模板
- 198. House Robber
- Codeforces Round #274 (Div. 2) C. Exams (贪心)
- php中常用的排序方法
- 基于OpenWrt的PPTP服务器
- eclipse java web 更换项目外部调用名称
- Treap树模板
- python出现Non-ASCII character '\xe7' in file ex6.py on line 1, but no encoding declare错误
- 搭建分布式架构1--CentOs下安装jdk7(环境准备)
- Powershell指令集_2
- Powershell 音乐播放
- 关于VS编译兼容XP的那点事(转)
- 键盘记录器
- c++异常
- 【Java】深夜代码祭(1)