1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. #define maxn 1000000
  7. #define INF 2147483647
  8. int n,fa[maxn],val[maxn],c[maxn][2],root,tot,siz[maxn],cnt[maxn];
  9. void Maintain(int x)
  10. {
  11. siz[x]=siz[c[x][0]]+siz[c[x][1]]+cnt[x];
  12. }
  13. void NewNode(int &x,int Fa,int key)
  14. {
  15. x=++tot;
  16. fa[x]=Fa;
  17. c[x][0]=c[x][1]=0;
  18. val[x]=key;
  19. siz[x]=cnt[x]=1;
  20. }
  21. void Rotate(int x,bool flag)
  22. {
  23. int y=fa[x];
  24. c[y][!flag]=c[x][flag];
  25. fa[c[x][flag]]=y;
  26. if(fa[y]){
  27. c[fa[y]][c[fa[y]][1]==y]=x;
  28. }
  29. fa[x]=fa[y];
  30. c[x][flag]=y;
  31. fa[y]=x;
  32. Maintain(y);
  33. Maintain(x);
  34. }
  35. void Splay(int x,int goal)
  36. {
  37. if(!x){
  38. return;
  39. }
  40. int y;
  41. while((y=fa[x])!=goal){
  42. if(fa[y]==goal){
  43. Rotate(x,c[y][0]==x);
  44. }
  45. else{
  46. if((c[y][0]==x)==(c[fa[y]][0]==y)){
  47. Rotate(y,c[fa[y]][0]==y);
  48. }
  49. else{
  50. Rotate(x,c[y][0]==x);
  51. y=fa[x];
  52. }
  53. Rotate(x,c[y][0]==x);
  54. }
  55. }
  56. Maintain(x);
  57. if(!goal){
  58. root=x;
  59. }
  60. }
  61. int Find(int key,int x=root)
  62. {
  63. while(c[x][val[x]<key]){
  64. if(val[x]==key){
  65. return x;
  66. }
  67. x=c[x][val[x]<key];
  68. }
  69. return x;
  70. }
  71. void Insert(int key)
  72. {
  73. if(!root){
  74. NewNode(root,0,key);
  75. return;
  76. }
  77. int x=Find(key);
  78. if(val[x]==key){
  79. ++cnt[x];
  80. Splay(x,0);
  81. return;
  82. }
  83. NewNode(c[x][val[x]<key],x,key);
  84. Splay(c[x][val[x]<key],0);
  85. }
  86. int Findmax(int x=root)
  87. {
  88. while(c[x][1]){
  89. x=c[x][1];
  90. }
  91. return x;
  92. }
  93. int Findmin(int x=root)
  94. {
  95. while(c[x][0]){
  96. x=c[x][0];
  97. }
  98. return x;
  99. }
  100. void Delete(int key)
  101. {
  102. int x=Find(key);
  103. Splay(x,0);
  104. if(val[x]==key){
  105. --cnt[x];
  106. if(!cnt[x]){
  107. if(!c[x][0]&&!c[x][1]){
  108. root=0;
  109. }
  110. else if(!c[x][0]||!c[x][1]){
  111. fa[c[x][c[x][0]==0]]=0;
  112. root=c[x][c[x][0]==0];
  113. }
  114. else{
  115. int y=Findmax(c[x][0]);
  116. Splay(y,root);
  117. c[y][1]=c[root][1];
  118. fa[c[root][1]]=y;
  119. root=y;
  120. fa[root]=0;
  121. Maintain(root);
  122. }
  123. }
  124. else{
  125. Maintain(x);
  126. }
  127. }
  128. }
  129. int Rank(int key)
  130. {
  131. int x=Find(key);
  132. Splay(x,0);
  133. return siz[c[x][0]]+1;
  134. }
  135. int Kth(int K,int x=root)
  136. {
  137. while(1){
  138. int Siz0=siz[c[x][0]];
  139. if(K<=Siz0){
  140. x=c[x][0];
  141. }
  142. else if(K<=Siz0+cnt[x]){
  143. return val[x];
  144. }
  145. else{
  146. K-=(Siz0+cnt[x]);
  147. x=c[x][1];
  148. }
  149. }
  150. }
  151. int GetPre(int key)
  152. {
  153. int x=Find(key);
  154. if(val[x]==key){
  155. Splay(x,0);
  156. return val[Findmax(c[x][0])];
  157. }
  158. while(val[x]>key&&fa[x]){
  159. x=fa[x];
  160. }
  161. return val[x];
  162. }
  163. int GetNex(int key)
  164. {
  165. int x=Find(key);
  166. if(val[x]==key){
  167. Splay(x,0);
  168. return val[Findmin(c[x][1])];
  169. }
  170. while(val[x]<key&&fa[x]){
  171. x=fa[x];
  172. }
  173. return val[x];
  174. }
  175. int main(){
  176. int op, x;
  177. scanf("%d",&n);
  178. for(int i=1;i<=n;i++){
  179. scanf("%d%d",&op,&x);
  180. if(op==1){
  181. Insert(x);
  182. }
  183. else if(op==2){
  184. Delete(x);
  185. }
  186. else if(op==3){
  187. printf("%d\n",Rank(x));
  188. }
  189. else if(op==4){
  190. printf("%d\n",Kth(x));
  191. }
  192. else if(op==5){
  193. printf("%d\n",GetPre(x));
  194. }
  195. else{
  196. printf("%d\n",GetNex(x));
  197. }
  198. }
  199. return 0;
  200. }

