这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
|
2020-2021:teams:legal_string:lgwza:splay [2020/08/22 22:08] lgwza |
2020-2021:teams:legal_string:lgwza:splay [2020/08/22 22:11] (当前版本) lgwza [删除操作] |
||
|---|---|---|---|
| 行 219: | 行 219: | ||
| * 否则,合并它的左右两棵子树即可。 | * 否则,合并它的左右两棵子树即可。 | ||
| - | <hidden> | ||
| <code cpp> | <code cpp> | ||
| void del(int k) { | void del(int k) { | ||
| 行 254: | 行 253: | ||
| maintain(rt); | maintain(rt); | ||
| } | } | ||
| - | </hidden> | + | |
| </code> | </code> | ||
| + | |||
| + | ===== 完整代码 ===== | ||
| + | |||
| + | <hidden> | ||
| + | <code cpp> | ||
| + | #include <cstdio> | ||
| + | const int N = 100005; | ||
| + | int rt, tot, fa[N], ch[N][2], val[N], cnt[N], sz[N]; | ||
| + | struct Splay { | ||
| + | void maintain(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x]; } | ||
| + | bool get(int x) { return x == ch[fa[x]][1]; } | ||
| + | void clear(int x) { | ||
| + | ch[x][0] = ch[x][1] = fa[x] = val[x] = sz[x] = cnt[x] = 0; | ||
| + | } | ||
| + | void rotate(int x) { | ||
| + | int y = fa[x], z = fa[y], chk = get(x); | ||
| + | ch[y][chk] = ch[x][chk ^ 1]; | ||
| + | fa[ch[x][chk ^ 1]] = y; | ||
| + | ch[x][chk ^ 1] = y; | ||
| + | fa[y] = x; | ||
| + | fa[x] = z; | ||
| + | if (z) ch[z][y == ch[z][1]] = x; | ||
| + | maintain(x); | ||
| + | maintain(y); | ||
| + | } | ||
| + | void splay(int x) { | ||
| + | for (int f = fa[x]; f = fa[x], f; rotate(x)) | ||
| + | if (fa[f]) rotate(get(x) == get(f) ? f : x); | ||
| + | rt = x; | ||
| + | } | ||
| + | void ins(int k) { | ||
| + | if (!rt) { | ||
| + | val[++tot] = k; | ||
| + | cnt[tot]++; | ||
| + | rt = tot; | ||
| + | maintain(rt); | ||
| + | return; | ||
| + | } | ||
| + | int cnr = rt, f = 0; | ||
| + | while (1) { | ||
| + | if (val[cnr] == k) { | ||
| + | cnt[cnr]++; | ||
| + | maintain(cnr); | ||
| + | maintain(f); | ||
| + | splay(cnr); | ||
| + | break; | ||
| + | } | ||
| + | f = cnr; | ||
| + | cnr = ch[cnr][val[cnr] < k]; | ||
| + | if (!cnr) { | ||
| + | val[++tot] = k; | ||
| + | cnt[tot]++; | ||
| + | fa[tot] = f; | ||
| + | ch[f][val[f] < k] = tot; | ||
| + | maintain(tot); | ||
| + | maintain(f); | ||
| + | splay(tot); | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | int rk(int k) { | ||
| + | int res = 0, cnr = rt; | ||
| + | while (1) { | ||
| + | if (k < val[cnr]) { | ||
| + | cnr = ch[cnr][0]; | ||
| + | } else { | ||
| + | res += sz[ch[cnr][0]]; | ||
| + | if (k == val[cnr]) { | ||
| + | splay(cnr); | ||
| + | return res + 1; | ||
| + | } | ||
| + | res += cnt[cnr]; | ||
| + | cnr = ch[cnr][1]; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | int kth(int k) { | ||
| + | int cnr = rt; | ||
| + | while (1) { | ||
| + | if (ch[cnr][0] && k <= sz[ch[cnr][0]]) { | ||
| + | cnr = ch[cnr][0]; | ||
| + | } else { | ||
| + | k -= cnt[cnr] + sz[ch[cnr][0]]; | ||
| + | if (k <= 0) { | ||
| + | splay(cnr); | ||
| + | return val[cnr]; | ||
| + | } | ||
| + | cnr = ch[cnr][1]; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | int pre() { | ||
| + | int cnr = ch[rt][0]; | ||
| + | while (ch[cnr][1]) cnr = ch[cnr][1]; | ||
| + | splay(cnr); | ||
| + | return cnr; | ||
| + | } | ||
| + | int nxt() { | ||
| + | int cnr = ch[rt][1]; | ||
| + | while (ch[cnr][0]) cnr = ch[cnr][0]; | ||
| + | splay(cnr); | ||
| + | return cnr; | ||
| + | } | ||
| + | void del(int k) { | ||
| + | rk(k); | ||
| + | if (cnt[rt] > 1) { | ||
| + | cnt[rt]--; | ||
| + | maintain(rt); | ||
| + | return; | ||
| + | } | ||
| + | if (!ch[rt][0] && !ch[rt][1]) { | ||
| + | clear(rt); | ||
| + | rt = 0; | ||
| + | return; | ||
| + | } | ||
| + | if (!ch[rt][0]) { | ||
| + | int cnr = rt; | ||
| + | rt = ch[rt][1]; | ||
| + | fa[rt] = 0; | ||
| + | clear(cnr); | ||
| + | return; | ||
| + | } | ||
| + | if (!ch[rt][1]) { | ||
| + | int cnr = rt; | ||
| + | rt = ch[rt][0]; | ||
| + | fa[rt] = 0; | ||
| + | clear(cnr); | ||
| + | return; | ||
| + | } | ||
| + | int cnr = rt; | ||
| + | int x = pre(); | ||
| + | splay(x); | ||
| + | fa[ch[cnr][1]] = x; | ||
| + | ch[x][1] = ch[cnr][1]; | ||
| + | clear(cnr); | ||
| + | maintain(rt); | ||
| + | } | ||
| + | } tree; | ||
| + | |||
| + | int main() { | ||
| + | int n, opt, x; | ||
| + | for (scanf("%d", &n); n; --n) { | ||
| + | scanf("%d%d", &opt, &x); | ||
| + | if (opt == 1) | ||
| + | tree.ins(x); | ||
| + | else if (opt == 2) | ||
| + | tree.del(x); | ||
| + | else if (opt == 3) | ||
| + | printf("%d\n", tree.rk(x)); | ||
| + | else if (opt == 4) | ||
| + | prin | ||
| + | </code> | ||
| + | </hidden> | ||
| + | |||
| + | ===== 例题 ===== | ||
| + | |||
| + | 以下题目都是裸的 $\text{Splay}$ 维护二叉查找树。 | ||
| + | |||
| + | [[https://loj.ac/problem/104|【模板】普通平衡树]] | ||
| + | |||
| + | [[https://loj.ac/problem/105|【模板】文艺平衡树]] | ||
| + | |||
| + | [[https://loj.ac/problem/10143|「HNOI2002」营业额统计]] | ||
| + | |||
| + | [[https://loj.ac/problem/10144|「HNOI2004」宠物收养所]] | ||
| + | |||
| + | ===== 练习题 ===== | ||
| + | |||
| + | [[https://www.luogu.com.cn/problem/P4402|「Cerc2007」robotic sort 机械排序]] | ||
| + | |||
| + | [[https://loj.ac/problem/106|二逼平衡树(树套树)]] | ||
| + | |||
| + | [[http://www.lydsy.com/JudgeOnline/problem.php?id=2827|bzoj 2827 千山鸟飞绝]] | ||
| + | |||
| + | [[http://www.lydsy.com/JudgeOnline/problem.php?id=4923|「Lydsy1706 月赛」K 小值查询]] | ||
| + | |||
| + | ===== 参考链接 ===== | ||
| + | |||
| + | [[https://oi-wiki.org/ds/splay/|OI Wiki]] | ||