Description

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.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
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 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

  1. 7 3
  2. 1 5 2 6 3 7 4
  3. 2 5 3
  4. 4 4 1
  5. 1 7 3

Sample Output

  1. 5
  2. 6
  3. 3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

【思路】

  主席树 

  戳这 click here

【代码】

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. const int N = 1e5+;
  10. const int M = *1e6+;
  11.  
  12. int n,m,tot,sz;
  13. int v[N],hash[N],root[N];
  14. int sum[M],ls[M],rs[M];
  15.  
  16. ll read() {
  17. char c=getchar();
  18. ll x=,f=;
  19. while(!isdigit(c)) {
  20. if(c=='-') f=-; c=getchar();
  21. }
  22. while(isdigit(c))
  23. x=x*+c-'',c=getchar();
  24. return x*f;
  25. }
  26. void update(int l,int r,int x,int& y,int num)
  27. {
  28. y=++sz;
  29. sum[y]=sum[x]+;
  30. if(l==r) return ;
  31. ls[y]=ls[x],rs[y]=rs[x];
  32. int mid=(l+r)>>;
  33. if(num<=mid) update(l,mid,ls[x],ls[y],num);
  34. else update(mid+,r,rs[x],rs[y],num);
  35. }
  36. int query(int x,int y,int rk)
  37. {
  38. int a=root[x-],b=root[y];
  39. int l=,r=tot;
  40. while(l<r) {
  41. int mid=(l+r)>>;
  42. int now=sum[ls[b]]-sum[ls[a]];
  43. if(rk<=now) r=mid,a=ls[a],b=ls[b];
  44. else l=mid+,rk-=now,a=rs[a],b=rs[b];
  45. }
  46. return hash[l];
  47. }
  48. int main() {
  49. n=read(),m=read();
  50. FOR(i,,n)
  51. v[i]=read(),hash[i]=v[i];
  52. sort(hash+,hash+n+);
  53. tot=unique(hash+,hash+n+)-hash-;
  54. FOR(i,,n)
  55. v[i]=lower_bound(hash+,hash+tot+,v[i])-hash;
  56. FOR(i,,n) {
  57. update(,tot,root[i-],root[i],v[i]);
  58. }
  59. int x,y,z;
  60. FOR(i,,m) {
  61. x=read(),y=read(),z=read();
  62. printf("%d",query(x,y,z));
  63. if(i!=m) puts("");
  64. }
  65. return ;
  66. }

poj 2104 K-th Number(主席树)的更多相关文章

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

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

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

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

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

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

  4. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  5. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  6. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  7. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  8. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  9. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

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

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

随机推荐

  1. 荣誉,还是苦逼?| 也议全栈工程师和DevOps

    引言 全栈工程师(本文称「全栈」开发者)和 DevOps 无疑是近期最火的词汇,无论是国外还是国内.而且火爆程度远超于想象. 全栈和 DevOps,究竟是我们的新职业方向,还是仅仅创业公司老板的心头所 ...

  2. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务

    http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...

  3. Apache Flume 简介

    转自:http://blog.163.com/guaiguai_family/blog/static/20078414520138100562883/ Flume 是 Cloudera 公司开源出来的 ...

  4. http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html

    http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html

  5. Android 通过http访问服务器

    目前Android 与服务器交互有两种方式:1.Socket 2. Http : 但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互: 通过http访问服务器有三种 ...

  6. POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)

    The Embarrassed Cryptographer DescriptionThe young and very promising cryptographer Odd Even has imp ...

  7. Android Editext监听光标位置

    因为项目需要,需要实时监听光标的位置变化,网上提出的用TextWatcher和onTouchListener中调用contentText.getSelectionStart()都是获取的上一次位置. ...

  8. Hadoop分布式文件系统(HDFS)详解

    HDFS简介: 当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区 (partition)并存储到若干台单独的计算机上.管理网络中跨多台计算机存储的文件系统成为分布式文件系统 (D ...

  9. P102、面试题14:调整数组顺序使奇数位于偶数前面

    题目:输入一个整数数组,实现一个函数来调整该数组中数字的属性怒,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 思路:其实就是用快速排序法的第一轮排序,从左右夹逼,左边遇到偶数,停下来, ...

  10. 怎样成为一名PHP专家?

    当浏览各类与PHP相关的博客时,比如Quora上的问题,谷歌群组,简讯和杂志,我经常注意到技能的等级分化.问题都类似于“我如何连接到MySQL数据库?”或者“我该如何扩展邮件系统才能在每小时发送超过一 ...