无旋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 ...
随机推荐
- Lintcode105 Copy List with Random Pointer solution 题解
[题目描述] A linked list is given such that each node contains an additional random pointer which could ...
- sublime设置html在浏览器打开
1.快捷键 Ctrl+Shift+P输入:pcip选择第一个 2.输入:View In Browser安装此插件 3.菜单栏Preferences->Key Bindings 输入:[{ &q ...
- js Infinity 属性
Infinity 属性用于存放表示正无穷大的数值. 说明 无法使用 for/in 循环来枚举 Infinity 属性,也不能用 delete 运算符来删除它. Infinity 不是常量,可以把它设置 ...
- Python自学:第三章 在列表末尾添加元素与在列表中插入元素
motorcycles = ['honda', 'yamaha' ,'suzuki'] motorcycles.insert(0, "ducati") print(motorcyc ...
- 小程序组件 scroll-view 滑动
小程序组件 scroll-view 中分别有上下竖向滑动和左右横向滑动之分,在这次项目中刚好需要用到横向滑动,但在测试过程中发现横向滑动没有了效果(静止在那里没移动过),经调试发现: 1.scroll ...
- laravel 资源篇
转自:https://github.com/qianyugang/learn-laravel # Learn-Laravel — 学习资料和开源项目集 ## Laravel 学习资料 ### 官方网站 ...
- MySQL自增序列-亲试ok
#1.创建sequence表,公共的 DROP TABLE IF EXISTS sequence; CREATE TABLE sequence ( name VARCHAR(30) NOT ...
- 记录一个下最近用tensorflow的几个坑
1, softmax_cross_entropy_with_logits 的中的logits=x*w+b,其中w应该是[nfeats,nclass],b是[nclass]是对输出的每个类上logits ...
- javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names
问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误 解决方案一:使用java.net.HttpURLConnection i ...
- jQuery抽奖插件 jQueryRotate
实现代码 网页中引用 <script type="text/javascript" src="js/jquery.min.js"></scri ...