1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. using namespace std;
  6. int n, m, a[50005], opt, uu, vv, ww, ans;
  7. const int oo=2147483647;
  8. namespace treap{
  9. struct Node{
  10. int l, r, siz, val, hav, rnd;
  11. }nd[5000005];
  12. int tot;
  13. void upd(int k){
  14. nd[k].siz = nd[nd[k].l].siz + nd[nd[k].r].siz + nd[k].hav;
  15. }
  16. void lRotate(int &k){
  17. int t=nd[k].r; nd[k].r = nd[t].l; nd[t].l = k;
  18. nd[t].siz = nd[k].siz; upd(k); k = t;
  19. }
  20. void rRotate(int &k){
  21. int t=nd[k].l; nd[k].l = nd[t].r; nd[t].r = k;
  22. nd[t].siz = nd[k].siz; upd(k); k = t;
  23. }
  24. void insert(int &k, int x){
  25. if(!k){
  26. k = ++tot; nd[k].siz = nd[k].hav = 1;
  27. nd[k].val = x; nd[k].rnd = rand();
  28. return ;
  29. }
  30. nd[k].siz++;
  31. if(nd[k].val==x) nd[k].hav++;
  32. else if(nd[k].val>x){
  33. insert(nd[k].l, x);
  34. if(nd[nd[k].l].rnd<nd[k].rnd) rRotate(k);
  35. }
  36. else{
  37. insert(nd[k].r, x);
  38. if(nd[nd[k].r].rnd<nd[k].rnd) lRotate(k);
  39. }
  40. }
  41. void shanchu(int &k, int x){
  42. if(!k) return ;
  43. if(nd[k].val==x){
  44. if(nd[k].hav>1){
  45. nd[k].hav--;
  46. nd[k].siz--;
  47. return ;
  48. }
  49. if(nd[k].l*nd[k].r==0) k = nd[k].l + nd[k].r;
  50. else if(nd[nd[k].l].rnd<nd[nd[k].r].rnd) rRotate(k), shanchu(k, x);
  51. else lRotate(k), shanchu(k, x);
  52. }
  53. else if(nd[k].val>x)
  54. nd[k].siz--, shanchu(nd[k].l, x);
  55. else
  56. nd[k].siz--, shanchu(nd[k].r, x);
  57. }
  58. int queryRank(int k, int x){
  59. if(!k) return 0;
  60. if(nd[k].val>x) return queryRank(nd[k].l, x);
  61. else if(nd[k].val<x) return queryRank(nd[k].r, x)+nd[nd[k].l].siz+nd[k].hav;
  62. else return nd[nd[k].l].siz;
  63. }
  64. void queryPre(int k, int x){
  65. if(!k) return ;
  66. if(nd[k].val<x) ans = max(ans, nd[k].val), queryPre(nd[k].r, x);
  67. else queryPre(nd[k].l, x);
  68. }
  69. void queryNxt(int k, int x){
  70. if(!k) return ;
  71. if(nd[k].val>x) ans = min(ans, nd[k].val), queryNxt(nd[k].l, x);
  72. else queryNxt(nd[k].r, x);
  73. }
  74. }
  75. namespace sgt{
  76. int rot[200005];
  77. void insert(int o, int l, int r, int x, int k){
  78. treap::insert(rot[o], k);
  79. if(l==r) ;
  80. else{
  81. int mid=(l+r)>>1;
  82. int lson=o<<1;
  83. int rson=lson|1;
  84. if(x<=mid) insert(lson, l, mid, x, k);
  85. if(mid<x) insert(rson, mid+1, r, x, k);
  86. }
  87. }
  88. void shanchu(int o, int l, int r, int x, int k){
  89. treap::shanchu(rot[o], k);
  90. if(l==r) ;
  91. else{
  92. int mid=(l+r)>>1;
  93. int lson=o<<1;
  94. int rson=lson|1;
  95. if(x<=mid) shanchu(lson, l, mid, x, k);
  96. if(mid<x) shanchu(rson, mid+1, r, x, k);
  97. }
  98. }
  99. int queryRank(int o, int l, int r, int x, int y, int k){
  100. if(l>=x && r<=y) return treap::queryRank(rot[o], k);
  101. else{
  102. int mid=(l+r)>>1;
  103. int lson=o<<1;
  104. int rson=lson|1;
  105. int re=0;
  106. if(x<=mid) re += queryRank(lson, l, mid, x, y, k);
  107. if(mid<y) re += queryRank(rson, mid+1, r, x, y, k);
  108. return re;
  109. }
  110. }
  111. void queryPre(int o, int l, int r, int x, int y, int k){
  112. if(l>=x && r<=y) treap::queryPre(rot[o], k);
  113. else{
  114. int mid=(l+r)>>1;
  115. int lson=o<<1;
  116. int rson=lson|1;
  117. if(x<=mid) queryPre(lson, l, mid, x, y, k);
  118. if(mid<y) queryPre(rson, mid+1, r, x, y, k);
  119. }
  120. }
  121. void queryNxt(int o, int l, int r, int x, int y, int k){
  122. if(l>=x && r<=y) treap::queryNxt(rot[o], k);
  123. else{
  124. int mid=(l+r)>>1;
  125. int lson=o<<1;
  126. int rson=lson|1;
  127. if(x<=mid) queryNxt(lson, l, mid, x, y, k);
  128. if(mid<y) queryNxt(rson, mid+1, r, x, y, k);
  129. }
  130. }
  131. }
  132. int queryNum(int uu, int vv, int ww){
  133. int l=0, r=1e8, mid, re;
  134. while(l<=r){
  135. mid = (l + r) >> 1;
  136. if(sgt::queryRank(1, 1, n, uu, vv, mid)+1<=ww) re = mid, l = mid + 1;
  137. else r = mid - 1;
  138. }
  139. return re;
  140. }
  141. int main(){
  142. srand(time(NULL));
  143. cin>>n>>m;
  144. for(int i=1; i<=n; i++){
  145. scanf("%d", &a[i]);
  146. sgt::insert(1, 1, n, i, a[i]);
  147. }
  148. while(m--){
  149. scanf("%d", &opt);
  150. if(opt==1){
  151. scanf("%d %d %d", &uu, &vv, &ww);
  152. printf("%d\n", sgt::queryRank(1, 1, n, uu, vv, ww)+1);
  153. }
  154. if(opt==2){
  155. scanf("%d %d %d", &uu, &vv, &ww);
  156. printf("%d\n", queryNum(uu, vv, ww));
  157. }
  158. if(opt==3){
  159. scanf("%d %d", &uu, &vv);
  160. sgt::shanchu(1, 1, n, uu, a[uu]);
  161. a[uu] = vv;
  162. sgt::insert(1, 1, n, uu, a[uu]);
  163. }
  164. if(opt==4){
  165. scanf("%d %d %d", &uu, &vv, &ww);
  166. ans = -oo;
  167. sgt::queryPre(1, 1, n, uu, vv, ww);
  168. printf("%d\n", ans);
  169. }
  170. if(opt==5){
  171. scanf("%d %d %d", &uu, &vv, &ww);
  172. ans = oo;
  173. sgt::queryNxt(1, 1, n, uu, vv, ww);
  174. printf("%d\n", ans);
  175. }
  176. }
  177. return 0;
  178. }

luogu3380 【模板】二逼平衡树(树套树)的更多相关文章

  1. bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1807  Solved: 772[Submit][Stat ...

  2. bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description ...

  3. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...

  4. BZOJ3196 二逼平衡树 【线段树套平衡树】

    题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...

  5. BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay

    传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...

  6. luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)

    带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...

  7. [BZOJ3196] [Tyvj1730] 二逼平衡树(线段树 套 Splay)

    传送门 至少BZOJ过了,其他的直接弃. 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的 ...

  8. bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】

    四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...

  9. 「luogu3380」【模板】二逼平衡树(树套树)

    「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...

  10. [luogu3380][bzoj3196]【模板】二逼平衡树【树套树】

    题目地址 [洛谷传送门] 题目大意 区间查询k的排名,查找k排名的数,单点修改,区间前驱,区间后继. 感想 真的第一次写树套树,整个人都不对了.重构代码2次,发现样例都过不了,splay直接爆炸,可能 ...

随机推荐

  1. 522 Longest Uncommon Subsequence II 最长特殊序列 II

    详见:https://leetcode.com/problems/longest-uncommon-subsequence-ii/description/ C++: 方法一: class Soluti ...

  2. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

  3. Mysql中的索引问题

    索引的用途 提高查询的效率,相当于在字典中建立的字母表或者偏旁部首表,这样查询当然比一行一行查询要快的多 每个存储引擎可以建立索引的长度是不一样的,但每个表至少支持16个索引,总的索引长度至少为256 ...

  4. C/S WinForm自动升级

    这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary>    /// 版本检测    /// </summary>    public class ...

  5. 一些关于Spring的随笔

    Spring的IOC.AOP IOC(Inversion of Control): spring容器控制了所有的bean,不用spring以前,一个bean要依赖另一个bean就在这个bean里初始化 ...

  6. spring boot使用jpa查询mysql数据库的视图时不报错,但查询结果数据总是重复第一条

    问题描述: 在数据库里查询到的结果是正常显示的 在程序中返回的结果: 解决方法: 添加行号作为主键: 解决! 我明明是前端啊前端,为啥在搞后台....,总感觉我要在向全栈进发,希望自己有朝一日真的能成 ...

  7. 【js数据结构】图的深度优先搜索与广度优先搜索

    图类的构建 function Graph(v) {this.vertices = v;this.edges = 0;this.adj = []; for (var i = 0; i < this ...

  8. EditText输入手机号自动带空格

    xml: <EditText android:id="@+id/edit_main" android:layout_width="match_parent" ...

  9. 固定table表头

    <style> #box{ height:214px; width:500px; overflow-y:auto;/** 必须,否则当表格数据过多时,不会产生滚动条,而是自动延长该div的 ...

  10. 我所理解的MVVM

    将UI中的数据适配.交互处理: controller中与UI密切相关的功能: 剥离出来,形成单独的模块: 以增加UI和Controller的灵活性.