平衡树(模板 and 题目)记录

平衡树

替罪羊树

#include <bits/stdc++.h>using namespace std;const double alpha = 0.725;
const int N = 2e6 + 10;struct Spgtree {int ls[N], rs[N], val[N], num[N], fac[N], sz[N], sum[N], cnt, root;int top, stk[N];void update(int rt) {fac[rt] = fac[ls[rt]] + fac[rs[rt]] + (num[rt] ? 1 : 0);sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;sum[rt] = sum[ls[rt]] + sum[rs[rt]] + num[rt];}void dfs(int rt) {if (!rt) {return ;}dfs(ls[rt]);if (num[rt]) {stk[++top] = rt;}dfs(rs[rt]);}int build(int l, int r) {if (l > r) {return 0;}if (l == r) {ls[stk[l]] = rs[stk[l]] = 0;update(stk[l]);return stk[l];}int mid = l + r >> 1;ls[stk[mid]] = build(l, mid - 1), rs[stk[mid]] = build(mid + 1, r);update(stk[mid]);return stk[mid];}void rebuild(int &rt) {top = 0;dfs(rt);rt = build(1, top);}bool imbalence(int rt) {return sum[rt] && (alpha * sz[rt] <= max(sz[ls[rt]], sz[rs[rt]]) || alpha * sz[rt] >= fac[rt]);}void insert(int &rt, int value) {if (!rt) {rt = ++cnt, val[rt] = value, num[rt] = 1;update(rt);return ;}if (val[rt] == value) {num[rt]++;}else if (value < val[rt]) {insert(ls[rt], value);}else {insert(rs[rt], value);}update(rt);if (imbalence(rt)) {rebuild(rt);}}void insert(int value) {insert(root, value);}void erase(int &rt, int value) {if (!rt) {return ;}if (val[rt] == value) {num[rt]--;}else if (value < val[rt]) {erase(ls[rt], value);}else {erase(rs[rt], value);}update(rt);if (imbalence(rt)) {rebuild(rt);}}void erase(int value) {erase(root, value);}int get_rank(int value) {int rt = root, rank = 1;while (rt) {if (value <= val[rt]) {rt = ls[rt];}else {rank += num[rt] + sum[ls[rt]];rt = rs[rt];}}return rank;}int get_num(int rank) {int rt = root;while (rt) {if (num[rt] && sum[ls[rt]] < rank && sum[ls[rt]] + num[rt] >= rank) {break;}if (sum[ls[rt]] >= rank) {rt = ls[rt];}else {rank -= sum[ls[rt]] + num[rt];rt = rs[rt];}}return val[rt];}
}tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);return 0;
}

fhq treap

维护值

#include <bits/stdc++.h>using namespace std;const int N = 2e6 + 10;mt19937 rnd(233);struct fhqtreap {int ls[N], rs[N], val[N], key[N], sz[N], root, cnt;inline int new_node(int value) {cnt++;val[cnt] = value, sz[cnt] = 1, key[cnt] = rnd();return cnt;}void update(int rt) {sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;}void split(int rt, int value, int &x, int &y) {if (!rt) {x = y = 0;return ;}if (val[rt] <= value) {x = rt;split(rs[rt], value, rs[rt], y);}else {y = rt;split(ls[rt], value, x, ls[rt]);}update(rt);}int merge(int x, int y) {if (!x || !y) {return x | y;}if (key[x] < key[y]) {ls[y] = merge(x, ls[y]);update(y);return y;}else {rs[x] = merge(rs[x], y);update(x);return x;}}void insert(int value) {int x, y;split(root, value, x, y);root = merge(merge(x, new_node(value)), y);}void erase(int value) {int x, y, z;split(root, value, x, z);split(x, value - 1, x, y);y = merge(ls[y], rs[y]);root = merge(merge(x, y), z);}int get_num(int rank) {int rt = root;while (rt) {if (sz[ls[rt]] + 1 == rank) {break;}else if (sz[ls[rt]] >= rank) {rt = ls[rt];}else {rank -= sz[ls[rt]] + 1;rt = rs[rt];}}return val[rt];}int get_rank(int value) {int x, y, rank;split(root, value - 1, x, y);rank = sz[x] + 1;root = merge(x, y);return rank;}int pre(int value) {int x, y, rt;split(root, value - 1, x, y);rt = x;while (rs[rt]) {rt = rs[rt];}root = merge(x, y);return val[rt];}int suc(int value) {int x, y, rt;split(root, value, x, y);rt = y;while (ls[rt]) {rt = ls[rt];}root = merge(x, y);return val[rt];}
}tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);return 0;
}

