P3369 【模板】普通平衡树(Treap/SBT)

题目描述

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

  1. 插入x数

  2. 删除x数(若有多个相同的数,因只删除一个)

  3. 查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)

  4. 查询排名为x的数

  5. 求x的前驱(前驱定义为小于x,且最大的数)

  6. 求x的后继(后继定义为大于x,且最小的数)

输入输出格式

输入格式:

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号( 1 \leq opt \leq 61≤opt≤6 )

输出格式:

对于操作3,4,5,6每行输出一个数,表示对应答案

输入输出样例

输入样例#1: 复制

  1. 10
  2. 1 106465
  3. 4 1
  4. 1 317721
  5. 1 460929
  6. 1 644985
  7. 1 84185
  8. 1 89851
  9. 6 81968
  10. 1 492737
  11. 5 493598
输出样例#1: 复制

  1. 106465
  2. 84185
  3. 492737

说明

时空限制:1000ms,128M

1.n的数据范围: n \leq 100000n≤100000

2.每个数的数据范围: [-{10}^7, {10}^7][−107,107]

来源:Tyvj1728 原名:普通平衡树

在此鸣谢

code

  1. #include<cstdio>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. const int N = ;
  7. int ch[N][],siz[N],key[N],val[N];
  8. int tn,Root;
  9.  
  10. inline char nc() {
  11. static char buf[],*p1 = buf,*p2 = buf;
  12. return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF : *p1++;
  13. }
  14. inline int read() {
  15. int x = ,f = ;char ch = getchar();
  16. for (; ch<''||ch>''; ch = getchar())
  17. if (ch=='-') f = -;
  18. for (; ch>=''&&ch<=''; ch = getchar())
  19. x = x*+ch-'';
  20. return x * f;
  21. }
  22. inline void pushup(int x) {
  23. siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
  24. }
  25. inline int makenode(int x) {
  26. ++tn;val[tn] = x;siz[tn] = ;key[tn] = rand();return tn;
  27. }
  28.  
  29. int Merge(int x,int y) {
  30. if (!x || !y) return x + y;
  31. if (key[x] < key[y]) {
  32. ch[x][] = Merge(ch[x][],y);
  33. pushup(x); return x;
  34. }
  35. else {
  36. ch[y][] = Merge(x,ch[y][]);
  37. pushup(y); return y;
  38. }
  39. }
  40. void Split(int now,int k,int &x,int &y) {
  41. if (!now) x = y = ;
  42. else {
  43. if (val[now] <= k)
  44. x = now,Split(ch[now][],k,ch[now][],y);
  45. else
  46. y = now,Split(ch[now][],k,x,ch[now][]);
  47. pushup(now);
  48. }
  49. }
  50. inline int getkth(int p,int k) {
  51. while (true) {
  52. if (k == siz[ch[p][]] + ) return p;
  53. if (ch[p][] && k <= siz[ch[p][]]) p = ch[p][];
  54. else k-= ((ch[p][] ? siz[ch[p][]] : ) + ),p = ch[p][];
  55. }
  56. }
  57. int main() {
  58. int x,y,z,opt,k,n = read();
  59. while (n--) {
  60. opt = read(),k = read();
  61. if (opt==) {
  62. Split(Root,k,x,y);
  63. Root = Merge(Merge(x,makenode(k)),y);
  64. }
  65. else if (opt==) {
  66. Split(Root,k,x,y);
  67. Split(x,k-,x,z);
  68. z = Merge(ch[z][],ch[z][]);
  69. Root = Merge(Merge(x,z),y);
  70. }
  71. else if (opt==) {
  72. Split(Root,k-,x,y);
  73. printf("%d\n",siz[x]+);
  74. Root = Merge(x,y);
  75. }
  76. else if (opt==)
  77. printf("%d\n",val[getkth(Root,k)]);
  78. else if (opt==) {
  79. Split(Root,k-,x,y);
  80. printf("%d\n",val[getkth(x,siz[x])]);
  81. Root = Merge(x,y);
  82. }
  83. else {
  84. Split(Root,k,x,y);
  85. printf("%d\n",val[getkth(y,)]);
  86. Root = Merge(x,y);
  87. }
  88. }
  89. return ;
  90. }

P3369 【模板】普通平衡树FHQtreap的更多相关文章

  1. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  2. luoguP3369[模板]普通平衡树(Treap/SBT) 题解

    链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...

  3. 【洛谷P3369】 (模板)普通平衡树

    https://www.luogu.org/problemnew/show/P3369 Splay模板 #include<iostream> #include<cstdio> ...

  4. [luogu3369]普通平衡树(fhq-treap模板)

    解题关键:无旋treap模板. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  5. [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)

    解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...

  6. 【模板】平衡树——Treap和Splay

    二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...

  7. 【洛谷P3369】普通平衡树——Splay学习笔记(一)

    二叉搜索树(二叉排序树) 概念:一棵树,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉搜索树 ...

  8. 洛谷.3369.[模板]普通平衡树(Splay)

    题目链接 第一次写(2017.11.7): #include<cstdio> #include<cctype> using namespace std; const int N ...

  9. 洛谷.3369.[模板]普通平衡树(fhq Treap)

    题目链接 第一次(2017.12.24): #include<cstdio> #include<cctype> #include<algorithm> //#def ...

随机推荐

  1. A. Arya and Bran

    A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. R17下maps新增参数的问题

    今天遇到一个奇怪的问题,我之前写的一个函数在我弟弟的机器上编译出错.代码如下: %%将list [k1,v1,k2,v2...]转换成map {k1=>v1,key2=>v2...} -s ...

  3. GitHub上优秀Android 开源项目

    GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项目很是必要.利用这些项目,有时能够让你 ...

  4. vue2.0:(八)、外卖App弹窗部分知识点总结

    本篇文章是对外卖App弹窗部分知识点的总结. 知识点一:如何从接口取出不同的图片. 答: 1.header.vue: 代码: <ul v-if="seller.supports&quo ...

  5. linux命令行—《命令行快速入门》

    pwd print working directory 打印工作目录 hostname my computer's network name 电脑在网络中的名称 mkdir make director ...

  6. jquery.restrictFieldLength.js

    1.参考资料 http://www.cnblogs.com/aarond/archive/2013/08/02/3234042.html 2.使用举例 //字符控制 $(function () { $ ...

  7. windows7桌面小工具打不开的解决方案

    将任务管理器中的sidebar.exe结束任务: 将C:\Users\用户名\AppData\Local\Microsoft\Windows Sidebar下的settings.ini的文件名修改为任 ...

  8. MVC web api转换JSON 的方法

  9. MVC批量上传文件(使用uploadify)

    <script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jque ...

  10. css设置禁止文字被选中

    // 禁止文字被鼠标选中 moz-user-select: -moz-none; -moz-user-select: none; -o-user-select:none; -khtml-user-se ...