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 ...
随机推荐
- springboot-redis缓存
Redis缓存使用 1. 引入依赖(可能已经引入了):spring-boot-starter-cache 2. 在application.yml配置文件中配置spring:redis:host/p ...
- tctip打赏小插件
tctip是一个js插件,作用是在web网页右侧生成一个打赏浮动窗 使用方法 页面使用(多数人的使用方式) 插件下载地址 第一步,引入js 一般引入min版本,即引入tctip-版本号.min.js文 ...
- JSP/Servlet开发——第七章 Servel基础
1.Servlet简介: ●Servlet是一个符合特定规范的 JAVA 程序 , 是一个基于JAVA技术的Web组件. ●Servlet允许在服务器端,由Servlet容器所管理,用于处理客户端请求 ...
- Flink基本概念
Flink基本概念 1.The history of Flink? 2.What is Flink? Apache Flink是一个开源的分布式.高性能.高可用.准确的流处理框架,主要由Java代码实 ...
- ecshop跨站漏洞详情及修补网站漏洞
ecshop目前最新版本为4.0,是国内开源的一套商城系统,很多外贸公司,以及电商平台都在使用,正因为使用的人数较多,很多攻击者都在挖掘该网站的漏洞,就在最近ecshop被爆出高危漏洞,该漏洞利用跨站 ...
- Div标签使用inline-block有间距
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PWA-缓存
PWA-缓存 基础 PWA强大的离线能力就在于Service Worker拦截请求及提供缓存的能力,Service Worker的缓存能力比较强大,它能够赋予你更加精确控制缓存的能力.示例页面 < ...
- verilog中参数传递与参数定义中#的作用(二)
一.module内部有效的定义 用parameter来定义一个标志符代表一个常量,称作符号常量,他可以提高程序的可读性和可维护性.parameter是参数型数据的关键字,在每一个赋值语句的右边都必须是 ...
- hive 打印日志
hive -hiveconf hive.root.logger=INFO,console -e 'select 1' hive 打印log ,有时hive 在配置时默认不会将mapper reduce ...
- VMWare虚拟机的网络类型配置选择详解
VMWare虚拟机网络有三种类型,当然还有最后一种类型就是“不使用网络连接”,哈哈....... VMWare在安装会有让选择网络类型的选项,如果不确认使用那一种网络类型,也可以先随便选择一种网络类型 ...