Splay P3369 【模板】普通平衡树(Treap/SBT)
题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
插入x数
删除x数(若有多个相同的数,因只删除一个)
查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)
查询排名为x的数
求x的前驱(前驱定义为小于x,且最大的数)
- 求x的后继(后继定义为大于x,且最小的数)
输入输出格式
输入格式:
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号( 1 \leq opt \leq 61≤opt≤6 )
输出格式:
对于操作3,4,5,6每行输出一个数,表示对应答案
输入输出样例
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
106465
84185
492737
说明
时空限制:1000ms,128M
1.n的数据范围: n \leq 100000n≤100000
2.每个数的数据范围: [-{10}^7, {10}^7][−107,107]
来源:Tyvj1728 原名:普通平衡树
在此鸣谢
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; const int N = 1e5+;
const int INF = ; int n, opt, x;
struct NODE
{
NODE *fa;
NODE *son[];
int siz;
int num, cnt;
}node[N]; typedef NODE* Tree;
Tree Root, now_node, null; inline void init()
{
Root = now_node = null = node;
null->son[] = null->son[] = null;
} inline int read()
{
char c = getchar(); int num = , f = ;
for(; !isdigit(c); c = getchar())
f = c == '-' ? - : f;
for(; isdigit(c); c = getchar())
num = num * + c - '';
return num * f;
} inline Tree new_node(int num, Tree fa)
{
++ now_node;
now_node->siz = ;
now_node->num = num;
now_node->fa = fa;
now_node->son[] = now_node->son[] = null;
return now_node;
} inline bool getgx(Tree root)
{
return root->fa->son[] == root;
} inline void connect(Tree root, Tree fa, bool flag)
{
if(fa != null)
fa->son[flag] = root;
else
Root = root;
root->fa = fa;
} inline void update(Tree root)
{
root->siz = root->son[]->siz + root->son[]->siz + root->cnt;
} inline void rotate(Tree root)
{
Tree fa = root->fa;
bool a = getgx(root), b = !a;
connect(root->son[b], fa, a);
connect(root, fa -> fa, getgx(fa));
connect(fa, root, b);
update(fa);
update(root);
if(root->fa == null)
Root = root;
} inline void splay(Tree root, Tree goal)
{
for(; root->fa != goal; rotate(root))
if(root->fa->fa != goal)
rotate(getgx(root) == getgx(root->fa) ? root->fa : root);
} void insert(int num)
{
if(Root == null)
{
Root = new_node(num, null);
Root->cnt = ;
}
else
{
Tree root = Root;
for(; root->num != num; root = root->son[num > root->num])
{
++ (root->siz);
if(root->son[num > root->num] == null)
root->son[num > root->num] = new_node(num, root);
}
++ (root->cnt);
splay(root, null);
}
} void erase(int num)
{
if(Root == null)
return;
else
{
Tree root = Root;
for(;root != null && root->num != num; root = root->son[num > root->num]);
if(root == null)
return;
splay(root, null);
-- (root->cnt);
if(!root->cnt)
{
if(root->son[] != null)
{
Tree tmp = root->son[];
for(;tmp->son[] != null; tmp = tmp->son[]);
splay(tmp, null);
Root->son[] = root->son[];
if(Root->son[] != null)
Root->son[]->fa = Root;
}
else
{
Root = root->son[];
Root->fa = null;
}
}
}
} inline int query_rank(int num)
{
int rank = ;
for(Tree root = Root; root != null; root = root->son[num > root->num])
{
if(num == root->num)
return rank + root->son[]->siz + ;
if(num > root->num)
rank += root->son[]->siz + root->cnt;
}
return rank;
} inline int query_num(int rank)
{
for(Tree root = Root; root != null; )
{
if(rank <= root->son[]->siz)
root = root->son[];
else if(rank > root->son[]->siz + root->cnt)
rank -= root->son[]->siz + root->cnt, root = root->son[];
else
return root->num;
}
} inline int query_pre(int x)
{
int pre = -INF;
for(Tree root = Root; root != null; root = root->son[x > root->num])
{
if(x > root->num)
pre = max(pre, root->num);
}
return pre;
} inline int query_nxt(int x)
{
int nxt = INF;
for(Tree root = Root; root != null; root = root->son[x >= root->num])
{
if(root->num > x)
nxt = min(nxt, root->num);
}
return nxt;
} int main()
{
init();
n = read();
for(int i = ; i <= n; ++ i)
{
opt = read(), x = read();
switch(opt)
{
case :
insert(x); break;
case :
erase(x); break;
case :
printf("%d\n", query_rank(x)); break;
case :
printf("%d\n", query_num(x)); break;
case :
printf("%d\n", query_pre(x)); break;
default:
printf("%d\n", query_nxt(x));
}
}
return ;
}
Splay P3369 【模板】普通平衡树(Treap/SBT)的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- [luogu P3369]【模板】普通平衡树(Treap/SBT)
[luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...
- AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...
- 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...
- 红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) 喜闻乐见拿到了luo ...
- P3369 【模板】普通平衡树 Treap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...
- 平衡树简单教程及模板(splay, 替罪羊树, 非旋treap)
原文链接https://www.cnblogs.com/zhouzhendong/p/Balanced-Binary-Tree.html 注意是简单教程,不是入门教程. splay 1. 旋转: 假设 ...
随机推荐
- Spring Cloud Alibaba学习笔记(2) - Nacos服务发现
1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...
- GOF学习笔记1:术语
1.abstract class 抽象类定义了一个接口,把部分或全部实现留给了子类,不能实例化. 2.abstract coupling 抽象耦合如果一个类A引用了另一个抽象类B,那么就说A是抽象耦 ...
- Python查看模块
1.查看Python所有内置模块 按以下链接打开,每个模块有介绍,可以选择不同的版本 https://docs.python.org/3.6/library/index.html 2.查看Python ...
- git本地下载github上的某项目的部分资源
注意以下命令要在git bash下进行,不要是cmd,或者是powershell. cd 到某个目录下 git init git remote add -f origin <url> g ...
- iOS - 总结适配IOS10需要注意的问题
1.自动管理证书 首先要说的就是Xcode8.打开Xcode8最明显的就是Targets-->General下的自动管理证书模块.以前对于新手来说无论是开发还是打包都必须要被苹果的开发签名系统虐 ...
- OSI协议与TCP\IP协议之间的关系
OSI协议 TCP\IP协议 五层协议(学习参考) 应用层 应用层 应用层 表示层 会话层 运输层 运输层 运输层 网络层 网络层 网络层 数据链路层 网络接口层 数据链路层 物理层 物理层 基于五层 ...
- 巧用XML格式数据传入存储过程转成表数据格式
1.首先将后台数据转成对应的XML数据格式 /// <summary> /// 集合转XML数据格式 /// </summary> /// <param name=&qu ...
- Spring+Dubbo+TestNG接口测试初探
最近因工作原因,需要测试dubbo接口,通过公司同事写的框架,再结合度娘的帮助,自己做了一些总结记录. 通过下文意在说明如何搭建一个spring + dubbo + testng的测试环境,并完成一个 ...
- git使用方法(持续更新)
2018/5/9 最基本的操作: 添加文件.文件夹.所有文件: git add test.py //添加单个文件 git add src //添加文件夹 git add . ...
- python字典添加元素和删除元素
1. 添加字典元素 方法一:直接添加,给定键值对 #pycharm aa = {'人才':60,'英语':'english','adress':'here'} print(aa) # {'人才': 6 ...