P3369 【模板】普通平衡树FHQtreap
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 原名:普通平衡树
在此鸣谢
code
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- const int N = ;
- int ch[N][],siz[N],key[N],val[N];
- int tn,Root;
- inline char nc() {
- static char buf[],*p1 = buf,*p2 = buf;
- return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF : *p1++;
- }
- inline int read() {
- int x = ,f = ;char ch = getchar();
- for (; ch<''||ch>''; ch = getchar())
- if (ch=='-') f = -;
- for (; ch>=''&&ch<=''; ch = getchar())
- x = x*+ch-'';
- return x * f;
- }
- inline void pushup(int x) {
- siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
- }
- inline int makenode(int x) {
- ++tn;val[tn] = x;siz[tn] = ;key[tn] = rand();return tn;
- }
- int Merge(int x,int y) {
- if (!x || !y) return x + y;
- if (key[x] < key[y]) {
- ch[x][] = Merge(ch[x][],y);
- pushup(x); return x;
- }
- else {
- ch[y][] = Merge(x,ch[y][]);
- pushup(y); return y;
- }
- }
- void Split(int now,int k,int &x,int &y) {
- if (!now) x = y = ;
- else {
- if (val[now] <= k)
- x = now,Split(ch[now][],k,ch[now][],y);
- else
- y = now,Split(ch[now][],k,x,ch[now][]);
- pushup(now);
- }
- }
- inline int getkth(int p,int k) {
- while (true) {
- if (k == siz[ch[p][]] + ) return p;
- if (ch[p][] && k <= siz[ch[p][]]) p = ch[p][];
- else k-= ((ch[p][] ? siz[ch[p][]] : ) + ),p = ch[p][];
- }
- }
- int main() {
- int x,y,z,opt,k,n = read();
- while (n--) {
- opt = read(),k = read();
- if (opt==) {
- Split(Root,k,x,y);
- Root = Merge(Merge(x,makenode(k)),y);
- }
- else if (opt==) {
- Split(Root,k,x,y);
- Split(x,k-,x,z);
- z = Merge(ch[z][],ch[z][]);
- Root = Merge(Merge(x,z),y);
- }
- else if (opt==) {
- Split(Root,k-,x,y);
- printf("%d\n",siz[x]+);
- Root = Merge(x,y);
- }
- else if (opt==)
- printf("%d\n",val[getkth(Root,k)]);
- else if (opt==) {
- Split(Root,k-,x,y);
- printf("%d\n",val[getkth(x,siz[x])]);
- Root = Merge(x,y);
- }
- else {
- Split(Root,k,x,y);
- printf("%d\n",val[getkth(y,)]);
- Root = Merge(x,y);
- }
- }
- return ;
- }
P3369 【模板】普通平衡树FHQtreap的更多相关文章
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- 【洛谷P3369】 (模板)普通平衡树
https://www.luogu.org/problemnew/show/P3369 Splay模板 #include<iostream> #include<cstdio> ...
- [luogu3369]普通平衡树(fhq-treap模板)
解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- 【洛谷P3369】普通平衡树——Splay学习笔记(一)
二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...
- 洛谷.3369.[模板]普通平衡树(Splay)
题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...
- 洛谷.3369.[模板]普通平衡树(fhq Treap)
题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...
随机推荐
- A. Arya and Bran
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- R17下maps新增参数的问题
今天遇到一个奇怪的问题,我之前写的一个函数在我弟弟的机器上编译出错.代码如下: %%将list [k1,v1,k2,v2...]转换成map {k1=>v1,key2=>v2...} -s ...
- GitHub上优秀Android 开源项目
GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要.利用这些项目,有时能够让你 ...
- vue2.0:(八)、外卖App弹窗部分知识点总结
本篇文章是对外卖App弹窗部分知识点的总结. 知识点一:如何从接口取出不同的图片. 答: 1.header.vue: 代码: <ul v-if="seller.supports&quo ...
- linux命令行—《命令行快速入门》
pwd print working directory 打印工作目录 hostname my computer's network name 电脑在网络中的名称 mkdir make director ...
- jquery.restrictFieldLength.js
1.参考资料 http://www.cnblogs.com/aarond/archive/2013/08/02/3234042.html 2.使用举例 //字符控制 $(function () { $ ...
- windows7桌面小工具打不开的解决方案
将任务管理器中的sidebar.exe结束任务: 将C:\Users\用户名\AppData\Local\Microsoft\Windows Sidebar下的settings.ini的文件名修改为任 ...
- MVC web api转换JSON 的方法
- MVC批量上传文件(使用uploadify)
<script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jque ...
- css设置禁止文字被选中
// 禁止文字被鼠标选中 moz-user-select: -moz-none; -moz-user-select: none; -o-user-select:none; -khtml-user-se ...