splay树模版
来源:互联网 发布:帝国cms和苹果cms 编辑:程序博客网 时间:2024/06/06 06:31
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100010;int pre[maxn], ch[maxn][2], sz[maxn], rev[maxn];int root, top1;int val[maxn];void rever(int x){if(!x)return;swap(ch[x][0], ch[x][1]);rev[x] ^= 1;}void pushdown(int x){if(!x)return;if(rev[x]){rever(ch[x][0]);rever(ch[x][1]);rev[x] = 0;}}void pushup(int x){ sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;}void rotate(int x, int d){ int y = pre[x]; pushdown(y); pushdown(x); ch[y][d^1] = ch[x][d]; pre[ch[x][d]] = y; if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x; pre[x] = pre[y]; ch[x][d] = y; pre[y] = x; pushup(y); }void splay(int x, int goal){ pushdown(x); while(pre[x] != goal) { if(pre[pre[x]] == goal) { rotate(x, ch[pre[x]][0] == x); } else { int y = pre[x], z = pre[y]; int d = (ch[z][0] == y); if(ch[y][d] == x) { rotate(x, d^1); rotate(x, d); } else { rotate(y, d); rotate(x, d); } } } pushup(x); if(goal == 0) root = x;}void NewNode(int &x, int f, int c){ x = ++top1; ch[x][0] = ch[x][1]; sz[x] = 1; pre[x] = f;val[x] = c;rev[x] = 0;}void build(int &x, int l, int r, int f){ if(l > r) return; int m = (l + r) >> 1; NewNode(x, f, m); build(ch[x][0], l, m-1, x); build(ch[x][1], m+1, r, x); pushup(x);}void init(int n){ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;rev[0] = val[0] = 0;root = top1 = 0;NewNode(root, 0, -1);NewNode(ch[root][1], root, -1);sz[root] = 2;build(ch[ch[root][1]][0], 1, n, ch[root][1]);pushup(ch[root][1]);pushup(root);}int kth(int x, int k){pushdown(x);if(!x)return 0;int s = sz[ch[x][0]];if(k == s+1)return x;if(k <= s)return kth(ch[x][0], k);return kth(ch[x][1], k-s-1);}void update(int l, int r, int n){splay(kth(root, l), 0);splay(kth(root, r+2), root);rever(ch[ch[root][1]][0]);int temp = ch[ch[root][1]][0];ch[ch[root][1]][0] = 0;pushup(ch[root][1]);pushup(root);splay(kth(root, n+1), 0);int m = get_min(ch[root][1]);splay(m, root);ch[ch[root][1]][0] = temp;pre[ch[ch[root][1]][0]] = ch[root][1];pushup(ch[root][1]);pushup(root);}int get_min(int x){if(!x)return 0;while(ch[x][0]){x = ch[x][0];}return x;}int get_max(int x){if(!x)return 0;while(ch[x][1]){x = ch[x][1];}return x;}void remove(){if(!ch[root][0]){root = ch[root][1];pre[root] = 0;}else{int m = get_max(ch[root][0]);splay(m, root);ch[m][1] = ch[root][1];pre[ch[root][1]] = m;root = m;pre[root] = 0;}}void insert(int x, int k, int p){if(x == 0){NewNode(root, 0, k, p);return;}while(ch[x][val[x]<p])x = ch[x][val[x]<p];NewNode(ch[x][val[x]<p], x, k, p);splay(ch[x][val[x]<p], 0);}void print(int x, int n){if(!x)return;pushdown(x);print(ch[x][0], n);if(val[x] >= 1 && val[x]<= n){printf("%d\n", val[x]);}print(ch[x][1], n);}
0 0
- splay树模版
- Splay树模版
- splay模版
- splay模版
- Splay 模版
- splay的模版
- HNOI 2000 Splay Tree 模版题目
- [BZOJ 1588]营业额统计 && splay部分模版
- 自己的splay模版(bzoj1588)
- Splay——kuangnin的模版
- SPLAY树
- Splay树
- splay树
- splay树
- Splay 树
- splay树
- Splay树
- SPLAY树
- vim配置方案
- oracle 查看 用户,用户权限,用户表空间,用户默认表空间
- JAVA ee面试主要问的问题
- C# 事件机制
- LaTeX新人教程,30分钟从完全陌生到基本入门
- splay树模版
- JS实现的浏览器打印代码
- Embed dll Files Within an exe (C# WinForms)—Winform 集成零散dll进exe的方法
- java的map存入null值
- Android string.xml 显示特殊符号
- 单链表的反转
- ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限
- sublime text 开启正则表达式查找
- 雷吉-杰克逊:想成为最佳球员 中国球迷最爱雷霆