维护区间

#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;mt19937 rnd(233);struct fhqtreap {int ls[N], rs[N], val[N], key[N], sz[N], rev[N], cnt, root;int new_node(int value) {cnt++;val[cnt] = value, key[cnt] = rnd(), sz[cnt] = 1;return cnt;}void push_down(int rt) {if (rev[rt]) {swap(ls[rt], rs[rt]);rev[ls[rt]] ^= 1, rev[rs[rt]] ^= 1;rev[rt] = 0;}}void push_up(int rt) {sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;}void split(int rt, int siz, int &x, int &y) {if (!rt) {x = y = 0;return ;}push_down(rt);if (sz[ls[rt]] < siz) {x = rt;split(rs[rt], siz - sz[ls[rt]] - 1, rs[rt], y);}else {y = rt;split(ls[rt], siz, x, ls[rt]);}push_up(rt);}int merge(int x, int y) {if (!x || !y) {return x | y;}if (key[x] < key[y]) {push_down(x);rs[x] = merge(rs[x], y);push_up(x);return x;}else {push_down(y);ls[y] = merge(x, ls[y]);push_up(y);return y;}}void reverse(int l, int r) {int x, y, z;split(root, l - 1, x, y);split(y, r - l + 1, y, z);rev[y] ^= 1;root = merge(merge(x, y), z);}void print(int rt) {if (!rt) {return ;}push_down(rt);print(ls[rt]);printf("%d ", val[rt]);print(rs[rt]);}void print() {print(root);}void build(int n) {for (int i = 1; i <= n; i++) {root = merge(root, new_node(i));}}
}tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n, m;scanf("%d %d", &n, &m);tree.build(n);for (int i = 1; i <= m; i++) {int l, r;scanf("%d %d", &l, &r);tree.reverse(l, r);}tree.print(), putchar('\n');return 0;
}

P3850 [TJOI2007]书架

#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;string str[N];mt19937 rnd(233);struct Treap {int ls[N], rs[N], val[N], sz[N], key[N], root, cnt;void push_up(int rt) {sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;}int new_node(int value) {cnt++;val[cnt] = value, sz[cnt] = 1, key[cnt] = rnd();return cnt;}void split(int rt, int maxn, int &x, int &y) {if (!rt) {x = y = 0;return ;}if (sz[ls[rt]] < maxn) {x = rt;split(rs[rt], maxn - sz[ls[rt]] - 1, rs[x], y);}else {y = rt;split(ls[rt], maxn, x, ls[rt]);}push_up(rt);}int merge(int x, int y) {if (!x || !y) {return x | y;}if (key[x] < key[y]) {rs[x] = merge(rs[x], y);push_up(x);return x;}else {ls[y] = merge(x, ls[y]);push_up(y);return y;}}void insert(int pos, int value) {int x, y;split(root, pos, x, y);root = merge(merge(x, new_node(value)), y);}int get_num(int rank) {int x, y, z, ans;split(root, rank, x, y);split(y, 1, y, z);ans = val[y];root = merge(merge(x, y), z);return ans;}void build(int n) {for (int i = 1; i <= n; i++) {root = merge(root, new_node(i));}}
}tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n, m, q, cnt = 0;cin >> n;for (int i = 1; i <= n; i++) {cin >> str[++cnt];}tree.build(cnt);cin >> m;for (int i = 1, pos; i <= m; i++) {cin >> str[++cnt] >> pos;tree.insert(pos, cnt);}cin >> q;for (int i = 1, x; i <= q; i++) {cin >> x;cout << str[tree.get_num(x)] << "\n";}return 0;
}

