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 ...
随机推荐
- Vue填坑(1)----通过vue-cli,认识vue-router
开始 首先,确保之前已经安装过 npm 和 nodejs(为了避免版本的问题,最好使用较新的版本). 全局安装 vue-cli : npm install -g vue-cli 新建文件夹 my-pr ...
- MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题
参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...
- Linux 运维工程师学习成长路线上要经历哪四个阶段?
之前曾看到一篇新闻,Linux之父建议大家找一份基于Linux和开源环境的工作.今天就来聊一聊我的想法,本人8年Linux运维一线经验,呆过很多互联网公司,从一线运维做到运维架构师一职,也见证了中国运 ...
- jquery闭包概念
//闭包:有参数的加载事件(空参数形式)(function($){ alert("123");})(jQuery); //有参数的加载事件(function($){ alert($ ...
- day 19 反射
1.isinstance, type, issubclass 的含义 isinstance: 判断你给对象时候是xxx类型的.(向上判断) type: 返回xxx对象的数据类型 issubclass ...
- java读写HDFS
package cn.test.hdfs; import java.io.IOException; import java.net.URI; import java.net.URISyntaxEx ...
- 『Linux基础 - 2 』操作系统,Linux背景知识和Ubuntu操作系统安装
这篇笔记记录了以下几个知识点: 1.目前常见的操作系统及分类,虚拟机 2.Linux操作系统背景知识,Windows和Linux两个操作系统的对比 3.在虚拟机中安装Ubuntu系统的详细步骤 OS( ...
- JZ2440开发板:修改ARM芯片时钟(学习笔记)
想要修改ARM芯片的时钟,需要去查询芯片手册和原理图,获取相关的信息(见下方图片) 首先来看时钟的结构图 根据结构图可以看出,时钟源有两种选择:1. XTIpll和XTOpll所连接的晶振 2. EX ...
- golang 兼容不同json结构体解析实践
线上服务器,同一个web接口有时需要兼容不同版本的结构体.这种情况思路是使用interface{}接收任意类型数据,结合reflect包处理. 如下,http接口调用者会传入不同的json结构数据(单 ...
- 成都Uber优步司机奖励政策(3月11日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...