普通平衡树与文艺平衡树的splay代码
主要综合借鉴了yyb和马前卒两位大佬的。
- //普通平衡树
- #include <cstdio>
- #include <cctype>
- #include <cstring>
- #include <algorithm>
- #define R(x) scanf("%d", &x)
- #define ri readint()
- #define gc getchar()
- #define wi(x) printf("%d\n", x)
- #define ls(x) T[x].ch[0]
- #define rs(x) T[x].ch[1]
- using namespace std;
- int readint() {
- int x = , s = , c = gc;
- while (c <= ) c = gc;
- if (c == '-') s = -, c = gc;
- for (; isdigit(c); c = gc)
- x = x * + c - ;
- return x * s;
- }
- struct node {
- int fa;
- int ch[];
- int val, cnt;
- int size;
- };
- node T[];
- int tot, root;
- int lr(int x) {
- return x == rs(T[x].fa);
- }
- void Connect(int son, int fa, int way) {
- T[fa].ch[way] = son;
- T[son].fa = fa;
- }
- void push_up(int pos) {
- T[pos].size = T[ls(pos)].size + T[rs(pos)].size + T[pos].cnt;
- }
- void Rotate(int x) {
- int f = T[x].fa, gf = T[f].fa;
- int x_f = lr(x), f_gf = lr(f);
- Connect(T[x].ch[x_f^], f, x_f);
- Connect(f, x, x_f^);
- Connect(x, gf, f_gf);
- push_up(f);
- push_up(x);
- }
- void splay(int pos, int goal) {
- for (; T[pos].fa != goal; Rotate(pos)) {
- int f = T[pos].fa, gf = T[f].fa;
- if (gf != goal) {
- lr(f) ^ lr(pos) ? Rotate(pos) : Rotate(f);
- }
- }
- if (goal == ) root = pos;
- }
- void Insert(int x) {
- int pos = root, fa = ;
- while (pos && T[pos].val != x) {
- fa = pos;
- pos = T[pos].ch[x > T[pos].val];
- }
- if (pos) {
- T[pos].cnt++;
- } else {
- pos = ++tot;
- if (fa) T[fa].ch[x > T[fa].val] = pos;
- T[pos].ch[] = T[pos].ch[] = ;
- T[pos].fa = fa;
- T[pos].val = x;
- T[pos].cnt = T[pos].size = ;
- }
- splay(pos, );
- }
- int find(int x) {//数字val为x的位置
- int pos = root;
- if (!pos) return ;
- while (T[pos].ch[x > T[pos].val] && x != T[pos].val)
- pos = T[pos].ch[x > T[pos].val];
- splay(pos, );
- return pos;
- }
- int Next(int flag, int x) {//0为前驱,1为后继
- find(x);
- int pos = root;
- if (T[pos].val > x && flag) return pos;
- if (T[pos].val < x && !flag) return pos;
- pos = T[pos].ch[flag];
- while (T[pos].ch[flag^]) {
- pos = T[pos].ch[flag^];
- }
- return pos;
- }
- void Delet(int x) {//删除数字x
- int pos = find(x);
- if (!pos) return;
- if (T[pos].cnt > ) {
- T[pos].cnt--;
- T[pos].size--;
- return;
- } else {
- if (!ls(pos) && !rs(pos)) {
- root = ;
- return;
- } else if (ls(pos) && rs(pos)) {
- int u = ls(pos);
- while (rs(u)) u = rs(u);
- splay(u, );
- Connect(rs(pos), u, );
- push_up(u);
- } else {
- if (ls(pos)) root = ls(pos);
- else root = rs(pos);
- T[root].fa = ;
- }
- }
- }
- int Rank(int x) {//数字x的排名
- find(x);
- return T[ls(root)].size + ;
- }
- int Kth(int x) {//第x大的数
- int pos = root;
- if (T[pos].size < x) return -;
- while (true) {
- int lson = ls(pos);
- if (x > T[lson].size + T[pos].cnt) {
- x -= T[lson].size + T[pos].cnt;
- pos = rs(pos);
- } else {
- if (x > T[lson].size) return T[pos].val;//这个题返回的是值
- pos = lson;
- }
- }
- }
- int main() {
- int n;
- for (n = ri; n; n--) {
- int op = ri, x = ri;
- if (op == ) Insert(x);
- else if (op == ) Delet(x);
- else if (op == ) wi(Rank(x));
- else if (op == ) wi(Kth(x));
- else wi(T[Next(op - , x)].val);
- }
- return ;
- }
- //文艺平衡树
- #include <cstdio>
- #include <cctype>
- #include <iostream>
- #include <algorithm>
- #define ri readint()
- #define gc getchar()
- #define init(a, b) memset(a, b, sizeof(a))
- #define rep(i, a, b) for (int i = a; i <= b; i++)
- #define irep(i, a, b) for (int i = a; i >= b; i--)
- #define ls(x) T[x].ch[0]
- #define rs(x) T[x].ch[1]
- using namespace std;
- typedef double db;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef pair<int, int> P;
- const int inf = 0x3f3f3f3f;
- const ll INF = 1e18;
- inline int readint() {
- int x = , s = , c = gc;
- while (!isdigit(c)) c = gc;
- if (c == '-') s = -, c = gc;
- for (; isdigit(c); c = gc)
- x = x * + c - ;
- return x * s;
- }
- const int maxn = 1e5 + ;
- int n, m, root, tot;
- struct node {
- int fa, ch[];
- int val, size, tag;
- };
- node T[maxn];
- int lr(int x) {
- return x == T[T[x].fa].ch[];
- }
- void connect(int son, int fa, int way) {
- T[son].fa = fa;
- T[fa].ch[way] = son;
- }
- void push_down(int pos) {
- if (T[pos].tag) {
- T[ls(pos)].tag ^= ;
- T[rs(pos)].tag ^= ;
- T[pos].tag = ;
- swap(ls(pos), rs(pos));
- }
- }
- void push_up(int pos) {
- T[pos].size = T[ls(pos)].size + T[rs(pos)].size + ;
- }
- void rotate(int pos) {
- int f = T[pos].fa, gf = T[f].fa;
- int x_f = lr(pos), f_gf = lr(f);
- connect(T[pos].ch[x_f^], f, x_f);
- connect(f, pos, x_f^);
- connect(pos, gf, f_gf);
- push_up(f);
- push_up(pos);
- }
- void splay(int pos, int goal) {
- for (; T[pos].fa != goal; rotate(pos)) {
- int f = T[pos].fa, gf = T[f].fa;
- if (gf != goal)
- lr(pos) ^ lr(f) ? rotate(pos) : rotate(f);
- }
- if (!goal) root = pos;
- }
- void insert(int x) {
- int pos = root, fa = ;
- while (pos && T[pos].val != x) {
- fa = pos;
- pos = T[pos].ch[x > T[pos].val];
- }
- pos = ++tot;
- if (fa) T[fa].ch[x > T[fa].val] = pos;
- T[pos].fa = fa;
- T[pos].ch[] = T[pos].ch[] = ;
- T[pos].val = x;
- T[pos].size = ;
- T[pos].tag = ;
- splay(pos, );
- }
- int Kth(int x) {
- int pos = root;
- while (true) {
- push_down(pos);
- int lson = ls(pos);
- if (x > T[lson].size + ) {
- x -= T[lson].size + ;
- pos = rs(pos);
- } else {
- if (x > T[lson].size) return pos;
- pos = lson;
- }
- }
- }
- void work(int l, int r) {
- l = Kth(l), r = Kth(r + );
- splay(l, ), splay(r, l);
- int pos = rs(root);
- pos = ls(pos);
- T[pos].tag ^= ;
- }
- void dfs(int cur) {
- push_down(cur);
- if (T[cur].ch[]) dfs(T[cur].ch[]);
- if (T[cur].val != -inf && T[cur].val != inf)
- printf("%d ", T[cur].val);
- if (T[cur].ch[]) dfs(T[cur].ch[]);
- }
- int main() {
- n = ri, m = ri;
- insert(inf), insert(-inf);
- rep(i, , n) insert(i);
- while (m--) {
- int l = ri, r = ri;
- work(l, r);
- }
- dfs(root);
- return ;
- }
普通平衡树与文艺平衡树的splay代码的更多相关文章
- [bzoj3224]普通平衡树/3223文艺平衡树
这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...
- 【Splay】bzoj3223-Tyvj1729文艺平衡树
一.题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 ...
- bzoj3223 文艺平衡树 (treap or splay分裂+合并)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3313 Solved: 1883 [Submit][S ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- BZOJ3223 文艺平衡树(splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 洛谷 P3391【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
随机推荐
- 人生苦短之Python迭代器
迭代 在Python中,如果给定一个list或者touple,我们可以通过for循环来遍历,将值依次取出,这种遍历称为迭代. 在Python中是通过for...in..来进行遍历的,在Java中则是 ...
- IPFS 到底是怎么工作的?
简介 我们知道,一个存储服务,最基本的功能就是存和取.IPFS 中提供了这两种语义,那就是 add 和 get 操作. 在 IPFS 系统中执行 add 操作,就是执行了一次存操作,放在网络的概念里, ...
- 简单封装微信小程序
一.不同环境配置封装 新建config文件夹,根据自己有不同环境设置不同的js文件 具体js文件内容: exports.config = { requestHost: 'https://******. ...
- reactjs的一些笔记
1.使用虚拟DOM作为其不同的实现.同时可以由服务器node.js渲染,从而不需要过重的浏览器DOM支持. 2.虚拟DOM:在浏览器端用javascript实现了一套DOM API.用react开 ...
- 字面量(literal)与 C 语言复合字面量(compound literals)
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation)(字面量是相对变量常量等定义的,无论是常量还是变量,其值在某一时刻总是确定的,只是变量可以反复赋值.刷新 ...
- Learning Android ActionBar
PART1:Make Android2.1 Support ActionBar Last evening I learnt how to make android2.1 support actionb ...
- 使用 @RequestMapping 映射请求
- bzoj 2655 calc —— 拉格朗日插值
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2655 先设 f[i][j] 表示长度为 i 的序列,范围是 1~j 的答案: 则 f[i][ ...
- bzoj1013高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1013 似乎是很明显的高斯消元: 第一次写高斯消元. 代码如下: #include<io ...
- 蓝桥杯 2014本科C++ B组 李白打酒 三种实现方法 枚举/递归
标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共遇到店5次, ...