P2596 [ZJOI2006]书架

#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;mt19937 rnd(233);int minn, maxn;struct Tree {int ls[N], rs[N], val[N], sz[N], key[N], root, cnt;void push_up(int rt) {sz[rt] = sz[ls[rt]] + sz[rs[rt]] + 1;}int new_node(int value, int pos) {val[value] = pos, sz[value] = 1, key[value] = rnd();return value;}void split(int rt, int value, int &x, int &y) {if (!rt) {x = y = 0;return ;}if (val[rt] <= value) {x = rt;split(rs[rt], value, rs[rt], y);}else {y = rt;split(ls[rt], value, x, ls[rt]);}push_up(rt);}int merge(int x, int y) {if (!x || !y) {return x | y;}if (key[x] < key[y]) {rs[x] = merge(rs[x], y);push_up(x);return x;}else {ls[y] = merge(x, ls[y]);push_up(y);return y;}}int get_num(int rt, int rank) {while (rt) {if (sz[ls[rt]] + 1 == rank) {break;}if (sz[ls[rt]] >= rank) {rt = ls[rt];}else {rank -= sz[ls[rt]] + 1;rt = rs[rt];}}return rt;}int get_num(int rank) {return get_num(root, rank);}void insert(int value, int pos) {int x, y;split(root, pos, x, y);root = merge(merge(x, new_node(value, pos)), y);}void update(int value, int op) {int x, y, z;split(root, val[value], x, z);split(x, val[value] - 1, x, y);if (op) {val[value] = --minn;root = merge(merge(y, x), z);}else {val[value] = ++maxn;root = merge(merge(x, z), y);}}void reverse(int value, int op) {if (!op) {return ;}if (op == 1) {int x, y, z, w;split(root, val[value], x, z);split(x, val[value] - 1, x, y);int t = get_num(z, 1);split(z, val[t], z, w);swap(val[y], val[z]);root = merge(merge(x, z), merge(y, w));}else {int x, y, z, w;split(root, val[value] - 1, x, z);split(z, val[value], z, w);int t = get_num(x, sz[x]);split(x, val[t] - 1, x, y);swap(val[y], val[z]);root = merge(merge(x, z), merge(y, w));}}int get_rank(int value) {int x, y, ans;split(root, val[value] - 1, x, y);ans = sz[x];root = merge(x, y);return ans;}
}tree;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n, m;scanf("%d %d", &n, &m);minn = 1, maxn = n;for (int i = 1, x; i <= n; i++) {scanf("%d", &x);tree.insert(x, i);}char op[10];for (int i = 1, s, t; i <= m; i++) {scanf("%s %d", op, &s);if (op[0] == 'T') {tree.update(s, 1);}else if (op[0] == 'B') {tree.update(s, 0);}else if (op[0] == 'I') {scanf("%d", &t);tree.reverse(s, t);}else if (op[0] == 'A') {printf("%d\n", tree.get_rank(s));}else {printf("%d\n", tree.get_num(s));}}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313911.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[NewLife.XCode]分表分库(百亿级大数据存储)

NewLife.XCode是一个有15年历史的开源数据中间件&#xff0c;支持netcore/net45/net40&#xff0c;由新生命团队(2002~2019)开发完成并维护至今&#xff0c;以下简称XCode。整个系列教程会大量结合示例代码和运行日志来进行深入分析&#xff0c;蕴含多年开发经验于其中&#xf…

Java -- 泛型

1、什么是泛型&#xff1f; 泛型&#xff08;Generics&#xff09;是把类型参数化&#xff0c;运用于类、接口、方法中&#xff0c;在调用时传入具体的类型。 泛型就是参数化类型 适用于多种数据类型执行相同的代码泛型的类型在使用时指定泛型归根到底就是“模板” 优点&…

