[bzoj 3224] Tyvj 1728 普通平衡树(Splay)
2017-01-08 14:11
525 查看
题意:维护一些数,支持这些操作:插入x、删除x、查询x的排名(多个x则输出最小者)、查询排名为x的数、查询小于x的最大数、查询大于x的最小数。初始序列为空,操作数不超过10^5,每个数的数据范围:[-1e7,1e7]。
写一棵普通的平衡树就好了。由于是想练习一下Splay Tree,就决定是它了。
以前用指针写lct,非常难以调试,解决方案是用map把指针映射成数再输出……自此以后就改用数组了。用结构体数组,意味着要不停地打点加方括号,干脆不用结构体了,直接数组代码看起来清爽多了。
旋转
前驱
一开始,
写完之后发觉我的做法好像不太主流……又因为对其复杂度的分析不是很理解,浏览了一下Sleator和Tarjan的原始论文《Self-adjusting binary search trees》。
关于splay的复杂度。首先,直接放缩成2(r’(x)-r(x))+2是不可行的,因为有常数2。一旦累加起来它就不是常数了,不可忽略;2(r’(x)-r(x))+2 ≤ 3(r’(x)-r(x))要求r’(x)-r(x)≥2,不一定能达成。是说为什么按照我的理解,把论证中的对数函数换成一切非负的增函数都可以……其二,使用O((m+n)lg n+m)而非O(m lg n+m),能使复杂度对于一切初始情况皆成立;《算法导论》上说,任何时刻的势不小于初始的势,均摊时间才是实际时间的上界,有时的确无法满足这个条件,也无妨,把势能的减少量考虑进来即可。
关于insert和erase操作的实现。论文中说:“It is convenient to implement insert and delete using join and split.”又说:“There are alternative implementations of insert and delete that have slightly better amortized time bounds.”前者与我的方法等价,后者是这样的:
插入直接找待插入的位置,将新结点伸展为根。时间和方法一差不多。
删除直接找待删除的结点,合并左右子树,替换掉它,把它的父亲伸展到根。方法一进行两三次splay,提到根或根的儿子处;从根的儿子开始go left一次。方法二进行两次splay,一次提到根,一次提到某结点;从某结点开始go left一次。虽然splay会带来奇奇怪怪的效果……但是后一种感觉上常数就小一些。
但是牺牲一点时间换代码简单也不是特别不划算……
灵机一动,
用class进行了一些封装,看起来很清爽。
写一棵普通的平衡树就好了。由于是想练习一下Splay Tree,就决定是它了。
以前用指针写lct,非常难以调试,解决方案是用map把指针映射成数再输出……自此以后就改用数组了。用结构体数组,意味着要不停地打点加方括号,干脆不用结构体了,直接数组代码看起来清爽多了。
旋转
rot(x, d)以根和被提上来的儿子方向作为参数。
type(x)用于获知结点x是左儿子还是右儿子。
splay(x, p)操作写成了循环,所以得记父结点。为了避免忘记这一点,把设置父子关系写成了
set(x, d, y)函数。
find(c, b)操作找小于某数的最大数或大于某数的最小数,并伸展成为根,用一个参数b指明前者还是后者。以前者为例,当前结点小于c,用当前结点更新答案,向右子树走;否则,向左子树走。走到空结点
nil停止。有一个纠结的地方:到达的最深结点y可能不是要求被伸展成为根的结点z,此时怎样调整树的结构?后面再说。为了便于处理不存在这样的数的情况,加两个虚拟结点
-inf和
inf。
insert(c)插入一个c。先用
find找到小于c的最大结点并伸展为根,设它为l,r是l的右儿子,断开l, r,使它们成为新结点的左右儿子,并设新结点为根即可。别忘了用
up(x)更新子树大小的信息。
new_node(c)用于生成一个新结点,它的值为c,左右子树为空,大小为1。
erase(c)删除一个c。先用
find找到小于c的最大结点并伸展为根,设它为l,r是l的右儿子,找到r树中最小的结点r’,r’便是待删除的结点。
splay(r', l),再把r’的右子树接到l的右子树的位置即可。
rank(c)查询c的最小排名。x=
find(c, 0),返回小于c的数的个数+1=(x的左子树大小+1-1)+1=x的左子树大小+1。别忘了虚拟结点
-inf。
kth(k)查询第k小的数。首先++k,然后在树上二分即可。
前驱
predecessor(c)和后继
successor(c)有了
find就很简单了。查询,返回该结点的值即可。
一开始,
find操作中我把y和z都splay一下,636ms。只splay(z)是552ms。
写完之后发觉我的做法好像不太主流……又因为对其复杂度的分析不是很理解,浏览了一下Sleator和Tarjan的原始论文《Self-adjusting binary search trees》。
关于splay的复杂度。首先,直接放缩成2(r’(x)-r(x))+2是不可行的,因为有常数2。一旦累加起来它就不是常数了,不可忽略;2(r’(x)-r(x))+2 ≤ 3(r’(x)-r(x))要求r’(x)-r(x)≥2,不一定能达成。是说为什么按照我的理解,把论证中的对数函数换成一切非负的增函数都可以……其二,使用O((m+n)lg n+m)而非O(m lg n+m),能使复杂度对于一切初始情况皆成立;《算法导论》上说,任何时刻的势不小于初始的势,均摊时间才是实际时间的上界,有时的确无法满足这个条件,也无妨,把势能的减少量考虑进来即可。
关于insert和erase操作的实现。论文中说:“It is convenient to implement insert and delete using join and split.”又说:“There are alternative implementations of insert and delete that have slightly better amortized time bounds.”前者与我的方法等价,后者是这样的:
插入直接找待插入的位置,将新结点伸展为根。时间和方法一差不多。
删除直接找待删除的结点,合并左右子树,替换掉它,把它的父亲伸展到根。方法一进行两三次splay,提到根或根的儿子处;从根的儿子开始go left一次。方法二进行两次splay,一次提到根,一次提到某结点;从某结点开始go left一次。虽然splay会带来奇奇怪怪的效果……但是后一种感觉上常数就小一些。
但是牺牲一点时间换代码简单也不是特别不划算……
灵机一动,
find里,若y和z不等,就splay(y, z)再splay(z)。这样处理似乎很科学,复杂度的分析仍然适用(Tarjan他们的access/split操作不存在这样的问题,主要是因为需求不同)。572ms。
用class进行了一些封装,看起来很清爽。
#include <cstdio> #include <cctype> const int MAX_N = 1e5, inf = 1<<30; class Splay { static const int n = MAX_N+4, nil = n-1; int fa , ch [2], v , sz , ptr, &root; void set(int x, int d, int y) { fa[ch[x][d] = y] = x; } void up(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; } int type(int x) { return x == ch[fa[x]][1]; } int new_node(int c) { ch[ptr][0] = ch[ptr][1] = nil; v[ptr] = c; sz[ptr] = 1; return ptr++; } int rot(int x, int d) { int y = ch[x][d]; set(fa[x], type(x), y); set(x, d, ch[y][d^1]); set(y, d^1, x); up(x); up(y); } void splay(int x, int p=0) { int y; while ((y = fa[x]) != p) { int z = fa[y], t1 = type(x); if (z != p) { int t2 = type(y); if (t1 == t2) rot(z, t2), rot(y, t1); else rot(y, t1), rot(z, t2); } else rot(y, t1); } } int find(int c, int b=0) { int x = root, y, z; while (x != nil) { y = x; if (v[x] != c && v[x] > c == b) { z = x; x = ch[x][b^1]; } else x = ch[x][b]; } if (y != z) splay(y, z); splay(z); return z; } public: Splay(): ptr(1), root(ch[0][0]) { sz[nil] = 0; set(0, 0, new_node(-inf)); set(1, 1, new_node(inf)); up(1); } void insert(int c) { int l = find(c), r = ch[l][1], x = new_node(c); ch[l][1] = nil; set(0, 0, x); set(x, 0, l); set(x, 1, r); up(l); up(x); } void erase(int c) { int l = find(c), r = ch[l][1]; while (ch[r][0] != nil) r = ch[r][0]; splay(r, l); set(l, 1, ch[r][1]); up(l); } int predecessor(int c) { return v[find(c)]; } int successor(int c) { return v[find(c, 1)]; } int rank(int c) { return sz[ch[find(c)][0]] + 1; } int kth(int k) { ++k; int x = root; while (x != nil) { int s = sz[ch[x][0]]; if (s+1 == k) return splay(x), v[x]; if (s >= k) x = ch[x][0]; else k -= s+1, x = ch[x][1]; } } } T; template<typename t> inline void read(t& x) { char c = getchar(); int sgn = 1; x = 0; while (!isdigit(c)) { sgn = c == '-' ? -1 : 1; c = getchar(); } while (isdigit(c)) { x = x*10 + c - '0'; c = getchar(); } x *= sgn; } int main() { int n; read(n); while (n--) { int opt, x; read(opt), read(x); switch (opt) { case 1: T.insert(x); break; case 2: T.erase(x); break; case 3: printf("%d\n", T.rank(x)); break; case 4: printf("%d\n", T.kth(x)); break; case 5: printf("%d\n", T.predecessor(x)); break; case 6: printf("%d\n", T.successor(x)); } } return 0; }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)