#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. Java(1)JDK安装

    1.安装JDK开发环境 下载网站:http://www.oracle.com/ 开始安装JDK: 修改安装目录如下: 确定之后,单击"下一步". 注:当提示安装JRE时,可以选择不 ...

  2. 理解AJAX的原理

    1.原生ajax异步请求(ajax的原理) (异步请求:无跳转,无刷新....)通过XMLHttpRequst对象,向服务器发送请求.XMLHttpRequest对象具有一些属性和方法. 1.首先创建 ...

  3. React项目中实现右键自定义菜单

    最近在react项目中需要实现一个,右键自定义菜单功能.找了找发现纯react项目里没有什么工具可以实现这样的功能,所以在网上搜了搜相关资料.下面我会附上完整的组件代码. (注:以下代码非本人原创,具 ...

  4. MVC _Ajax的使用【七】

    一.本篇主要写的是在MVC项目中一种ajax的使用方法 1.  首先在控制器中创建两个方法,showCreate()和AddUserInfo() using System; using System. ...

  5. 安装Go插件遇到的问题及解决方法

    1. 问题:在 Windows 平台下使用 go get 安装sqlite3 驱动时报错 The remote end hung up unexpectedly ? 原因及解决方法: 原因可能有两种: ...

  6. URI和URL之间的区别

    URI的全称为统一资源标识符(Uniform Resource Identifier),是一个用于标识某一互联网资源名称的字符串. 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交 ...

  7. 漏洞复现——Apache HTTPD多后缀解析漏洞

    漏洞原理:Apache Httpd支持一个文件拥有多个后缀,不同的后缀执行不同的命令,也就是说当我们上传的文件中只要后缀名含有php,该文件就可以被解析成php文件,利用Apache Httpd这个特 ...

  8. 第6天【egrep、bash环境配置及脚本、vim编辑器】

    bash环境配置及脚本(02)_recv bash环境配置及脚本(02)_recv bash环境配置文件: 按生效范围划分,存在两类: 全局配置: /etc/profile /etc/bashrc 个 ...

  9. apply、call、bind的区别

    apply.call.bind这三种方法一般用来改变this指向. apply()方法接收两个参数,一个是函数运行的作用域this,另一个是参数数组 call()方法接收两个参数,一个是函数运行的作用 ...

  10. call、apply、bind,你有多了解?

    call.apply.bind 1.相同也不同 我们先从浅显的部分开始讲, 这三个方法都可以改变this的指向,都可以进行传参,第一个参数都是修改this的指向 call() 和 apply() 改变 ...