K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 51732   Accepted: 17722
Case Time Limit: 2000MS

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.

Source

Northeastern Europe 2004, Northern Subregion
 
原题大意:给一组数。对于每个查询区间,找出第k小的数。
解题思路:划分树或主席树的裸题,POJ还有一个类似的题目 POJ 2761.
              这两道题的代码一样都可以过。
              所谓划分树,类似于在已经知道区间中位数的情况下的快排,比中位数小的都放左边,大的分右边,相等的看看左边有没有空,有空放左边,没空放右边,相对位置不变,并记录每次的左右子树划分情况。
              如果所求第k小的树,每次我们看看有多少数在左边,就可以知道该查左区间还是右区间。对查询区间做了处理后递归下去即可得出唯一的数值。
  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. int tree[20][110001],sorted[110001],left[20][110001];
  5. void build(int dep,int l,int r)
  6. {
  7. int mid=(l+r)>>1,i,midnum=mid-l+1,sonl=l,sonr=mid+1;
  8. for(i=l;i<=r;++i) if(sorted[i]<sorted[mid]) --midnum; //寻找能放入左子树中值的个数
  9. for(i=l;i<=r;++i)
  10. {
  11. if(i==l) left[dep][i]=0; else left[dep][i]=left[dep][i-1]; //对不同的区间,进行左
  12. //树个数初始化
  13. if(tree[dep][i]==sorted[mid])
  14. {
  15. if(midnum)
  16. {
  17. --midnum;
  18. ++left[dep][i];
  19. tree[dep+1][sonl++]=tree[dep][i]; //统计进入左子树的个数,并更新//下一层树
  20. } else tree[dep+1][sonr++]=tree[dep][i];
  21. } else
  22. if(tree[dep][i]<sorted[mid]) //比中间值小,入左子树
  23. {
  24. ++left[dep][i];
  25. tree[dep+1][sonl++]=tree[dep][i];
  26. } else tree[dep+1][sonr++]=tree[dep][i]; //比中间值小,入右子树
  27. }
  28. if(l==r) return; //如果是叶子结点,返回
  29. build(dep+1,l,mid); //递归左右子树
  30. build(dep+1,mid+1,r);
  31. }
  32. int query(int dep,int l,int r,int ql,int qr,int k)
  33. {
  34. int l_ql,ql_qr,mid=(l+r)>>1;
  35. if(l==r) return tree[dep][l]; //如果找到值,返回
  36. if(l==ql) //恰好是所求左区间为递归左区间
  37. { //
  38. l_ql=0;
  39. ql_qr=left[dep][qr];
  40. } else
  41. {
  42. l_ql=left[dep][ql-1]; //l到ql-1的入左区间数
  43. ql_qr=left[dep][qr]-l_ql; //ql到qr的左区间数
  44. }
  45. if(k<=ql_qr) return query(dep+1,l,mid,l+l_ql,l+l_ql+ql_qr-1,k); else //递归下一区间
  46. return query(dep+1,mid+1,r,mid+1+ql-l_ql-l,mid+1+qr-ql_qr-l_ql-l,k-ql_qr);
  47. //右区间有点乱,ql-l-l_ql即l到ql-1中入右区间的个数依次类推
  48. }
  49. int main()
  50. {
  51. int n,m,i,ql,qr,qk;
  52. scanf("%d%d",&n,&m);
  53. for(i=1;i<=n;++i)
  54. {
  55. scanf("%d",&tree[0][i]);
  56. sorted[i]=tree[0][i];
  57. }
  58. sort(sorted+1,sorted+n+1);
  59. build(0,1,n);
  60. while(m--)
  61. {
  62. scanf("%d%d%d",&ql,&qr,&qk);
  63. printf("%d\n",query(0,1,n,ql,qr,qk));
  64. }
  65. return 0;
  66. }

  

[划分树] POJ 2104 K-th Number的更多相关文章

  1. HDU 4417 (划分树+区间小于k统计)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...

  2. 划分树 静态第k大

    划分树是保存了快速排序的过程的树,可以用来求静态第k小的数 如果,划分树可以看做是线段树,它的左孩子保存了mid-L+1 个 小于等于 a[mid] 的数字,  右孩子保存了 R-mid个大于等于a[ ...

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

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

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

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

  5. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  6. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  7. 主席树----POJ 2104(主席树裸题)(转)

    首先来介绍一下我们需求:给你n个数,多次问你某个区间内的第k小是哪个数 主席树: 主席树的全名应该是 函数式版本的线段树.加上附带的一堆 technology.. ..总之由于原名字太长了,而且 “主 ...

  8. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  9. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

随机推荐

  1. eval 简单应用

     --> 循环来定义变量 //想定义四个变量 one,two,three,four var initVar = ['one','two','three','four']; initVar.for ...

  2. 微信小程序-媒体组件

    audio 音频. MediaError.code 示例代码: <!-- audio.wxml --> <audio poster="{{poster}}" na ...

  3. hihoCoder 1427 : What a Simple Research(大㵘研究)

    hihoCoder #1427 : What a Simple Research(大㵘研究) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 ...

  4. 【转】linux下安装ssh服务器端及ssh的安全配置

    一.在服务器上安装ssh的服务器端. $ sudo apt-get install openssh-server 2. 启动ssh-server. $ /etc/init.d/sshrestart 3 ...

  5. shell中$0,$?,$!等变量意义

    变量说明:   $$     #Shell本身的PID(ProcessID) $!     #Shell最后运行的后台Process的PID $?     #最后运行的命令的结束代码(返回值) $- ...

  6. HTCondor安装

    192.168.30.253 master     cd /etc/yum.repos.dwget http://research.cs.wisc.edu/htcondor/yum/repo.d/ht ...

  7. Docker实践,来自沪江、滴滴、蘑菇街架构师的交流分享

    架构师小组交流会:每期选一个时下最热门的技术话题进行实践经验分享. 第一期主题:容器实践.Docker 作为当前最具颠覆性的开源技术之一,其轻量虚拟化.可移植性是CI/CD,DevOps,微服务的重要 ...

  8. 【腾讯云的1001种玩法】腾讯云搭建DiscuzX论坛

    版权声明:本文由艾可德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/506828001481866457 来源:腾云阁 h ...

  9. RedHat下apache\ftp\mysql 4.0 的安装方法

    RedHat下安装这三个服务的方法大同小异 Apache服务: 找到Apache安装包: rpm -ivh httpd-2.0.40-21.i386.rpm 等待安装完成即可 检查安装结果: rpm ...

  10. 【adb】adb基本命令总结

    adb常用基本命令如下: adb devices           列出你的devices aapt dump badging <file_path.apk>     查看包名 adb ...