
普通平衡树板子
发布日期:2021-05-07 08:57:13
浏览次数:9
分类:原创文章
本文共 3103 字,大约阅读时间需要 10 分钟。
#include <bits/stdc++.h>struct Splay_tree{ struct node{ int fa, ch[2], val, siz, cnt; node() { fa = val = siz = cnt = 0; memset(ch, 0, sizeof ch); }; }; int tot,root; std::vector <node> t; void update(int x){ t[x].siz = t[t[x].ch[0]].siz + t[t[x].ch[1]].siz + t[x].cnt; } void rotate(int x){ int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x); t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z; t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y; t[x].ch[k ^ 1] = y; t[y].fa = x; update(y); update(x); } void splay(int x, int s){ while(t[x].fa != s){ int y = t[x].fa, z = t[y].fa; if(z != s) (t[y].ch[1] == x) ^ (t[z].ch[1] == y) ? rotate(x) : rotate(y); rotate(x); } if(s == 0) root = x; } void find(int x){ int u = root; if(!u) return ; while(t[u].ch[x > t[u].val] && x != t[u].val) u = t[u].ch[x > t[u].val]; splay(u, 0); } void insert(int x){ int u = root,fa = 0; while(u && t[u].val != x){ fa = u; u = t[u].ch[x > t[u].val]; } if(u) t[u].cnt++; else{ u = ++tot; if(fa) t[fa].ch[x > t[fa].val] = u; t[u].ch[0] = t[u].ch[1] = 0; t[u].fa = fa; t[u].val = x; t[u].siz = t[u].cnt = 1; } splay(u, 0); } int Next(int x, int f){ find(x); int u = root; if((t[u].val > x && f) || (t[u].val < x && !f)) return u; u = t[u].ch[f]; while(t[u].ch[f ^ 1]) u = t[u].ch[f ^ 1]; return u; } void Delete(int x){ int last = Next(x, 0), next = Next(x, 1); splay(last, 0), splay(next, last); int del = t[next].ch[0]; if(t[del].cnt > 1){ t[del].cnt--; splay(del, 0); } else{ t[next].ch[0] = 0; } } int k_th(int x){ int u = root; if(t[u].siz < x) return 0; while(1){ int y = t[u].ch[0]; if(t[y].siz + t[u].cnt < x) { x -= t[y].siz + t[u].cnt; u = t[u].ch[1]; } else if(t[y].siz >= x) { u = y; } else return t[u].val; } } Splay_tree(int n) : tot(0), root(0), t(n << 1) { insert(-1e9), insert(1e9);}};int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int n; std:: cin >> n; Splay_tree T(n); while(n--) { int op, x; std::cin >> op >> x; if(op == 1) T.insert(x); else if(op == 2) T.Delete(x); else if(op == 3) { T.find(x); std::cout << T.t[T.t[T.root].ch[0]].siz << '\n'; } else if(op == 4) std::cout << T.k_th(x + 1) << '\n'; else if(op == 5) std::cout << T.t[T.Next(x,0)].val << '\n'; else std::cout << T.t[T.Next(x,1)].val << '\n'; } return 0;}
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月28日 05时34分55秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
英语02_单词词性
2019-03-04
C语言12_预处理 #
2019-03-04
低通滤波器的设计
2019-03-04
窄带随机过程的产生
2019-03-04
随机四则运算
2019-03-04
Java重载overload
2019-03-04
Java面向对象
2019-03-04
JAVA带标签的break和continue
2019-03-04
Java获取线程基本信息的方法
2019-03-04
JavaWeb用户信息管理系统-创建登录业的务持久层
2019-03-04
SpringIoC和DI注解开发
2019-03-04
Java类和对象
2019-03-04
Java集合Collection
2019-03-04
SpringMVC入门-概述和基本配置
2019-03-04
SpringBoot快速入门
2019-03-04
医疗管理系统-手机快速登录和SpringSecurity权限控制
2019-03-04
网页实现微信登录
2019-03-04
vue源码分析(MVVM篇)
2019-03-04
React(八)- ReactUI组件库及Redux的使用
2019-03-04