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 ...
随机推荐
- Oracle->oracle单实例Shell脚本[20180122]
脚本主要用于redhat平台安装11g和12c软件 依赖包检查与安装 用户.组检查与安装 系统内核.用户限制 防火墙.selinux关闭 注意,linux组脚本 ...
- c语言指针的指针
c语言在函数传递时常常使用如下的形式. void get(int **p) 对于这个形式,我想过为什么不能够使用 *p 作为形参呢.下面我们看一下代码和执行结果 void get(int **p) { ...
- C#中如何使用JS脚本
C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...
- Scala学习笔记(二)——Scala基础
1. 常用数据类型 Scala与Java有着相同的常用数据类型: Byte.Short.Int.Long.Float.Double.Chat.Boolean(只有包装类型,无原始类型) Scala继承 ...
- 2018南京网络赛L题:Magical Girl Haze(最短路分层图)
题目链接:https://nanti.jisuanke.com/t/31001 解题心得: 一个BZOJ的原题,之前就写过博客了. 原题地址:https://www.lydsy.com/JudgeOn ...
- Ruby数据类型
数字类型 书写整数时,可以根据需要在整数之间任意加入下划线而不会影响数字的值 a=123_45_78 puts a # => 12345678 to_i 截掉小数点之后的数字取整 内置Math模 ...
- 友晶Altera Cyclone V GX Starter Kit开发板使用ADC-第一篇
1. 拿到板子在,做工很好,属于GX系列,GX应该是高速收发器 2. 去探究下GX是什么用途,大约有6个型号,这个板子是5CGXFX5,有77 LE逻辑单元,这个收发器不知道是什么?6个 3.125G ...
- solr 学习
点击dataimport 没有handler数据 重启下 tomcat 如果没有权限 Cannot find ./catalina.shThe file is absent or does not ...
- 基于Redis+Kafka的首页曝光过滤方案
本文来自网易云社区 作者:李勇 背景 网易美学首页除了banner和四个固定位,大部分都是通过算法推荐获取的内容,其中的内容包括心得.合辑.视频及问答等.现在需要实现的是当推荐内容在用户屏幕曝光后(即 ...
- 惊喜Skr人,Istio的创始人Shriram Rajagopalan手把手教你如何使用Istio
Shriram与来自Google.Lyft.IBM和其他公司的社区贡献者们一起并肩作战,积极地向Istio和Envoy项目作贡献.同时,Shriram是IBM的Amalgam8项目的创始成员之一.目前 ...