#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. ehcache 简介和基本api使用

    文章转载自: https://blog.csdn.net/zhouzhiwengang/article/details/59838105 1.ehcahce简介 在开发高并发量,高性能的网站应用系统时 ...

  2. 缺陷管理工具Jira安装参考

      1安装简介 1.1方案/流程简介 需要依赖安装数据库,可以是mysql,orace或sqlserver.以mysql为例说明. 各模块各阶段安装任务说明如下: 安装模块 说明 jira 项目与事务 ...

  3. 机器学习之决策树_CART算法

    目录 3.CART算法(classification and regression tree tree) 3.1 CART生成算法(回归树生成和分类树生成) 3.2 CART剪枝 决策树基本知识参考, ...

  4. DNS及DNS有什么作用

    什么是DNS,DNS有什么作用: DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直 ...

  5. Winform关于未找到元数据文件.exe和不包含适合于入口点的静态“Main”方法

    在三层架构中ItcastCaterModel项目是被其他项目引用的,所以输出类型为类库.

  6. Linux 服务器中木马及木马清除

    1.查看流量图发现问题 查看的时候网页非常卡,有的时候甚至没有响应 2.top动态查看进程 我马上远程登录出问题的服务器,远程操作很卡,网卡出去的流量非常大,通过top发现了一个异常的进程占用资源比较 ...

  7. 用Eclipse在Weka中嵌入新算法

    本文介绍添加一个新算法到Weka集成环境中的过程,并能在GUI中运行并显示其结果.想做到这一点有两种方法,一是用ANT命令生成新的weka.jar(稍后写教程),二是用IDE(Eclipse或NetB ...

  8. Wireshark 过滤器语法

    wireshark有两种过滤器: 捕捉过滤器(CaptureFilters):用于决定将什么样的信息记录在捕捉结果中. 显示过滤器(DisplayFilters):用于在捕捉结果中进行详细查找. 捕捉 ...

  9. animation特效

    在小程序中的使用: <view class='test1'> <image src='/images/light.png'></image> </view&g ...

  10. iOS 在工程内部创建一个静态库target

    当你在开发项目的时候需要把公用的东西打包出来,其他项目方便使用的时候,打包成静态库是你的最优选择,在工程内部开发的时候新建一个target进行静态库的开发可以使你的开发调试更加方便而不是单独新建一个工 ...