#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的更多相关文章

  1. [转载]无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    转自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182631.html 1500: [NOI2005]维修数列 Time Limit: 10 Sec  Mem ...

  2. [转载]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    转载自ZZH大佬,原文:http://www.cnblogs.com/LadyLex/p/7182491.html 今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和t ...

  3. [您有新的未分配科技点]无旋treap:从好奇到入门(例题:bzoj3224 普通平衡树)

    今天我们来学习一种新的数据结构:无旋treap.它和splay一样支持区间操作,和treap一样简单易懂,同时还支持可持久化. 无旋treap的节点定义和treap一样,都要同时满足树性质和堆性质,我 ...

  4. Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...

  5. 【算法学习】Fhq-Treap(无旋Treap)

    Treap——大名鼎鼎的随机二叉查找树,以优异的性能和简单的实现在OIer们中广泛流传. 这篇blog介绍一种不需要旋转操作来维护的Treap,即无旋Treap,也称Fhq-Treap. 它的巧妙之处 ...

  6. 无旋treap的区间操作实现

    最近真的不爽...一道维修数列就做了我1上午+下午1h+1晚上+晚上1h+上午2h... 一道不错的自虐题... 由于这一片主要讲思想,代码我放这里了 不会无旋treap的童鞋可以进这里 呵呵... ...

  7. 无旋treap的简单思想以及模板

    因为学了treap,不想弃坑去学splay,终于理解了无旋treap... 好像普通treap没卵用...(再次大雾) 简单说一下思想免得以后忘记.普通treap因为带旋转操作似乎没卵用,而无旋tre ...

  8. [BZOJ3223]文艺平衡树 无旋Treap

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...

  9. [您有新的未分配科技点] 无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MB Description Input 输入的第1 行包含两个数N 和M(M ≤20 ...

  10. BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)

    题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...

随机推荐

  1. HADOOP HA 踩坑 - org.apache.hadoop.hdfs.qjournal.protocol.JournalNotFormattedException: Journal Storage Directory /mnt/data1/hadoop/dfs/journal/hdfscluster not formatted

    报错:在journalnode的log中: org.apache.hadoop.hdfs.qjournal.protocol.JournalNotFormattedException: Journal ...

  2. nodeJs的Buffer操作

    再nodejs里,很多类是引入模块才能使用,Buffer是一个全局类,他不需要require引入 Buffer有三种构造函数 //1.在构造函数传一个数字,规定buffer的长度.默认全是16进制的0 ...

  3. vue的技巧代码

    转载:https://segmentfault.com/a/1190000014085613 第一招:化繁为简的Watchers 场景还原: created(){ this.fetchPostList ...

  4. Practical Node.js (2018版) 第8章:Building Node.js REST API Servers

    Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...

  5. 开源项目Zookeeper、Doozer、etcd进行总结

    Jason Wilder的一篇博客对分别对常见的服务发现开源项目Zookeeper.Doozer.etcd进行了总结介绍: Zookeeper是一个用户维护配置信息.命名.分布式同步以及分组服务的集中 ...

  6. CentOS7配置httpd虚拟主机

    本实验旨在CentOS7系统中,httpd-2.4配置两台虚拟主机,主要有以下要求: (1) 提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1: ...

  7. JS中使用时间戳,获取当前日期,计算前一周的日期~

    今天项目中用到了一点 随便记录一下 function timestampToTime(timestamp) { );//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = d ...

  8. Python- - -基础目录

    一.Python.pycharm的介绍与安装. 二.变量.整数.字符串.列表.字典.集合. 三.运算符.格式化输出.流程控制语句. 四.break和continue. 五.range.enumerat ...

  9. html5(四) canvas

    http://www.cnblogs.com/Gyoung/archive/2013/04/08/2994515.html

  10. vscode extensions

    ├─ .obsolete├─ .wlck├─ .wtid├─ abeyuhang.vscode-lesslint-0.0.1├─ abusaidm.html-snippets-0.2.1├─ adam ...