【Splay】bzoj3224 Tyvj 1728 普通平衡树的更多相关文章

  1. bzoj3224: Tyvj 1728 普通平衡树(平衡树)

    bzoj3224: Tyvj 1728 普通平衡树(平衡树) 总结 a. cout<<(x=3)<<endl;这句话输出的值是3,那么对应的,在splay操作中,当父亲不为0的 ...

  2. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  3. bzoj3224: Tyvj 1728 普通平衡树(splay)

    3224: Tyvj 1728 普通平衡树 题目:传送门 题解: 啦啦啦啦又来敲个模版水经验啦~ 代码: #include<cstdio> #include<cstring> ...

  4. bzoj3224 Tyvj 1728 普通平衡树(名次树+处理相同)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5354  Solved: 2196[Submit][Sta ...

  5. 绝对是全网最好的Splay 入门详解——洛谷P3369&BZOJ3224: Tyvj 1728 普通平衡树 包教包会

    平衡树是什么东西想必我就不用说太多了吧. 百度百科: 一个月之前的某天晚上,yuli巨佬为我们初步讲解了Splay,当时接触到了平衡树里的旋转等各种骚操作,感觉非常厉害.而第二天我调Splay的模板竟 ...

  6. bzoj3224: Tyvj 1728 普通平衡树(打个splay暖暖手)

    (其实今天好热啊? 题目大意:插入,删除,k小,前驱后继,数的排名. splay和treap裸题...过几天补个treap的 splay: #include<iostream> #incl ...

  7. [bzoj3224]Tyvj 1728 普通平衡树——splay模板

    题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...

  8. 【权值分块】bzoj3224 Tyvj 1728 普通平衡树

    权值分块和权值线段树的思想一致,离散化之后可以代替平衡树的部分功能. 部分操作的时间复杂度: 插入 删除 全局排名 全局K大 前驱 后继 全局最值 按值域删除元素 O(1) O(1) O(sqrt(n ...

  9. 【权值线段树】bzoj3224 Tyvj 1728 普通平衡树

    一个板子. #include<cstdio> #include<algorithm> using namespace std; #define N 100001 struct ...

随机推荐

  1. C++学习之路(三):volatile关键字

    volatile是c++中的一个关键字.用volatile修饰的变量,具有三个性质:易变性 (一)易变性: 由于编译器对代码执行的优化,两条赋值语句,下一条语句可能会直接从上一条语句使用的寄存器中取得 ...

  2. map,set的底层实现:红黑树[多图,手机慎入]

    最近天下有一种颇不太平的感觉,各地的乱刀砍人,到处是贪官服法.京东准备上市了,阿里最近也提交申请了,猎豹也逆袭了,据说猎豹移动在国际市场上表现甚是抢眼.只有屌丝还在写着代码.花开花又谢,花谢花又开,为 ...

  3. fbx sdk

    autodesk fbx review autodesk fbx review http://www.greenxf.com/soft/169025.html autodesk fbx review( ...

  4. selenium.webdriver.common.keys 模块中常用的变量

    表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...

  5. 【bzoj4562】HAOI2016食物链

    记忆化搜索水过去了…… QwQ #include<bits/stdc++.h> #define N 400010 typedef long long ll; using namespace ...

  6. 升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode” Xcode 插件安装

    升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode”  Xcode 插件安装 A:  升级10.11后Xcode 左上角模拟器选择菜单不在了   ...

  7. apache加入chkconfig

    #First Step: cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd #Second Step: vim /etc/init.d/htt ...

  8. python安装基础

    . python安装 //先查看是否存在python的包,如果没有,那可以用yum或去python的官网安装 [root@localhost ~]# rpm -qa|grep python pytho ...

  9. yum安装的Apache的各种配置文件的位置

    //配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...

  10. Context-Aware Network Embedding for Relation Modeling

    Context-Aware Network Embedding for Relation Modeling 论文:http://www.aclweb.org/anthology/P17-1158 创新 ...