[关闭]
@AkamemakA 2022-10-18T13:19:57.000000Z 字数 2422 阅读 178

知识点 模板 数据结构

Splay (平衡树) 函数速查


  1. #include<iostream>
  2. using namespace std;
  3. #define INf INF
  4. const int N=1e5,INF=0x3f3f3f3f;
  5. struct node{
  6. int fa;
  7. int son[2];
  8. int val,num,si;
  9. #define root t[0].son[1]
  10. }t[N];
  11. int n,cnt;
  12. inline int ident(int x){
  13. if(t[t[x].fa].son[0]==x) return 0;
  14. else return 1;
  15. }
  16. inline void update(int x){
  17. t[x].si=t[t[x].son[0]].si+t[t[x].son[1]].si+t[x].num;
  18. }
  19. inline void connect(int x,int fa,int who){
  20. t[fa].son[who]=x;
  21. t[x].fa=fa;
  22. }
  23. inline void rotate(int x){
  24. int y=t[x].fa,r=t[y].fa;
  25. int yson=ident(x),rson=ident(y);
  26. connect(t[x].son[yson^1],y,yson);
  27. connect(y,x,yson^1);
  28. connect(x,r,rson);
  29. update(y);update(x);
  30. }
  31. inline void splay(int x,int to){
  32. to=t[to].fa;
  33. while(t[x].fa!=to){
  34. int y=t[x].fa;
  35. if(t[y].fa==to)rotate(x);
  36. else if(ident(x)==ident(y)) rotate(y),rotate(x);
  37. else rotate(x),rotate(x);
  38. }
  39. }
  40. inline int newnode(int v,int f){
  41. t[++cnt].fa=f;
  42. t[cnt].num=t[cnt].si=1;
  43. t[cnt].val=v;
  44. return cnt;
  45. }
  46. inline void insert(int x){
  47. int now=root;
  48. if(root==0){
  49. newnode(x,0);
  50. root=cnt;
  51. }else{
  52. while(1){
  53. t[now].si++;
  54. if(t[now].val==x){
  55. t[now].num++;
  56. splay(now,root);
  57. return ;
  58. }
  59. int nxt=x<t[now].val?0:1;
  60. if(!t[now].son[nxt]){
  61. int p=newnode(x,now);
  62. t[now].son[nxt]=p;
  63. splay(p,root);
  64. return ;
  65. }
  66. now=t[now].son[nxt];
  67. }
  68. }
  69. }
  70. inline int find(int x){
  71. int now=root;
  72. while(1){
  73. if(!now) return 0;
  74. if(t[now].val==x){
  75. splay(now,root);
  76. return now;
  77. }
  78. int nxt=x<t[now].val?0:1;
  79. now=t[now].son[nxt];
  80. }
  81. }
  82. inline void delet(int x){
  83. int pos=find(x);
  84. if(!pos) return ;
  85. if(t[pos].num>1){
  86. t[pos].num--;t[pos].si--;
  87. return ;
  88. }else {
  89. if(!t[pos].son[0]&&!t[pos].son[1]){
  90. root=0;
  91. return ;
  92. }else if(!t[pos].son[0]){
  93. root=t[pos].son[1];
  94. t[root].fa=0;
  95. return ;
  96. }else{
  97. int l=t[pos].son[0];
  98. while(t[l].son[1]) l=t[l].son[1];
  99. splay(l,t[pos].son[0]);
  100. connect(t[pos].son[1],l,1);
  101. connect(l,0,1);
  102. update(l);
  103. }
  104. }
  105. }
  106. inline int Rank(int x){
  107. return t[t[find(x)].son[0]].si+1;
  108. }
  109. inline int arank(int x){
  110. int now=root;
  111. while(1){
  112. int tem_num=t[now].si-t[t[now].son[1]].si;
  113. if(t[t[now].son[0]].si<x&&x<=tem_num){
  114. splay(now,root);
  115. return t[now].val;
  116. }
  117. if(x<tem_num) now=t[now].son[0];
  118. else now=t[now].son[1],x-=tem_num;
  119. }
  120. }
  121. inline int lower(int x){
  122. int now=root,ans=-INF;
  123. while(now){
  124. if(t[now].val<x) ans=max(ans,t[now].val);
  125. int nxt=x<=t[now].val?0:1;
  126. now=t[now].son[nxt];
  127. }
  128. return ans;
  129. }
  130. inline int upper(int x){
  131. int now=root,ans=INF;
  132. while(now){
  133. if(t[now].val>x) ans=min(ans,t[now].val);
  134. int nxt=x<t[now].val?0:1;
  135. now=t[now].son[nxt];
  136. }
  137. return ans;
  138. }
  139. int main()
  140. {
  141. cin>>n;
  142. while(n--){
  143. int ops,x;
  144. scanf("%d%d",&ops,&x);
  145. if(ops==1) insert(x);
  146. else if(ops==2) delet(x);
  147. else if(ops==3) printf("%d\n",Rank(x));
  148. else if(ops==4) printf("%d\n",arank(x));
  149. else if(ops==5) printf("%d\n",lower(x));
  150. else printf("%d\n",upper(x));
  151. }
  152. }//我不理解!!!
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注