树状数组套权值线段树

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <string>
  7.  
  8. #define E exit(0)
  9.  
  10. using namespace std;
  11.  
  12. #define LL long long
  13.  
  14. #define gc getchar()
  15. inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
  16. while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
  17. inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
  18. while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
  19. #undef gc
  20.  
  21. const int N = 5e4 + ;
  22. const int Out = ;
  23.  
  24. struct Node {int opt, l, r, k;} Ask[N];
  25. int n, m;
  26. int Num[N << ], totNum, A[N], Length;
  27.  
  28. int Root[N];
  29. int W[N * ], Lson[N * ], Rson[N * ];
  30.  
  31. int Add_root[N], Cut_root[N];
  32. int Hjt_;
  33.  
  34. namespace Seg {
  35.  
  36. void Insert(int &rt, int l, int r, int x) {
  37. if(!rt) rt = ++ Hjt_;
  38. W[rt] ++;
  39. if(l == r) return ;
  40. int mid = (l + r) >> ;
  41. if(x <= mid) Insert(Lson[rt], l, mid, x);
  42. else Insert(Rson[rt], mid + , r, x);
  43. }
  44.  
  45. int Getrank(int l, int r, int k, int opt) {
  46. if(l == r) {
  47. if(opt == ) return ;
  48. int s = ;
  49. for(int i = ; i <= Add_root[]; i ++) s += W[Add_root[i]];
  50. for(int i = ; i <= Cut_root[]; i ++) s -= W[Cut_root[i]];
  51. return s;
  52. }
  53. int mid = (l + r) >> ;
  54. if(k <= mid) {
  55. for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
  56. for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
  57. return Getrank(l, mid, k, opt);
  58. } else {
  59. int sum = ;
  60. for(int i = ; i <= Add_root[]; i ++) {
  61. sum += W[Lson[Add_root[i]]]; Add_root[i] = Rson[Add_root[i]];
  62. }
  63. for(int i = ; i <= Cut_root[]; i ++) {
  64. sum -= W[Lson[Cut_root[i]]]; Cut_root[i] = Rson[Cut_root[i]];
  65. }
  66. int ret = sum + Getrank(mid + , r, k, opt);
  67. return ret;
  68. }
  69. }
  70.  
  71. int Getnum(int l, int r, int k) {
  72. if(l == r) return l;
  73. int mid = (l + r) >> ;
  74. int sum = ;
  75. for(int i = ; i <= Add_root[]; i ++) sum += W[Lson[Add_root[i]]];
  76. for(int i = ; i <= Cut_root[]; i ++) sum -= W[Lson[Cut_root[i]]];
  77. if(sum >= k) {
  78. for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
  79. for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
  80. Getnum(l, mid, k);
  81. } else {
  82. for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Rson[Add_root[i]];
  83. for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Rson[Cut_root[i]];
  84. Getnum(mid + , r, k - sum);
  85. }
  86. }
  87.  
  88. void Poi_G(int l, int r, int x, int val) {
  89. for(int i = ; i <= Cut_root[]; i ++) W[Cut_root[i]] += val;
  90. if(l == r) return ;
  91. int mid = (l + r) >> ;
  92. if(x <= mid) {
  93. for(int i = ; i <= Cut_root[]; i ++) {
  94. if(Lson[Cut_root[i]] == ) Lson[Cut_root[i]] = ++ Hjt_;
  95. Cut_root[i] = Lson[Cut_root[i]];
  96. }
  97. Poi_G(l, mid, x, val);
  98. } else {
  99. for(int i = ; i <= Cut_root[]; i ++) {
  100. if(Rson[Cut_root[i]] == ) Rson[Cut_root[i]] = ++ Hjt_;
  101. Cut_root[i] = Rson[Cut_root[i]];
  102. }
  103. Poi_G(mid + , r, x, val);
  104. }
  105. }
  106.  
  107. int Asknum(int l, int r, int k) {
  108. if(l == r) {
  109. int sum = ;
  110. for(int i = ; i <= Add_root[]; i ++) sum += W[Add_root[i]];
  111. for(int i = ; i <= Cut_root[]; i ++) sum -= W[Cut_root[i]];
  112. k -= sum;
  113. if(k > ) return -;
  114. return l;
  115. }
  116. int sum = ;
  117. for(int i = ; i <= Add_root[]; i ++) sum += W[Lson[Add_root[i]]];
  118. for(int i = ; i <= Cut_root[]; i ++) sum -= W[Lson[Cut_root[i]]];
  119. int mid = (l + r) >> ;
  120. if(k <= sum) {
  121. for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Lson[Add_root[i]];
  122. for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Lson[Cut_root[i]];
  123. Asknum(l, mid, k);
  124. } else {
  125. for(int i = ; i <= Add_root[]; i ++) Add_root[i] = Rson[Add_root[i]];
  126. for(int i = ; i <= Cut_root[]; i ++) Cut_root[i] = Rson[Cut_root[i]];
  127. Asknum(mid + , r, k - sum);
  128. }
  129. }
  130. }
  131.  
  132. namespace Bit {
  133.  
  134. inline int Lowbit(int x) {return (x & (-x));}
  135.  
  136. void Add(int rt, int x, int val) {
  137. for(int i = rt; i <= n; i += Lowbit(i)) {
  138. Seg:: Insert(Root[i], , Length, x);
  139. }
  140. }
  141.  
  142. int Getrank(int l, int r, int k) {
  143. Add_root[] = Cut_root[] = ;
  144. for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
  145. for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  146. k = lower_bound(Num + , Num + Length + , k) - Num;
  147. return Seg:: Getrank(, Length, k, );
  148. }
  149.  
  150. int Getnum(int l, int r, int k) {
  151. Add_root[] = Cut_root[] = ;
  152. for(int i = r; i >= ; i -= Lowbit(i)) {
  153. Add_root[++ Add_root[]] = Root[i];
  154. }
  155. for(int i = l - ; i >= ; i -= Lowbit(i)) {
  156. Cut_root[++ Cut_root[]] = Root[i];
  157. }
  158. return Seg:: Getnum(, Length, k);
  159. }
  160.  
  161. void Poi_G(int x, int k) {
  162. Cut_root[] = ;
  163. for(int i = x; i <= n; i += Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  164. int a = lower_bound(Num + , Num + Length + , A[x]) - Num;
  165. A[x] = k;
  166. Seg:: Poi_G(, Length, a, -);
  167. Cut_root[] = ;
  168. for(int i = x; i <= n; i += Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  169. a = lower_bound(Num + , Num + Length + , k) - Num;
  170. Seg:: Poi_G(, Length, a, );
  171. }
  172.  
  173. int Asknum(int l, int r, int k, int opt) {
  174. k = lower_bound(Num + , Num + Length + , k) - Num;
  175. int kk = k;
  176. Add_root[] = Cut_root[] = ;
  177. for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
  178. for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  179. k = Seg:: Getrank(, Length, k, );
  180. Add_root[] = Cut_root[] = ;
  181. for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
  182. for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  183. int k2 = Seg:: Getrank(, Length, kk, );
  184. Add_root[] = Cut_root[] = ;
  185. for(int i = r; i >= ; i -= Lowbit(i)) Add_root[++ Add_root[]] = Root[i];
  186. for(int i = l - ; i >= ; i -= Lowbit(i)) Cut_root[++ Cut_root[]] = Root[i];
  187. if(opt == ) {
  188. if(k == ) return -;
  189. return Seg:: Asknum(, Length, k - );
  190. }
  191. else return Seg:: Asknum(, Length, k2 + );
  192. }
  193. }
  194.  
  195. int main() {
  196. n = read(), m = read();
  197. for(int i = ; i <= n; i ++) Num[i] = read(), A[i] = Num[i];
  198. totNum = n;
  199. for(int i = ; i <= m; i ++) {
  200. Ask[i].opt = read();
  201. if(Ask[i].opt != ) {Ask[i].l = read(), Ask[i].r = read(), Ask[i].k = read();}
  202. else Ask[i].l = read(), Ask[i].k = read(), Num[++ totNum] = Ask[i].k;
  203. if(Ask[i].opt == || Ask[i].opt == ) {
  204. Num[++ totNum] = Ask[i].k;
  205. }
  206. }
  207. sort(Num + , Num + totNum + );
  208. Length = unique(Num + , Num + totNum + ) - Num - ;
  209. for(int i = ; i <= n; i ++) {
  210. int a = lower_bound(Num + , Num + Length + , A[i]) - Num;
  211. Bit:: Add(i, a, );
  212. }
  213.  
  214. #define opt Ask[i].opt
  215. #define l Ask[i].l
  216. #define r Ask[i].r
  217. #define k Ask[i].k
  218.  
  219. for(int i = ; i <= m; i ++) {
  220. if(opt == ) {
  221. cout << Bit:: Getrank(l, r, k) << "\n";
  222. } else if(opt == ) {
  223. int ans = Bit:: Getnum(l, r, k);
  224. cout << Num[ans] << "\n";
  225. } else if(opt == ) {
  226. Bit:: Poi_G(l, k);
  227. } else if(opt == ) {
  228. int ans = Bit:: Asknum(l, r, k, );
  229. if(ans == -) cout << "-2147483647" << "\n";
  230. else cout << Num[ans] << "\n";
  231. } else {
  232. int ans = Bit:: Asknum(l, r, k, );
  233. if(ans == -) cout << "" << "\n";
  234. else cout << Num[ans] << "\n";
  235. }
  236. }
  237. return ;
  238. }

luogu 3380的更多相关文章

  1. bzoj 3196 && luogu 3380 JoyOI 1730 二逼平衡树 (线段树套Treap)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3196 题面; 3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Se ...

  2. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  3. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  4. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  5. [luogu P2647] 最大收益(贪心+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...

  6. Luogu 考前模拟Round. 1

    A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...

  7. luogu P2580 于是他错误的点名开始了

    luogu  P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...

  8. CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)

    CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...

  9. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

随机推荐

  1. windows 下搭建go开发环境

    下载golang安装包: 步骤一: 先打开环境变量配置的界面 步骤二;配置我们的环境变量 对上图的一个说明: 1).path这个变量不需要再创建,因为系统本就有,在后面添加即可. 2).增加Go的bi ...

  2. Vector、ArrayList异同和HTTP请求异同的概括和区别

    今天我所记录的是两个异同的概括: HTTP: 同步请求:提交请求->等待服务器处理->处理完毕返回给客户端  这个期间客户端浏览器只能处于等待状态,得到回应才可以执行下一步操作. 异步请求 ...

  3. 在微服务架构中service mesh是什么?

    在微服务架构中service mesh是什么 什么是 service mesh ? 微服务架构将软件功能隔离为多个独立的服务,这些服务可独立部署,高度可维护和可测试,并围绕特定业务功能进行组织. 这些 ...

  4. github的pull request是指什么意思?有什么用处(转)

    https://www.cnblogs.com/-walker/p/6093277.html

  5. java 线程实现、线程暂停和终止 、线程联合join、线程基本信息获取和设置、线程优先级

    转载地址:速学堂 https://www.sxt.cn/Java_jQuery_in_action/eleven-inheritthread.html 1. 通过继承Thread类实现多线程 继承Th ...

  6. django.http.request中HttpRequest对象的一些属性与方法

    HttpRequest对象的属性 属性 描述 path 表示提交请求页面完整地址的字符串,不包括域名,如 "/music/bands/the_beatles/". method 表 ...

  7. jQuery的显示和隐藏

    在 jQuery 中可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素,以及使用 toggle() 方法能够切换 hide() 和 show() 方法. 隐藏例子: <! ...

  8. [转]github 上传project代码

    原文地址:https://www.cnblogs.com/f1194361820/p/4741558.html 1)将远程仓库纳入管理 其实就是添加远程仓库,在你已有的本地仓库目录下执行如下命令: $ ...

  9. 打印html页面

    // 打印类属性.方法定义 const Print = function (dom, options) { if (!(this instanceof Print)) return new Print ...

  10. MySQL常见问题集锦及注意事项

    一.表设计上的坑 1.字段设计 1.1 字段类型设计 尽量使用整型表示字符串: `INET_ATON(str)`,address to number `INET_NTOA(number)`,numbe ...