题目链接:

http://poj.org/problem?id=2104

K-th Number

Time Limit: 20000MS
Memory Limit: 65536K
#### 问题描述
> You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
> That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
> For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
#### 输入
> The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
> The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
####样例输入
> 7 3
> 1 5 2 6 3 7 4
> 2 5 3
> 4 4 1
> 1 7 3
####样例输出
> 5
> 6
> 3
## 题意
> 给你n个数m个询问,每个询问给你l,r,k,求(l,r)区间里第k大的数,保证每个数只出现一次。

题解

主席树裸板。

一个简洁形象的教程:[port]

代码:[port]

代码

本土化:

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. #include<sstream>
  17. using namespace std;
  18. #define X first
  19. #define Y second
  20. #define mkp make_pair
  21. #define lson(i) (tre[(i)].ls)
  22. #define rson(i) (tre[(i)].rs)
  23. #define sumv(i) tre[(i)].sum
  24. #define mid (l+(r-l)/2)
  25. #define sz() size()
  26. #define pb(v) push_back(v)
  27. #define all(o) (o).begin(),(o).end()
  28. #define clr(a,v) memset(a,v,sizeof(a))
  29. #define bug(a) cout<<#a<<" = "<<a<<endl
  30. #define rep(i,a,b) for(int i=a;i<(b);i++)
  31. #define scf scanf
  32. #define prf printf
  33. typedef long long LL;
  34. typedef vector<int> VI;
  35. typedef pair<int,int> PII;
  36. typedef vector<pair<int,int> > VPII;
  37. const int INF=0x3f3f3f3f;
  38. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  39. const double eps=1e-8;
  40. const double PI = acos(-1.0);
  41. //start----------------------------------------------------------------------
  42. const int maxn=1e5+10;
  43. ///nlogn空间复杂度
  44. struct Tre{
  45. int ls,rs,sum;
  46. Tre(){ls=rs=sum=0;}
  47. }tre[maxn*20];
  48. int n,m;
  49. int rt[maxn],tot;
  50. int _v;
  51. void update(int &o,int l,int r){
  52. tre[++tot]=tre[o],o=tot;
  53. if(l==r){
  54. sumv(o)++;
  55. }else{
  56. if(_v<=mid) update(lson(o),l,mid);
  57. else update(rson(o),mid+1,r);
  58. sumv(o)=sumv(lson(o))+sumv(rson(o));
  59. }
  60. }
  61. int _res;
  62. void query(int o1,int o2,int l,int r,int k){
  63. if(l==r){
  64. _res=l;
  65. }else{
  66. ///前缀和思想
  67. int cnt=sumv(lson(o2))-sumv(lson(o1));
  68. if(cnt>=k) query(lson(o1),lson(o2),l,mid,k);
  69. else query(rson(o1),rson(o2),mid+1,r,k-cnt);
  70. }
  71. }
  72. int idx[maxn],arr[maxn],ra[maxn];
  73. bool cmp(int x,int y){
  74. return arr[x]<arr[y];
  75. }
  76. ///0是个超级节点
  77. void init(){
  78. rt[0]=tot=0;
  79. }
  80. int main() {
  81. while(scf("%d%d",&n,&m)==2&&n){
  82. init();
  83. for(int i=1;i<=n;i++) scf("%d",&arr[i]);
  84. for(int i=1;i<=n;i++) idx[i]=i;
  85. ///离散化
  86. sort(idx+1,idx+n+1,cmp);
  87. for(int i=1;i<=n;i++) ra[idx[i]]=i;
  88. ///主席树
  89. for(int i=1;i<=n;i++){
  90. _v=ra[i];
  91. rt[i]=rt[i-1];
  92. update(rt[i],1,n);
  93. }
  94. ///查询区间第k大
  95. while(m--){
  96. int l,r,k;
  97. scf("%d%d%d",&l,&r,&k);
  98. query(rt[l-1],rt[r],1,n,k);
  99. prf("%d\n",arr[idx[_res]]);
  100. }
  101. }
  102. return 0;
  103. }
  104. //end-----------------------------------------------------------------------

POJ 2104 K-th Number 主席树(区间第k大)的更多相关文章

  1. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  2. 主席树区间第K大

    主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...

  3. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  4. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  5. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  6. Poj 2104 K-th Number(主席树&&整体二分)

    K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...

  7. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  8. zoj2112 树状数组+主席树 区间动第k大

    Dynamic Rankings Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Subm ...

  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. Linux 和 ubuntu安装redis

    Linux 下安装reids 下载地址:http://redis.io/download,下载最新稳定版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://dow ...

  2. React Webpack cookbook

    https://christianalfoni.github.io/react-webpack-cookbook/index.html https://fakefish.github.io/react ...

  3. 【commons】IO工具类——commons-io之IOUtils

    本文转载自xingoo: https://www.cnblogs.com/xing901022/p/5978989.html 一.常用静态变量 public static final char DIR ...

  4. 时间戳转为C#格式时间

    经常发现很多地方使用一个时间戳表示时间.比如: 1370838759 表示 2013年6月10日 12:32:39. 我们就需要一个工具,方便地转换这种时间格式 什么是时间戳? 时间戳, 又叫Unix ...

  5. 【BZOJ1050】[HAOI2006]旅行

    [BZOJ1050][HAOI2006]旅行 题面 bzoj 洛谷 题解 先将所有边从小往大排序 枚举钦定一条最小边 再枚举依次枚举最大边,如果两个点联通了就\(break\)统计答案即可 代码 #i ...

  6. 亲手搭建一个基于Asp.Net WebApi的项目基础框架4

    实现目的:配置website端与服务端对接 1:配置好各项配置文件 2:server端编写接口客户端调用 1.1首先配置文件有log4的配置文件,有config的配置文件,还有服务列表的配置文件 首先 ...

  7. Python爬虫之requests库介绍(一)

    一:Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 ...

  8. hdu1285确定比赛名次(拓扑排序+优先队列)

    传送门 第一道拓扑排序题 每次删除入度为0的点,并输出 这题要求队名小的排前面,所以要用到重载的优先队列 #include<bits/stdc++.h> using namespace s ...

  9. JDBC Mysql 驱动连接异常

    在做JDBC连接Mysql的时候遇到了三个异常: 第一个是:mysql8.0 caching_sha2_password 这个异常是由于是因为在mysql8.0之前的密码规则是mysql_native ...

  10. [leetcode]从中序与后序/前序遍历序列构造二叉树

    从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...