SPOJ3273.ORDERSET(Treap 模版)
来源:互联网 发布:互联网公司 薪资 知乎 编辑:程序博客网 时间:2024/04/28 11:40
一个空序列,支持过插入一个数,删除一个数,要查询当前序列第 k 大是哪个数,或者比x 小的数有几个。
Treap 模版题
#include <cstdio>#include <cstring>#include <algorithm>#define inf 1500000000using namespace std;typedef long long LL;const int N = 300005;struct treap {int ch[2], val, sz; unsigned int wt;void Set(int vl) { val = vl, wt = rand() * rand() * rand(), sz = 1; }} t[N];int rt, n, tz, q;void update(int x) { t[x].sz = t[t[x].ch[0]].sz + t[t[x].ch[1]].sz + 1; }void rot(int &x, bool ty) { int y = t[x].ch[ty]; t[x].ch[ty] = t[y].ch[!ty], t[y].ch[!ty] = x;update(x), update(y), x = y; }void add(int &x, int val) {if (!x) { t[x = ++ tz].Set(val); return ; }if (t[x].val == val) return ;bool ty = val > t[x].val;add (t[x].ch[ty], val);(t[t[x].ch[ty]].wt > t[x].wt) ? rot(x, ty) : update(x);}void del(int &x, int val) {if (!x) return ;if (t[x].val == val) {bool ty = t[t[x].ch[1]].wt > t[t[x].ch[0]].wt;if (!t[x].ch[ty]) { x = 0; return ; }rot(x, ty), del(t[x].ch[!ty], val);} else del(t[x].ch[val > t[x].val], val);update(x);}int kth(int k) {int x = rt;if (t[x].sz < k) return inf;for (; k != t[t[x].ch[0]].sz + 1;) (k <= t[t[x].ch[0]].sz) ? x = t[x].ch[0] : (k -= t[t[x].ch[0]].sz + 1, x = t[x].ch[1]);return t[x].val;}int Count(int x) {if (!x) return 0;if (t[x].val < q) return Count(t[x].ch[1]) + t[t[x].ch[0]].sz + 1;else return Count(t[x].ch[0]);}void init() {scanf("%d", &n);}void doit() {for (int i = 1; i <= n; i ++) {int x; char ty;scanf (" %c %d", &ty, &x);if (ty == 'I') add(rt, x);else if (ty == 'D') del(rt, x);else if (ty == 'K') {x = kth(x);(x != inf) ? printf ("%d\n", x) : puts("invalid");}else q = x, printf ("%d\n", Count(rt));}}int main(){init();doit();return 0;}
0 0
- SPOJ3273.ORDERSET(Treap 模版)
- SPOJ3273(Treap)
- [SPOJ ORDERSET] Order statistic set [Treap]
- treap模版
- Treap模版
- treap模版
- treap模版
- SPOJ ORDERSET Order statistic set 非旋转treap
- treap树模版
- 平衡树Treap模版
- BZOJ 1588 Treap模版题
- 可持久化treap 模版
- (新知)数据结构--Treap--模版
- spoj ORDERSET
- LA5031 Graph and Queries (Treap模版)
- BZOJ 1208 平衡树Treap模版题
- SPOJ3273--Order statistic set
- 随机自平衡二叉搜索树Treap模版
- zabbix 模版其实就是主机
- hdoj 1220 Cube
- 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 二分答案+最大流
- 228. Summary Ranges leetCode[Java]
- 后台程序运行进度提示(ProgressBar与Handle的整合应用)
- SPOJ3273.ORDERSET(Treap 模版)
- String could not be serialized with encoding: 4
- 批量梯度下降算法和随机梯度下降算法的代码实现
- BZOJ 1000
- Codeforces--106C--Buns(背包)
- java类加载器
- 龙芯学习笔记
- 电商系统Broadleaf文档翻译(四) - 应用架构组件视图application architecture component view
- 位域与大小端问题