HDU 6703 array(主席树 + set)

array 给一个全排列&#xff0c;接下来有两种操作&#xff1a; 一、把pospospos位置上的值10,000,00010,000,00010,000,000。 二、查询[1,r][1, r][1,r]区间&#xff0c;没有出现的且≥k\geq k≥k的最小值是多少。 考虑用主席树 set 求解&#xff0c; 先建立一颗主席树&a…

你的技术债还了吗?

什么是技术债&#xff1f;技术债是由沃德坎宁安在1992年提出&#xff0c;指我们在软件架构或代码编写过程中有意无意地做了错误的决策。随着时间的累积&#xff0c;这种错误会越来越多&#xff0c;就像背负了很多债务一样。技术债的危害技术债同财务债一样&#xff0c;是有利息…

拿 C# 搞函数式编程

最近闲下来了&#xff0c;准备出一个 C# 搞 FP 的合集。本合集所有代码均以 C# 8 为示例。可能你说&#xff0c;为什么要这么做呢&#xff1f;回答&#xff1a;为了好玩。另外&#xff0c;意义党们请 gun cu ke&#xff01;C# 有委托&#xff0c;而且有 Func<> 和 Action…

P3250 [HNOI2016]网络(利用堆建线段树 + 树剖)

P3250 [HNOI2016]网络 做法有点神奇&#xff01;&#xff01;&#xff01;利用堆作为节点建立一颗线段树&#xff0c;用堆维护线段树上点的信息。 说说查询操作&#xff0c;我们的目的是要查询&#xff0c;没有经过这个点的事件最大值&#xff0c;考虑如何维护。 我们定义线…

CNCF发布K8s项目历程报告,35k贡献者有你吗?

云原生计算基金会 CNCF 首次发布了 Kubernetes 项目历程报告。Kubernetes 托管于 CNCF&#xff0c;它是目前使用最广泛的容器编排平台&#xff0c;通常被称为“云端 Linux”&#xff0c;CNCF 介绍此报告旨在客观地评估 Kubernetes 项目的状态以及 CNCF 如何影响 Kubernetes 的发…

NUMTRYE - Number Theory (Easy)

NUMTRYE - Number Theory (Easy) Hard 版本就是用 pollard_rho 分解质因子。 f(n)∏(pi2ei11)f(n) \prod(p_i ^{2e_i 1} 1)f(n)∏(pi2ei​1​1)&#xff0c;g(n)∑i1nngcd⁡(n,i)g(n) \sum\limits_{i 1} ^{n} \frac{n}{\gcd(n, i)}g(n)i1∑n​gcd(n,i)n​&#xff0c;pip…

API 和 SPI

简介&#xff1a; API&#xff1a;Application Programming Interface应用程序接口 SPI&#xff1a;Service Provider Interface服务商提供接口 JDK中有描述&#xff0c; the API is the description of classes/interfaces/methods/… that you call and use to achieve a go…

编程语言这一年

最近开源中国&#xff08;OSCHINA&#xff09;在庆祝 11 周年生日&#xff0c;编辑部借着这个机会梳理了一下这一年来我们追过的那些开源界/开发界的热点新闻&#xff0c;算作一个阶段性小结。&#xff08;其实只有 9 个月&#xff5e;&#xff09;开源中国是目前国内为数不多深…

(CCPC 2020 网络选拔赛)HDU 6900 Residual Polynomial(分治 + NTT)

Residual Polynomial 写出所有的fi(x)f_i(x)fi​(x)出来&#xff0c;fi,jf_{i, j}fi,j​表示fi(x)f_i(x)fi​(x)的第jjj项系数 {f1,0f1,1f1,2…f1,n−1f1,nf2,0f2,1f2,2…f2,n−1f2,nf3,0f3,1f3,2…f3,n−1f3,n⋮⋮⋮⋱⋮⋮fn−1,0fn−1,1fn−1,2…fn−1,n−1fn−1,nfn,0fn,1f…

使用Elastic APM监控你的.NET Core应用

前言在应用实际的运维过程中&#xff0c;我们需要更多的日志和监控来让我们对自己的应用程序的运行状况有一个全方位的了解。然而对于大部分开发者而言&#xff0c;平时大家所关注的更多的是如何更优雅的实现业务&#xff0c;或者是如何让应用的响应速度更快等等与编码相关的技…

HDU 6755 Fibonacci Sum(二次剩余 + 二项式展开)

Fibonacci Sum 斐波那契通项有an15((152)n−(1−52)n)(15)k∑i0n((152)ic−(1−52)ic)kA152,B1−52(15)k∑i0n∑j0k(−1)k−jCkjAicjBic(k−j)(15)k∑j0k(−1)k−jCkj(∑i0nAcjiBc(k−j)i)斐波那契通项有a_n \frac{1}{\sqrt 5}\left((\frac{1 \sqrt 5}{2}) ^ n - (\frac{1 - …

Java 8 新的时间处理API

一&#xff1a;时间日期API的演进&#xff0c;及存在的问题 JDK 1.0 时期&#xff1a; 对于日期和时间的支持只能依赖于java.util.Date类。它的最小精度是毫秒起始年份为1900年&#xff0c;起始月份为0。20180822表示为new Date (118,7,22)返回值使用JVM默认时区&#xff1a;…

ASP.NET Core结合Nacos来完成配置管理和服务发现

前言今年4月份的时候&#xff0c;和平台组的同事一起调研了一下Nacos&#xff0c;也就在那个时候写了.net core版本的非官方版的SDK。虽然公司内部由于某些原因最后没有真正的用起来&#xff0c;但很多人还是挺看好的。在和镇汐大大沟通后&#xff0c;决定写一篇博客简单介绍一…

2020 ICPC 济南 F. Gcd Product

Gcd Product Cm∑i1mAgcd⁡(i,m)Bgcd⁡(k1−i,m)∑d1∣mAd1∑d2∣mBd2∑i1m([gcd⁡(id1,md1)1][d1∣i])([gcd⁡(m1−id2,md2)1][d2∣m1−i])∑d1∣mAd1∑d2∣mBd2∑k1∣md1μ(k1)∑k2∣md2μ(k2)∑i1m([d1∣i][k1∣id1])([d2∣m1−i][k2∣m1−id2])T1d1k1,T2d2k2∑T1∣m∑d1∣T…

Java 时间处理

时区、冬令时和夏令时、时间戳 时间戳 距离一个标准参照时间经过的秒数&#xff08;毫秒数&#xff09; 有两个常用参照时间&#xff1a; 1970-01-01 00:00:00 应用最广泛的时间戳参照点2001-01-01 00:00:00 常被苹果系统使用 注意&#xff1a;以上时间节点皆采用UTC的标准时…

试试这个Excel知识测验,得分超过80分算你赢

大家可能都知道&#xff0c;全世界使用Excel的用户超过了10亿。Excel的知识真所谓是博大精深&#xff0c;并且还很有趣味。我最近编写了一个Excel小工具&#xff0c;可以让大家可以在Excel里面进行各种知识小测验&#xff0c;并且与全世界的高手一比高低。这个小工具&#xff0…

SimpleDateFormat与线程安全

SimpleDateFormat不是线程安全的。 SimpleDateFormat(下面简称sdf)类内部有一个Calendar对象引用&#xff0c;它用来储存和这个sdf相关的日期信息&#xff0c;例如sdf.parse(dateStr)&#xff0c;sdf.format(date) 诸如此类的方法参数传入的日期相关String, Date等等&#xff…

几道偏序问题(数据结构)

P3157 [CQOI2011]动态逆序对 #include <bits/stdc.h>using namespace std;typedef long long ll;const int N 1e5 10;int root[N], ls[N << 8], rs[N << 8], sum[N << 8], cnt;int n, m, pos[N];inline int lowbit(int x) {return x & (-x); }v…