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.

题目大意:给一串数字,多次询问区间的第k小值

思路:划分树模板题。关于划分树:http://www.cppblog.com/MatoNo1/archive/2011/06/27/149604.aspx

 #include <cstdio>
#include <algorithm>
using namespace std; #define MAXN 100005
int a[MAXN], a_sort[MAXN];
int n, m;
int sum[][MAXN];
int tree[][MAXN]; void build(int l, int r, int dep) {
int i, mid = (l + r) >> , sum_mid = mid - l + , lp = l, rp = mid + ;
for(int i = mid - ; i >= l; --i)
if(a_sort[i] < a_sort[mid]) {sum_mid = mid - i; break;}
for(i = l; i <= r; ++i) {
sum[dep][i] = (i == l ? : sum[dep][i - ]);
if (tree[dep][i] < a_sort[mid]){
++sum[dep][i];
tree[dep + ][lp++] = tree[dep][i];
} else if(tree[dep][i] == a_sort[mid] && sum_mid) {
--sum_mid;
++sum[dep][i];
tree[dep + ][lp++] = tree[dep][i];
} else tree[dep + ][rp++] = tree[dep][i];
}
if (l == r)return;
build(l, mid, dep + );
build(mid + , r, dep + );
} int query(int l, int r, int s, int t, int k, int dep) {
if(l == r) return tree[dep][l];
int mid = (l + r) >> ;
int sum1 = (l == s ? : sum[dep][s - ]), sum2 = sum[dep][t] - sum1;
if(k <= sum2) return query(l, mid, l + sum1, l + sum1 + sum2 - , k, dep + );
else return query(mid + , r, mid + s - l + - sum1, mid + t - l + - sum1 - sum2, k - sum2, dep + );
} int main(){
int s, t, k;
while(scanf("%d%d", &n, &m) != EOF){
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
tree[][i] = a_sort[i] = a[i];
}
sort(a_sort + , a_sort + + n);
build(, n, );
while(m--){
scanf("%d%d%d", &s, &t, &k);
printf("%d\n", query(, n, s, t, k, ));
}
}
return ;
}

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

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

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

  2. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  3. 静态区间第k大(划分树)

    POJ 2104为例[经典划分树问题] 思想: 利用快速排序思想, 建树时将区间内的值与区间中值相比,小于则放入左子树,大于则放入右子树,如果相等则放入左子树直到放满区间一半. 查询时,在建树过程中利 ...

  4. [NBUT 1458 Teemo]区间第k大问题,划分树

    裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...

  5. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  6. POJ 2104 K-th Number(划分树)

    题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...

  7. hdu 2665 Kth number (poj 2104 K-th Number) 划分树

    划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...

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

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

  9. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  10. HDU 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. JAVA基础之控制台输入输出

    ---恢复内容开始--- 输入需要用scanner机制 代码: 启用scanner机制 Scanner input = new Scanner(System.in); //String x= inpu ...

  2. shell脚本显示当前主机系统信息

    编写脚本/root/bin/systeminfo.sh, 当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小. vim /root/bin/system ...

  3. Java线程池的创建详解

    本篇文章主要总结了Java创建线程池的三种方式以及线程池参数的详细说明,对线程池感兴趣的同学可以作为参考学习. 1)通过工具类java.util.concurrent.Executors的静态方法来创 ...

  4. 关于CoreLocation定位服务的简单使用

    在我们发微博,发表空间内容,以及在朋友圈发表动态的时候,会发现有一个位置信息的控件.iOS中是如何定位我们的位置信息的呢?基于此写一个小Demo,供大家参考使用. 在iOS中,用于定位时需要我们导入以 ...

  5. Canvas路径方向

    使用Canvas路径画图需要注意方向,画图方向是顺时针还是逆时针需要记住.下面让我们看看Canvas常见路径方向. arc 参数值 context.arc(x,y,r,sAngle,eAngle,co ...

  6. font(字体)所使用的属性

    1.font-weight:normal blod bolder lighter  100-900之间 400=normal p:first-child{ padding-top: 50px; pos ...

  7. ZYNQ的Linux Linaro系统镜像制作SD卡启动

    ZYNQ的Linux Linaro系统镜像制作SD卡启动 0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ...

  8. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  9. ruby 条件判断&循环控制

    参考:https://www.jb51.net/article/66709.htm

  10. Python学习笔记八:文件操作(续),文件编码与解码,函数,递归,函数式编程介绍,高阶函数

    文件操作(续) 获得文件句柄位置,f.tell(),从0开始,按字符数计数 f.read(5),读取5个字符 返回文件句柄到某位置,f.seek(0) 文件在编辑过程中改变编码,f.detech() ...