POJ 2104 K-th Number(划分树)
Description
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 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
题目大意:给一串数字,多次询问区间的第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(划分树)的更多相关文章
- 【POJ 2104】 K-th Number 主席树模板题
达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...
- 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 ...
- 静态区间第k大(划分树)
POJ 2104为例[经典划分树问题] 思想: 利用快速排序思想, 建树时将区间内的值与区间中值相比,小于则放入左子树,大于则放入右子树,如果相等则放入左子树直到放满区间一半. 查询时,在建树过程中利 ...
- [NBUT 1458 Teemo]区间第k大问题,划分树
裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- POJ 2104 K-th Number(划分树)
题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...
- hdu 2665 Kth number (poj 2104 K-th Number) 划分树
划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- HDU 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- Linux系统中的vi/vim指令【详解】
vi是Unix世界里极为普遍的全屏幕文本编辑器,vim是它的改进版本Vi IMproved的简称.几乎可以说任何一台Unix机器都会提供这套软件. 只要简单的在Shell下执行vi就可以进入 vi 的 ...
- wireshark利用正则表达式过滤http协议中的jpg png zip等无用的数据包
主要工具:小度随身wifi热点 + wireshark抓包工具.(强烈不建议使用360的产品,非常垃圾,而且干扰代理#墙IP,搞得你不能***) 利用wireshark这个强大的协议分析利器.去分析某 ...
- HTML5开篇定义(更新中)
以下介绍的两种属性是为后面的属性支持左铺垫,大概一看就OK了. 通用属性 id 用于为HTML元素指定唯一标识 style 用于为HTML元素指定CSS指定样式 class 用于匹配CSS样式的cla ...
- angularjs中控制器之间的通信----$on、$emit和$broadcast解析
$on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...
- git找回本地误删的文件或文件夹
一:首先,我们先用git status 看看工作区的变化 application/Admin/Conf/config.php 如果要恢复文件记住这个 application 如果要恢复文件夹记住这个工 ...
- angular2路由之routerLinkActive指令
angular2的routerLinkActive指令在路由激活时添加样式class .red{ color: red;} <a routerLink="/user/login ...
- 分治与递归-找k个临近中位数的数
问题描述:给定由n个互不相同的数组成的集合S以及正整数k≤n,试设计一个O(n)时间算法找出S中最接近S的中位数的k个数. 算法描述: 用线性时间选择实现的算法找到中位数 S’=除去中位数外的S S& ...
- vimrc 配置
" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by" the call to : ...
- verilog中参数传递与参数定义中#的作用(二)
一.module内部有效的定义 用parameter来定义一个标志符代表一个常量,称作符号常量,他可以提高程序的可读性和可维护性.parameter是参数型数据的关键字,在每一个赋值语句的右边都必须是 ...
- TensorFlow之tf.nn.dropout():防止模型训练过程中的过拟合问题
一:适用范围: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层 二:原理: dropout就是在不同的训练过程中随机扔掉一部分神经元.也就是让 ...