K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 44537   Accepted: 14781
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

题目大意:给你一段序列,求给定区间[u,v]中第k小的值

代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #define maxn 1000100
  5.  
  6. using namespace std;
  7.  
  8. int n,m,sz;
  9. int hash[maxn],a[maxn],root[maxn],ls[maxn*],rs[maxn*],sum[maxn*];
  10.  
  11. void updata(int l,int r,int x,int &y,int v)
  12. {
  13. y=++sz;
  14. sum[y]=sum[x]+;
  15. if (l==r) return;
  16. ls[y]=ls[x];
  17. rs[y]=rs[x];
  18. int mid=(l+r)>>;
  19. if (v<=mid) updata(l,mid,ls[x],ls[y],v);
  20. else updata(mid+,r,rs[x],rs[y],v);
  21. }
  22.  
  23. int que(int L,int R,int K)
  24. {
  25. int l=,r=n,mid,x,y;
  26. x=root[L-];
  27. y=root[R];
  28. while (l!=r)
  29. {
  30. mid=(l+r)>>;
  31. if (K<=sum[ls[y]]-sum[ls[x]])
  32. {
  33. x=ls[x];
  34. y=ls[y];
  35. r=mid;
  36. }
  37. else
  38. {
  39. K-=(sum[ls[y]]-sum[ls[x]]);
  40. x=rs[x];
  41. y=rs[y];
  42. l=mid+;
  43. }
  44. }
  45. return l;
  46. }
  47.  
  48. int main()
  49. {
  50. scanf("%d%d",&n,&m);
  51. for (int i=;i<=n;i++)
  52. scanf("%d",&a[i]),hash[i]=a[i];
  53. sort(hash+,hash+n+);
  54. for (int i=;i<=n;i++)
  55. {
  56. int w=lower_bound(hash+,hash+n+,a[i])-hash;
  57. updata(,n,root[i-],root[i],w);
  58. }
  59. for (int i=;i<=m;i++)
  60. {
  61. int l,r,v;
  62. scanf("%d%d%d",&l,&r,&v);
  63. printf("%d\n",hash[que(l,r,v)]);
  64. }
  65. return ;
  66. }

C++之路进阶——poj2104(K-th Number)的更多相关文章

  1. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  2. 【POJ2104】K-th Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABToAAAJ2CAIAAADwi6oDAAAgAElEQVR4nOy9a5Pj1nnvi0/Q71Llj3

  3. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  4. C++之路进阶codevs1269(匈牙利游戏)

    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description ...

  5. c++之路进阶——hdu3507(Print Article)

    参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种 ...

  6. C++之路进阶——hdu2222(Keywords Search)

    /*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  7. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...

  8. 【POJ2104】K-th Number(主席树)

    题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...

  9. C++之路进阶——HDU1880(魔咒词典)

    ---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 3 ...

随机推荐

  1. Nginx 常用全局变量 及Rewrite规则详解

    每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...

  2. jfinal框架教程-学习笔记(二)

    上一节介绍了jfinal框架的简单搭建,这节通过一个小例子了解jfinal的结构和特点 先上图 1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table crea ...

  3. javaScript没有块级作用域

    1.如下,变量i,j,k 的作用域是相同的. function test(obj){ var i= 0; if(typeof obj == "object"){ var j = 0 ...

  4. Prism&MEF构建开发框架

    系统框架构想效果图 平台简单由左侧菜单和右侧内容区以及顶部系统和用户信息区构成 菜单根据系统模块动态加载 右侧,根据左侧选中菜单动态加载子模块,子模块集合以tab选项卡方式布局 系统模块划分为Shel ...

  5. link标签和script标签跑到body下面,网页顶部有空白

    用UltraEdit的16进制编辑模式查看代码,都是EF BB BF开头的,说明都是带BOM的.我手动的将所有文件转成UTF-8 without BOM.页面终于正常了.link,script标签乖乖 ...

  6. Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择

    在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...

  7. SQLSERER 中select排序问题

    SELECT  * FROM 表名 ORDER BY PageNO DESC 这种排序会排出这种效果:1, 11,2,20 select             *,              RIG ...

  8. java中String.valueOf()和toString()方法的区别

    http://www.ztyhome.com/android-tostring-string-valueof-diff/

  9. 中国电信某站点JBOSS任意文件上传漏洞

    1.目标站点 http://125.69.112.239/login.jsp 2.简单测试 发现是jboss,HEAD请求头绕过失败,猜测弱口令失败,发现没有删除 http://125.69.112. ...

  10. ASP.NET IIS设置 Session时间

    1.打开IIS需设置的网站主页 2.打开主页IIS--ASP项目,如下图: 3.设置 会话属性---超时 的值,如下图: