[luogu 3369]普通平衡树(fhq_treap)
题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
插入x数
删除x数(若有多个相同的数,因只删除一个)
查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)
查询排名为x的数
求x的前驱(前驱定义为小于x,且最大的数)
- 求x的后继(后继定义为大于x,且最小的数)
输入输出格式
输入格式:
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号( 1≤opt≤6 1 \leq opt \leq 6 1≤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≤100000 n \leq 100000 n≤100000
2.每个数的数据范围: $[-{10}^7, {10}^7]$
题解:
模板题,用的是fhq_treap。
//Never forget why you start
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#define ll(x) tre[x].child[0]
#define rr(x) tre[x].child[1]
#define son(x,t) tre[x].child[t]
using namespace std;
int n,m,cnt,root;
struct Treap{
int child[],x,size,rev;
}tre[];
void push_up(int root){
tre[root].size=tre[ll(root)].size+tre[rr(root)].size+;
}
int newnode(int x){
cnt++;
tre[cnt].size=;
tre[cnt].rev=rand();
tre[cnt].x=x;
return cnt;
}
void split(int now,int k,int &x,int &y){
if(!now)x=y=;
else{
if(tre[now].x<=k)
x=now,split(rr(now),k,rr(now),y);
else
y=now,split(ll(now),k,x,ll(now));
push_up(now);
}
}
int merge(int x,int y){
if(!x||!y)return x+y;
if(tre[x].rev<tre[y].rev){
rr(x)=merge(rr(x),y);
push_up(x);
return x;
}
else{
ll(y)=merge(x,ll(y));
push_up(y);
return y;
}
}
int find(int root,int k){
int y=tre[ll(root)].size;
if(y+==k)return root;
else if(y>=k)return find(ll(root),k);
else find(rr(root),k-y-);
}
int main(){
int i,j,a,b,c;
srand(time());
scanf("%d",&n);
while(n--){
scanf("%d%d",&i,&j);
if(i==){
split(root,j,a,b);
root=merge(merge(a,newnode(j)),b);
}
if(i==){
split(root,j,a,c);
split(a,j-,a,b);
b=merge(ll(b),rr(b));
root=merge(merge(a,b),c);
}
if(i==){
split(root,j-,a,b);
printf("%d\n",tre[a].size+);
root=merge(a,b);
}
if(i==){
printf("%d\n",tre[find(root,j)].x);
}
if(i==){
split(root,j-,a,b);
printf("%d\n",tre[find(a,tre[a].size)].x);
root=merge(a,b);
}
if(i==){
split(root,j,a,b);
printf("%d\n",tre[find(b,)].x);
root=merge(a,b);
}
}
return ;
}
[luogu 3369]普通平衡树(fhq_treap)的更多相关文章
- [luogu P3391] 文艺平衡树
[luogu P3391] 文艺平衡树 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区 ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [替罪羊树]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- Luogu 3369 我用线段树骗了一道平衡树题……
这篇博客毫无意义-- 只是表达一下我仍然会写树状数组和线段树-- 题目链接 #include <cstdio> #include <cstring> #include < ...
- 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay
题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...
- Luogu P3391 文艺平衡树(Splay or FHQ Treap)
这道题要求区间反转...好东西.. 对于Splay:把l-1旋到根,把r+1旋到根的右儿子,这样r+1的左儿子就是整个区间了,然后对这个区间打个tg 注意要插-Inf和Inf到树里面,防止越界,坐标要 ...
- cyyz: Day 6 平衡树整理
一.平衡树 知识点: ,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方法有红黑树.AVL.替罪羊树.Treap.伸展树等. 最小二叉平衡树的节点的公式如下 F(n)=F(n-1)+F(n- ...
- 2021.07.02 P1383 高级打字机题解(可持久化平衡树)
2021.07.02 P1383 高级打字机题解(可持久化平衡树) 分析: 从可以不断撤销并且查询不算撤销这一骚操作可以肯定这是要咱建一棵可持久化的树(我也只会建可持久化的树,当然,还有可持久化并查集 ...
- [总结] fhq_Treap 学习笔记
无旋版 $Treap$. 只需要两个操作即可达到 $splay$ 的所有功能 1.$split$ 它的主要思想就是把一个 $Treap$ 分成两个. $split$ 操作有两种类型,一种是按照权值分配 ...
随机推荐
- 异常:Project configuration is not up-to-date with pom.xml解决方案
转自:https://www.cnblogs.com/zhujiabin/p/6343423.html 1. Description Resource Path Location ...
- python 基础 列表 增删改查
names = ["aaron", "alex", "james", "meihengfan"]names2 = [1, ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- <c和指针>学习笔记5动态内存分配和预处理器
1 动态内存 比如声明数组得时候,我们需要提前预估数组长度,分配大了浪费,少了就更不好操作了.从而引入动态分配,需要的时候再分配. (1)malloc和free void *malloc(size_t ...
- 【QtAV】QtAV中的工厂模式
QtAV中的各个模块大量使用的工厂模式,下面对其实现进行介绍. 工厂模式的使用 以 VideoRenderer 类为例子,他含有下面3个工厂模式相关的方法,Register方法用于给一个产品<c ...
- [Linux]关于sigprocmask函数的讨论
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- go语言web开发框架_Iris框架讲解(六):Session的使用和控制
在实际的项目开发中,我们会经常有业务场景使用到Session功能.在iris框架中,也为我们提供了方便使用,功能齐全的Session模块.Session模块的源码目录为kataras/iris/ses ...
- 验证控件jQuery Validation Engine调用外部函数验证
在使用jQuery Validation Engine的时候,我们除了使用自带的API之外,还可以自己自定义正则验证.自定义正则验证上一篇已经讲过了,如果想使用自定义函数进行验证怎么办?其实这个控件有 ...
- 前端三部曲之Css-- 1(常见的居中方式)
下面来介绍一下web端页面最常见的居中方式 页面的基本结构:一个简单的div <!DOCTYPE html> <html lang="en"> <he ...
- QuotaExceededError: The quota has been exceeded. localStorage缓存超出限制
今天在项目中遇到了一个问题,localStorage存储超出限制.报错信息如标题.这个是因为最近做了一波优化,把导航栏和一些用户信息本地化存储,都放在localStorage里,也不是每个用户会出现这 ...