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
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source
 
Recommend
zty   |   We have carefully selected several similar problems for you:  2660 2662 2667 2663 

pid=2661" target="_blank">2661 

跟POJ2104一样。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector> #define maxn 100005
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; vector<int> T[maxn << 2];
int N, Q; void build(int l, int r, int rt) {
if(l == r) {
int val;
scanf("%d", &val);
T[rt].clear();
T[rt].push_back(val);
return;
} int mid = (l + r) >> 1; build(lson);
build(rson); T[rt].resize(r - l + 1); // Attention
merge(T[rt<<1].begin(), T[rt<<1].end(), T[rt<<1|1].begin(), T[rt<<1|1].end(), T[rt].begin());
} int query(int L, int R, int val, int l, int r, int rt) {
if(L == l && R == r) {
return upper_bound(T[rt].begin(), T[rt].end(), val) - T[rt].begin();
} int mid = (l + r) >> 1; if(R <= mid) return query(L, R, val, lson);
else if(L > mid) return query(L, R, val, rson);
return query(L, mid, val, lson) + query(mid + 1, R, val, rson);
} int main() {
int a, b, c, k, left, right, mid, t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &N, &Q);
build(1, N, 1);
while(Q--) {
scanf("%d%d%d", &a, &b, &k);
left = -1; right = N - 1;
while(right - left > 1) { // binary search
mid = (left + right) >> 1;
c = query(a, b, T[1][mid], 1, N, 1);
if(c >= k) right = mid;
else left = mid;
}
printf("%d\n", T[1][right]);
}
}
return 0;
}

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

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. IPv4与IPv6数据报格式详解

    摘要: 本文给出IPv4与IPv6数据报格式示意图,并整理了各个字段含义,最后对比IPv4与IPv6数据报格式的区别. 一.IPv4数据报 图1 IPv4数据报格式版本号(version) 不同的IP ...

  2. Android系统开发(5)——Eclipse for C/C++

    一.下载JDK 官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 二 ...

  3. 【53.57%】【codeforces 722D】Generating Sets

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 苹果浏览器Safari对html标签submit按钮的默认渲染

    -webkit-appearance: none; 上面的设置就告诉Safari不要使用默认渲染,使用我们写好的 有这么一个webkit的私有属性: -webkit-appearance:none;  ...

  5. 逐步把Nginx及Redis引入项目组之负载均衡技术调研初版总结

    本篇以一个Nginx服务.两个Tomcat服务.一个Redis搭建一个负载均衡环境,由于就一台电脑暂以随机分配client请求策略开展,详细工作中推荐以IP地址来实现client请求的动态负载策略.省 ...

  6. SpringMVC ModelAndView跳转失效

    今天隔壁的兄弟遇到一个奇怪的问题,他写好了一个表单用post提交到了addNew.do 里面,然后利用 return new ModelAndView("forward:success.js ...

  7. [Angular] Export directive functionalities by using 'exportAs'

    Directive ables to change component behaives and lookings. Directive can also export some APIs which ...

  8. js的dom对象(带实例超详细全解)

    js的dom对象(带实例超详细全解) 一.总结 一句话总结: 1.DOM中的方法区分大小写么? 解答:区分 2.DOM中元素和节点的关系式什么? 解答:元素就是标签,节点中有元素节点,也是标签,节点中 ...

  9. color2gray 的实现

    无论是 rgb 还是 yuv 等三通道的颜色空间中的像素点,将其转换为单通道(pixel_depth=255.)中的像素,一般情况下都是采用的对原始颜色空间的 3 通道的像素点线性组合而得到单通道的像 ...

  10. Erlang 杂记

    学习Erlang的时候在书的留白处随手记录了一些东西,还有一些记录在了demo的注释里面,今天抽时间整理出来了一部分,分享一下. Erlang的设计哲学是为每一个独立的事件创建一个新进程. Erlan ...