查询区间第K大,而且没有修改。

使用划分树是可以做的。

作为主席树的入门题,感觉太神奇了,Orz

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2013-9-4 20:13:20
  4. File Name :POJ2104.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20.  
  21. const int MAXN = ;
  22. const int M = MAXN * ;
  23. int n,q,m,tot;
  24. int a[MAXN], t[MAXN];
  25. int T[M], lson[M], rson[M], c[M];
  26.  
  27. void Init_hash()
  28. {
  29. for(int i = ; i <= n;i++)
  30. t[i] = a[i];
  31. sort(t+,t++n);
  32. m = unique(t+,t++n)-t-;
  33. }
  34. int build(int l,int r)
  35. {
  36. int root = tot++;
  37. c[root] = ;
  38. if(l != r)
  39. {
  40. int mid = (l+r)>>;
  41. lson[root] = build(l,mid);
  42. rson[root] = build(mid+,r);
  43. }
  44. return root;
  45. }
  46. int hash(int x)
  47. {
  48. return lower_bound(t+,t++m,x) - t;
  49. }
  50. int update(int root,int pos,int val)
  51. {
  52. int newroot = tot++, tmp = newroot;
  53. c[newroot] = c[root] + val;
  54. int l = , r = m;
  55. while(l < r)
  56. {
  57. int mid = (l+r)>>;
  58. if(pos <= mid)
  59. {
  60. lson[newroot] = tot++; rson[newroot] = rson[root];
  61. newroot = lson[newroot]; root = lson[root];
  62. r = mid;
  63. }
  64. else
  65. {
  66. rson[newroot] = tot++; lson[newroot] = lson[root];
  67. newroot = rson[newroot]; root = rson[root];
  68. l = mid+;
  69. }
  70. c[newroot] = c[root] + val;
  71. }
  72. return tmp;
  73. }
  74. int query(int left_root,int right_root,int k)
  75. {
  76. int l = , r = m;
  77. while( l < r)
  78. {
  79. int mid = (l+r)>>;
  80. if(c[lson[left_root]]-c[lson[right_root]] >= k )
  81. {
  82. r = mid;
  83. left_root = lson[left_root];
  84. right_root = lson[right_root];
  85. }
  86. else
  87. {
  88. l = mid + ;
  89. k -= c[lson[left_root]] - c[lson[right_root]];
  90. left_root = rson[left_root];
  91. right_root = rson[right_root];
  92. }
  93. }
  94. return l;
  95. }
  96. int main()
  97. {
  98. //freopen("in.txt","r",stdin);
  99. //freopen("out.txt","w",stdout);
  100. while(scanf("%d%d",&n,&q) == )
  101. {
  102. tot = ;
  103. for(int i = ;i <= n;i++)
  104. scanf("%d",&a[i]);
  105. Init_hash();
  106. T[n+] = build(,m);
  107. for(int i = n;i ;i--)
  108. {
  109. int pos = hash(a[i]);
  110. T[i] = update(T[i+],pos,);
  111. }
  112. while(q--)
  113. {
  114. int l,r,k;
  115. scanf("%d%d%d",&l,&r,&k);
  116. printf("%d\n",t[query(T[l],T[r+],k)]);
  117. }
  118. }
  119. return ;
  120. }

POJ 2104 && POJ 2761 (静态区间第k大,主席树)的更多相关文章

  1. POJ 2104 K-th Number (区间第k大)

    题意:给定一个序列A,接下来又m个询问,每个询问输出A[L,R]中的第K大.(保证第k大存在) 思路: 我想拿来练习“可持久化线段树”的,搜到这个比较巧的算法也可以解决这个问题,叫“归并树?.大概的思 ...

  2. HDU2665 求区间第K大 主席树

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665 代码: //#include<bits/stdc++.h> #include< ...

  3. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

  4. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  5. HDU3473--Minimum Sum(静态区间第k大)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 43315   Accepted: 14296 Ca ...

  7. 主席树(静态区间第k大)

    前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...

  8. 可持久化线段树(主席树)——静态区间第k大

    主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. 字体格式类型(.eot/.otf/.woff/.svg)

    @font-face语句是css中的一个功能模块,用于实现网页字体多样性的模块(设计者可随意指定字体,不需要考虑浏览者电脑上是否安装). @font-face文件 而由于网页中使用的字体类型,也是各浏 ...

  2. python进阶学习之高阶函数

    高阶函数就是把函数当做参数传递的一种函数, 例如: 执行结果: 1.map()函数 map()接收一个函数 f 和一个list, 并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 l ...

  3. 【读书笔记】Android的Ashmem机制学习

    Ashmem是安卓在linux基础上添加的驱动模块,就是说安卓有linux没有的功能. Ashmem模块在内核层面上实现,在运行时库和应用程序框架层提供了访问接口.在运行时库层提供的是C++接口,在应 ...

  4. Struts 2 - Hello World Example

    As you learnt from the Struts 2 architecture, when you click on a hyperlink or submit an HTML form i ...

  5. hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

  6. day6面向对象

    面向对象介绍(http://www.cnblogs.com/alex3714/articles/5188179.htm)     世界万物,皆可分类     世界万物,皆为对象     只要是对象,就 ...

  7. python日期格式转换小记

    utc格林时间==>东八区北京时间 原始日期格式: utctime = ‘2016-07-26 10:08:29’ localtime = (datetime.datetime.fromtime ...

  8. 易普优APS混流排序算法助力汽车整车厂的均衡生产

    一.汽车整车厂生产排序的难点 “ 冲压-焊接-涂装-总装”是汽车整车生产的四大工艺类型,它们存在上下游关联关系,每个车间都有自己的优化排序目标,汽车混流生产模式使得生产过程更加复杂,从而生产管控的难度 ...

  9. python日常总结

    1. post请求中是否可以在url中携带请求体信息? 可以.Get请求时,请求体放在URL中; POST请求,请求体既可以是Form表单中的数据 也可以在请求的URL地址中放请求体信息. 如: &l ...

  10. nginx、php-fpm、swoole HTTP/TCP压测对比

    本次测试是在win7下docker环境中进行压测,共创建一个nginx容器.一个php-fpm容器和一个swoole容器,客户端请求nginx服务器,nginx接收用户访问请求并转发给php-fpm, ...