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

思路:

  劳资敲了一个多星期;

  劳资终于a了;

  劳资一直不a是因为一个小错误;

  劳资最后看的模板;

  劳资现在很愤怒;

  劳资不想谈思路!!!

来,上代码:

  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5. #define maxn 1000005
  6.  
  7. struct SplayTreeNodeType {
  8. int w,key,opi,size,ch[];
  9. };
  10. struct SplayTreeNodeType tree[maxn];
  11.  
  12. int n,root,tot;
  13.  
  14. inline void updata(int now)
  15. {
  16. tree[now].size=tree[now].w;
  17. if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
  18. if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
  19. }
  20.  
  21. inline int pre()
  22. {
  23. int now=tree[root].ch[];
  24. while(tree[now].ch[]) now=tree[now].ch[];
  25. return now;
  26. }
  27.  
  28. inline int suc()
  29. {
  30. int now=tree[root].ch[];
  31. while(tree[now].ch[]) now=tree[now].ch[];
  32. return now;
  33. }
  34.  
  35. inline int getson(int now)
  36. {
  37. return tree[tree[now].opi].ch[]==now;
  38. }
  39.  
  40. void rotate(int now)
  41. {
  42. int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
  43. tree[opi].ch[pos]=tree[now].ch[pos^];
  44. tree[tree[opi].ch[pos]].opi=opi;
  45. tree[now].ch[pos^]=opi;tree[opi].opi=now;
  46. tree[now].opi=fopi;
  47. if(fopi) tree[fopi].ch[tree[fopi].ch[]==opi]=now;
  48. updata(opi),updata(now);
  49. }
  50.  
  51. void splay(int now)
  52. {
  53. for(int opi;opi=tree[now].opi;rotate(now))
  54. {
  55. if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
  56. }
  57. root=now;
  58. }
  59.  
  60. int rank(int x)
  61. {
  62. int now=root,ans=;
  63. while()
  64. {
  65. if(x<tree[now].key) now=tree[now].ch[];
  66. else
  67. {
  68. ans+=tree[now].ch[]?tree[tree[now].ch[]].size:;
  69. if(x==tree[now].key)
  70. {
  71. splay(now);
  72. return ans+;
  73. }
  74. ans+=tree[now].w;
  75. now=tree[now].ch[];
  76. }
  77. }
  78. }
  79.  
  80. int rank_(int x)
  81. {
  82. int now=root;
  83. while()
  84. {
  85. if(tree[now].ch[]&&x<=tree[tree[now].ch[]].size) now=tree[now].ch[];
  86. else
  87. {
  88. int tmp=(tree[now].ch[]?tree[tree[now].ch[]].size:)+tree[now].w;
  89. if(x<=tmp) return tree[now].key;
  90. x-=tmp;now=tree[now].ch[];
  91. }
  92. }
  93. }
  94.  
  95. inline void clear(int now)
  96. {
  97. tree[now].ch[]=tree[now].ch[]=tree[now].w=tree[now].size=tree[now].key=tree[now].opi=;
  98. }
  99.  
  100. inline void create(int x)
  101. {
  102. tree[++tot].key=x;
  103. tree[tot].w=tree[tot].size=;
  104. tree[tot].ch[]=tree[tot].ch[]=tree[tot].opi=;
  105. }
  106.  
  107. void insert(int x)
  108. {
  109. if(!root) create(x),root=tot;
  110. else
  111. {
  112. int now=root,opi=;
  113. while()
  114. {
  115. if(tree[now].key==x)
  116. {
  117. tree[now].w++;
  118. tree[now].size++;
  119. splay(now);
  120. break;
  121. }
  122. opi=now;
  123. now=tree[now].ch[x>tree[opi].key];
  124. if(!now)
  125. {
  126. create(x);
  127. tree[tot].opi=opi;
  128. tree[opi].ch[x>tree[opi].key]=tot;
  129. splay(tot);
  130. break;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. void del(int x)
  137. {
  138. int t=rank(x);
  139. if(tree[root].w>)
  140. {
  141. tree[root].w--;
  142. tree[root].size--;
  143. return ;
  144. }
  145. if(!tree[root].ch[]&&!tree[root].ch[])
  146. {
  147. clear(root);
  148. root=;
  149. return ;
  150. }
  151. if(!tree[root].ch[])
  152. {
  153. int tmp=root;
  154. root=tree[root].ch[];
  155. tree[root].opi=;
  156. clear(tmp);
  157. return ;
  158. }
  159. if(!tree[root].ch[])
  160. {
  161. int tmp=root;
  162. root=tree[root].ch[];
  163. tree[root].opi=;
  164. clear(tmp);
  165. return ;
  166. }
  167. int pre1=pre(),tmp=root;
  168. splay(pre1);
  169. tree[root].ch[]=tree[tmp].ch[];
  170. tree[tree[tmp].ch[]].opi=root;
  171. clear(tmp);updata(root);
  172. }
  173.  
  174. inline void in(int &now)
  175. {
  176. register int if_z=;now=;
  177. register char Cget=getchar();
  178. while(Cget>''||Cget<'')
  179. {
  180. if(Cget=='-') if_z=-;
  181. Cget=getchar();
  182. }
  183. while(Cget>=''&&Cget<='')
  184. {
  185. now=now*+Cget-'';
  186. Cget=getchar();
  187. }
  188. now*=if_z;
  189. }
  190.  
  191. int main()
  192. {
  193. in(n);int ty,x;
  194. while(n--)
  195. {
  196. in(ty),in(x);
  197. if(ty==) insert(x);
  198. if(ty==) del(x);
  199. if(ty==) printf("%d\n",rank(x));
  200. if(ty==) printf("%d\n",rank_(x));
  201. if(ty==) insert(x),printf("%d\n",tree[pre()].key),del(x);
  202. if(ty==) insert(x),printf("%d\n",tree[suc()].key),del(x);
  203. }
  204. return ;
  205. }

AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369的更多相关文章

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

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

  2. AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  3. AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937

    [USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...

  4. AC日记——[ZJOI2015]幻想乡战略游戏 洛谷 P3345

    [ZJOI2015]幻想乡战略游戏 思路: 树剖暴力转移: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...

  5. AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203

    [HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  6. AC日记——斐波那契数列 洛谷 P1962

    斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...

  7. AC日记——[JLOI2014]松鼠的新家 洛谷 P3258

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  8. AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  9. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  10. AC日记——让我们异或吧 洛谷 P2420

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

随机推荐

  1. Federated引擎

    Federated就像他的名字所说“联盟”,其作用就是把两个不同区域的数据库联系起来,以至可以访问在远程数据库的表中的数据,而不是本地的表. 1.进入mysql命令行,查看是否已安装Federated ...

  2. UVA11825 Hacker's Crackdown 二进制集合+关于子集的动态规划

    题意:有N台服务器,全部服务器都直接运行着完全相同的N个任务.对于每台电脑,你都可以进行“一次”操作,使得某(自己选定)一种任务停止,且同时会使得其他和这台服务器直接相连的电脑上面相同的服务完全终止. ...

  3. 读书笔记jvm探秘之二: 对象创建

    对象是面向对象设计语言无法回避的东西,可见其重要性,JAVA的对象相较于C++来说,不算很复杂,但是我们看到一句话背后往往有很多东西值得探讨(NEW关键字). 对象如何被创建? 首先一句简单的NEW语 ...

  4. Codeforces 653G Move by Prime 组合数学

    题意: 有一个长度为\(n\)的正整数序列\(a\),有这样一种操作: 每次可以选序列中的某一个数乘上或除以某一个素数. 求对于每一个子序列使其所有元素相等的最少操作次数之和. 分析: 因为两个素数之 ...

  5. log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor

    与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...

  6. jmeter快捷键

    快捷键 功能 备注 Ctrl + C 复制 可复制组件 Ctrl + V 粘贴 可粘贴组件 Ctrl + Shift + C 复制粘贴当前组件到下一行   Ctrl + R 运行测试计划   Ctrl ...

  7. Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268

    https://blog.csdn.net/wei_zhi/article/details/52745268

  8. backpropagation算法示例

    backpropagation算法示例 下面举个例子,假设在某个mini-batch的有样本X和标签Y,其中\(X\in R^{m\times 2}, Y\in R^{m\times 1}\),现在有 ...

  9. CentOS7 haproxy+keepalived实现高可用集群搭建

    一.搭建环境 CentOS7 64位 Keepalived 1.3.5 Haproxy 1.5.18 后端负载主机:192.168.166.21 192.168.166.22 两台节点上安装rabbi ...

  10. BZOJ 2190:[SDOI2008]仪仗队(欧拉函数)

    [SDOI2008]仪仗队 Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视 ...