伸展树(splay)模板
来源:互联网 发布:建筑模型 知乎 编辑:程序博客网 时间:2024/04/29 17:05
神级数据结构
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 200 + 7;int m, n;struct node { node *left, *right; node *par; int val, w;} *root;void RightRotate(node *x) { node *y = x->par; y->left = x->right; if(x->right) x->right->par = y; x->par = y->par; if(y->par) { if(y->par->left == y) y->par->left = x; else y->par->right = x; } x->right = y; y->par = x;}void LeftRotate(node *x) { node *y = x->par; y->right = x->left; if(x->left) x->left->par = y; x->par = y->par; if(y->par) { if(y->par->left == y) y->par->left = x; else y->par->right = x; } x->left = y; y->par = x;}void splay(node *x, node *y) { while(x->par != y) { if(x->par->par == y) x->par->left == x ? RightRotate(x) : LeftRotate(x); else { if(x->par->par->left == x->par) { if(x->par->left == x) { RightRotate(x); RightRotate(x); } else { LeftRotate(x); RightRotate(x); } } else { if(x->par->left == x) { RightRotate(x); LeftRotate(x); } else { LeftRotate(x); LeftRotate(x); } } } } if(y == 0) root = x;}void Insert(int val) { node *p = root, *y = NULL; int isL = 1; while(true) { if(!p) { p = new node; p->left = p->right = NULL; p->val = val; p->w = 1; p->par = y; if(y) { if(isL) y->left = p; else y->right = p; } splay(p, 0); break; } y = p; if(p->val == val) { p->w++; splay(p, 0); break; } else if(p->val < val) { p = p->right; isL = 0; } else { p = p->left; isL = 1; } }}node *join(node *a, node *b) { if(!a) return b; if(!b) return a; a->par = b->par = 0; node *t = a; while(t->right) t = t->right; splay(t, 0); t->right = b; b->par = t; return t;}void Remove(node *x, int val) { if(x->val == val) { splay(x, 0); root = join(x->left, x->right); return ; } else if(x->val > val) { Remove(x->left, val); } else Remove(x->right, val);}void print(node *d) { if(d->left) print(d->left); if(d->right) print(d->right); printf("%d ", d->val);}int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); scanf("%d", &n); int x; for(int i = 0; i < n; ++i) { scanf("%d", &x); Insert(x); print(root); printf("\n"); } scanf("%d", &m); for(int i = 0; i < m; ++i) { scanf("%d", &x); Remove(root, x); print(root); printf("\n"); } return 0;}
0 0
- Splay伸展树&模板
- 伸展树(splay)模板
- Splay伸展树模板总结
- 伸展树模板小结(Splay Tree)
- 伸展树(SPLAY)个人总结+模板 [平衡树]【数据结构】【模板】
- 伸展树(splay树)
- Splay树(伸展树)
- 【伸展树】[CQBZOJ2803]普通平衡树splay模板
- 【伸展树】[CQBZOJ2803]普通平衡树splay top_down模板
- Splay Tree(伸展树)
- Splay Tree 伸展树
- Splay 伸展树
- 伸展树splay tree
- Splay Tree(伸展树)
- splay tree(伸展树)
- 【数据结构】伸展树 Splay
- 伸展树(Splay tree)
- 伸展树(Splay tree)
- 在Windows下使用cl编译器编译生成简单的DLL例程
- HDU-ACM-1097
- to write ...
- 51Nod 1138 连续整数的和(数学、规律)
- 转自MySQL官网的企业版的防火墙的一篇博客
- 伸展树(splay)模板
- POJ 2100 Graveyard Design(尺取法)
- org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted o
- XAMPP启动MySQL报错
- iteraotr的hasNext和next方法除了异常NoSuchElementException
- GeoHadoop 之 空间抽样(一)
- 调试程序心得-2(提高程序执行效率)
- Java中使用SQL标签库<sql:setDataSource />设置数据源
- 如何判断字符是全角还是半角