Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5425    Accepted Submission(s): 1760
Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases.

For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.


The second line contains n integers, describe the sequence.

Each of following m lines contains three integers s, t, k.

[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
  1. 1
  2. 10 1
  3. 1 4 2 3 5 6 7 8 9 0
  4. 1 3 2
 
Sample Output
  1. 2
 
Source
 
Recommend
zty   |   We have carefully selected several similar problems for you:  2660 2662 2667 2663 

pid=2661" target="_blank">2661 

跟POJ2104一样。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. #define maxn 100005
  7. #define lson l, mid, rt << 1
  8. #define rson mid + 1, r, rt << 1 | 1
  9. using namespace std;
  10.  
  11. vector<int> T[maxn << 2];
  12. int N, Q;
  13.  
  14. void build(int l, int r, int rt) {
  15. if(l == r) {
  16. int val;
  17. scanf("%d", &val);
  18. T[rt].clear();
  19. T[rt].push_back(val);
  20. return;
  21. }
  22.  
  23. int mid = (l + r) >> 1;
  24.  
  25. build(lson);
  26. build(rson);
  27.  
  28. T[rt].resize(r - l + 1); // Attention
  29. merge(T[rt<<1].begin(), T[rt<<1].end(), T[rt<<1|1].begin(), T[rt<<1|1].end(), T[rt].begin());
  30. }
  31.  
  32. int query(int L, int R, int val, int l, int r, int rt) {
  33. if(L == l && R == r) {
  34. return upper_bound(T[rt].begin(), T[rt].end(), val) - T[rt].begin();
  35. }
  36.  
  37. int mid = (l + r) >> 1;
  38.  
  39. if(R <= mid) return query(L, R, val, lson);
  40. else if(L > mid) return query(L, R, val, rson);
  41. return query(L, mid, val, lson) + query(mid + 1, R, val, rson);
  42. }
  43.  
  44. int main() {
  45. int a, b, c, k, left, right, mid, t;
  46. scanf("%d", &t);
  47. while(t--) {
  48. scanf("%d%d", &N, &Q);
  49. build(1, N, 1);
  50. while(Q--) {
  51. scanf("%d%d%d", &a, &b, &k);
  52. left = -1; right = N - 1;
  53. while(right - left > 1) { // binary search
  54. mid = (left + right) >> 1;
  55. c = query(a, b, T[1][mid], 1, N, 1);
  56. if(c >= k) right = mid;
  57. else left = mid;
  58. }
  59. printf("%d\n", T[1][right]);
  60. }
  61. }
  62. return 0;
  63. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU2665 Kth number 【合并树】的更多相关文章

  1. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  2. HDU2665 kth number 线段树做法

    题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树 ...

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

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

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

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

  5. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  6. POJ2104 K-th Number[主席树]【学习笔记】

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

  7. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

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

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

  9. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

随机推荐

  1. anaconda中实现双spyder版本

    1)先在conda中创建一个名为python2的环境,并下载对应版本python2.7 conda create --name python27 python=2.7 2)激活python2环境 ac ...

  2. Oracle 11gR2 静默安装奇怪错误

    在静默安装Oracle 11gR2 的时候发现的奇怪错误,有点摸不着头脑 【步骤一】配置静默文件只安装软件 #--------------------------------------------- ...

  3. Android开发中的小技巧

    转自:http://blog.csdn.net/guxiao1201/article/details/40655661 简单介绍: startActivities (Intent[] intents) ...

  4. [SCSS] Loop Over Data with the SCSS @each Control Directive

    The SCSS @for directive is great when we know how many iterations are required and we only need 1 va ...

  5. js课程 1-4 js变量的作用域是怎样的

    js课程  1-4   js变量的作用域是怎样的 一.总结 一句话总结:只有在函数内部前面带var的变量为局部变量,局部变量只能在函数体内使用. 1.什么情况下会出现NaN类型的错误,举一例? Num ...

  6. [Angular] FormBuildAPI

    Using FormBuilder API can simply our code, for example we want to refactor following code by using F ...

  7. [ES6] Use ES6 Proxies

    A JavaScript Proxy allows you to intercept operations performed on objects, arrays, or functions lik ...

  8. js进阶 11-8 jquery如何获取元素相对于父元素的位置

    js进阶 11-8  jquery如何获取元素相对于父元素的位置 一.总结 一句话总结:用jquery的position方法,但是使用这个方法的前提是父元素相对定位,子元素绝对定位,否则和offset ...

  9. [iOS]iOS开发人员账号申请与注冊流程

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  10. HTML5物理游戏开发 - 越野山地自行车(三)粉碎自行车

    自上一章公布到如今已时隔四月,实在对不住大家.让大家久等了~话说不是我不关注我的博客,而是事情一多起来写博客的时间就少了. 待到今日有空了,回头看了看自己曾经写的文章,猛得发现已经四个月不曾写文章了. ...