无旋treap
#ifndef FHQTREAP_H_INCLUDED
#define FHQTREAP_H_INCLUDED //author Eterna
#define Hello The_Cruel_World!
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cmath>
#include<climits>
#include<deque>
#include<functional>
#include<complex>
#include<numeric>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define Pi acos(-1.0)
#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
#define pb(x) push_back(x)
#define lowbit(x) (x & -x)
#define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
#define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int maxn = 1e5;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const double eps = 1e-;
inline int read_int() {
char c;
int ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
inline ll read_ll() {
char c;
ll ret = , sgn = ;
do { c = getchar(); } while ((c < '' || c > '') && c != '-');
if (c == '-') sgn = -; else ret = c - '';
while ((c = getchar()) >= '' && c <= '') ret = ret * + (c - '');
return sgn * ret;
}
struct node {
int value, key, Size, child[];
}arr[maxn + ];
int tot;
inline void Push_Up(int index) {
arr[index].Size = arr[arr[index].child[]].Size + arr[arr[index].child[]].Size + ;
}
void Split_By_Size(int root, int& a, int& b, int k) {
if (!root) {
a = b = ;
return;
}
if (arr[arr[root].child[]].Size < k) a = root, Split_By_Size(arr[root].child[], arr[a].child[], b, k - arr[arr[root].child[]].Size - );
else b = root, Split_By_Size(arr[root].child[], a, arr[b].child[], k);
Push_Up(root);
}
void Split_By_Value(int root, int& a, int& b, int value) {
if (!root) {
a = b = ;
return;
}
if (arr[root].value <= value)a = root, Split_By_Value(arr[root].child[], arr[a].child[], b, value);
else b = root, Split_By_Value(arr[root].child[], a, arr[b].child[], value);
Push_Up(root);
}
void Merge(int& root, int a, int b) {
if (!a || !b) {
root = a + b;
return;
}
if (arr[a].key < arr[b].key)root = a, Merge(arr[root].child[], arr[a].child[], b);
else root = b, Merge(arr[root].child[], a, arr[b].child[]);
Push_Up(root);
}
inline void Insert(int& root, int value) {
int x = , y = , z = ++tot;
arr[z].value = value, arr[z].key = rand(), arr[z].Size = ;
Split_By_Value(root, x, y, value);
Merge(x, x, z);
Merge(root, x, y);
}
inline void Erase(int& root, int value) {
int x = , y = , z = ;
Split_By_Value(root, x, y, value);
Split_By_Value(x, x, z, value - );
Merge(z, arr[z].child[], arr[z].child[]);
Merge(x, x, z);
Merge(root, x, y);
}
int Kth_number(int root, int k) {
while (arr[arr[root].child[]].Size + != k) {
if (arr[arr[root].child[]].Size >= k)root = arr[root].child[];
else k -= (arr[arr[root].child[]].Size + ), root = arr[root].child[];
}
return arr[root].value;
}
int Get_Rank(int& root, int value) {
int x = , y = ;
Split_By_Value(root, x, y, value - );
int res = arr[x].Size + ;
Merge(root, x, y);
return res;
}
int Predecessor(int& root, int value) {
int x = , y = ;
Split_By_Value(root, x, y, value - );
int res = Kth_number(x, arr[x].Size);
Merge(root, x, y);
return res;
}
int Successor(int& root, int value) {
int x = , y = ;
Split_By_Value(root, x, y, value);
int res = Kth_number(y, );
Merge(root, x, y);
return res;
}
int n, op, u, root;
int main()
{
srand();
scanf("%d", &n);
while (n--) {
scanf("%d %d", &op, &u);
if (op == )Insert(root, u);
if (op == )Erase(root, u);
if (op == )printf("%d\n", Get_Rank(root, u));
if (op == )printf("%d\n", Kth_number(root, u));
if (op == )printf("%d\n", Predecessor(root, u));
if (op == )printf("%d\n", Successor(root, u));
}
return ;
} #endif // FHQTREAP_H_INCLUDED
无旋treap的更多相关文章
- [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec Mem ...
- [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...
- [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)
今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- 【算法学习】Fhq-Treap(无旋Treap)
Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...
- 无旋treap的区间操作实现
最近真的不爽...一道维修数列就做了我1上午+下午1h+1晚上+晚上1h+上午2h... 一道不错的自虐题... 由于这一片主要讲思想,代码我放这里了 不会无旋treap的童鞋可以进这里 呵呵... ...
- 无旋treap的简单思想以及模板
因为学了treap,不想弃坑去学splay,终于理解了无旋treap... 好像普通treap没卵用...(再次大雾) 简单说一下思想免得以后忘记.普通treap因为带旋转操作似乎没卵用,而无旋tre ...
- [BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...
- [您有新的未分配科技点] 无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MB Description Input 输入的第1 行包含两个数N 和M(M ≤20 ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
随机推荐
- Cookie和Seesion
会话跟踪技术 1 什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而10 ...
- 【搬运】Wget 命令详解
用过 Linux 系统的对于 wget 不陌生吧,从网上下载资源等操作都是少不了它,它体积小但功能集全,支持 FTP HTTP HTTPS 协议下载方式,支持断点续传 代理服务器. 现在 Window ...
- CSS基础学习(二) 之 width min-width max-width属性
width 1. 设置元素内容区(content area)的宽度. 2. 如果box-szing属性设置为border-box,width表示border area的宽度,如下图 min-width ...
- CSS自定义样式
CSS自定义样式 1. 自定义字体 先将字体文件放到web服务器上,需要时自动下载到用户计算机上 属性:@font-face 例: @font-face{ font-family:myFont; sr ...
- vue 项目 使用sass
1,使用save会在package.json中自动添加. npm install node-sass --save-dev npm install sass-loader --save-dev 注: ...
- Pandas 基础(7) - Group By 分组的相关知识
首先, 引入这节需要的 csv 文件 (已上传) import pandas as pd city_df = pd.read_csv('/Users/rachel/Sites/pandas/py/pa ...
- 这可能是史上最全的css布局教程
标题严格遵守了新广告法,你再不爽,我也没犯法呀!话不多说,直入正题. 所谓布局,其实包含两个含义:尺寸与定位.也就是说,所有与尺寸和定位相关的属性,都可以用来布局. 大体上,布局中会用到的有:尺寸相关 ...
- fabric网络环境启动过程详解
这篇文章对fabric的网络环境启动过程进行讲解,也就是我们上节讲到的启动测试fabric网络环境时运行network_setup.sh这个文件的执行流程 fabric网络环境启动过程详解 上一节我们 ...
- git branch 不显示的原因
转自:https://blog.csdn.net/qq_39671159/article/details/81261049 必须使用git init命令创建仓库,执行git add . 和git co ...
- string部分方法
1.string.lastIndexOf() lastIndexOf 是从string末尾查找,但是返回值仍是首部的位置值. 2.string.replace() 放一个正则匹配会全部替换. 3.st ...