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.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
 
今天主要来学习一个划分树 ,也就是入门吧  ,我先拓宽一下知识面吧 ,
感觉只有拓宽算法知识面才能更加深入 学习更复杂的算法
 
 划分树板子
 
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + ;
int a[maxn], sorted[maxn];
int num[][maxn], val[][maxn]; void build(int l, int r, int ceng) {
if (l == r) return ;
int mid = (l + r) >> , same = mid - l + ;
for (int i = l ; i <= r ; i++)
if (val[ceng][i] < sorted[mid]) same--;
int ln = l, rn = mid + ;
for (int i = l ; i <= r ; i++) {
if (i == l) num[ceng][i] = ;
else num[ceng][i] = num[ceng][i - ];
if (val[ceng][i] < sorted[mid] || val[ceng][i] == sorted[mid] && same > ) {
val[ceng + ][ln++] = val[ceng][i];
num[ceng][i]++;
if (val[ceng][i] == sorted[mid]) same--;
} else val[ceng + ][rn++] = val[ceng][i];
}
build(l, mid, ceng + );
build(mid + , r, ceng + );
} int query(int ceng, int L, int R, int l, int r, int k) {
if (L == R) return val[ceng][L];
int lsum;
if (l == L) lsum = ;
else lsum = num[ceng][l - ];
int tot = num[ceng][r] - lsum;
if (tot >= k) return query(ceng + , L, (L + R) / , L + lsum, L + num[ceng][r] - , k);
else {
int lr = (L + R) / + + (l - L - lsum);
return query(ceng + , (L + R) / + , R, lr, lr + r - l + - tot - , k - tot);
}
} int main() {
int n, m, l, r, k;
while(scanf("%d%d", &n, &m) != EOF) {
for (int i = ; i <= n ; i++) {
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted + , sorted + n + );
build(, n, );
while(m--) {
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", query(, , n, l, r, k ));
}
}
return ;
}

poj 2104 (划分树模板)的更多相关文章

  1. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

  2. poj 2104 划分树

    思路:裸的划分树 #include<iostream> #include<algorithm> #include<cstring> #include<cstd ...

  3. hdu 4417,poj 2104 划分树(模版)归并树(模版)

    这次是彻底把划分树搞明确了,与此同一时候发现了模版的重要性.敲代码一个字符都不能错啊~~~ 划分树具体解释:点击打开链接 题意:求一组数列中随意区间不大于h的个数. 这个题的做法是用二分查询  求给定 ...

  4. POJ 2104 主席树模板题

    #include <iostream> #include <cstdio> #include <algorithm> int const maxn = 200010 ...

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

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

  6. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

  7. poj2104(划分树模板)

    poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...

  8. poj2104(划分树模板)

    poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...

  9. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

随机推荐

  1. R语言绘图:箱线图

    使用ggplot2绘制箱线图 ######*****绘制箱线图代码*****####### data1$学区房 <- factor(data1$school, levels = 0:1, lab ...

  2. spring boot打包问题

    java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories ...

  3. 批处理bat实现创建、复制、删除文件及文件夹

    转自:http://blog.csdn.net/linda1000/article/details/10221285 1 建bat文件自动执行复制,删除命令. 例1:以下是复制cd.dll文件至win ...

  4. 纯HTML+CSS实现阿童木头像

    他有十万马力,能上天能入地:他分辨善恶,是勇敢正义化身:1963年,他登上荧幕,在日本创下了未曾有过的高收视率……他叫阿童木,一个纯真.善良.勇 敢的机器娃娃.“阿童木之父”手冢治虫曾说,将阿童木生日 ...

  5. 深度可分卷积(Depthwise Separable Conv.)计算量分析

    上次读到深度可分卷积还是去年暑假,各种细节都有些忘了.记录一下,特别是计算量的分析过程. 1. 标准卷积和深度可分卷积 标准卷积(MobileNet论文中称为Standard Convolution, ...

  6. Spring Cloud 自定义ConfigServer 解决敏感信息存储问题

    公司需要将系统配置信息中的敏感信息独立存放. 现有系统采用Spring Cloud Config提供配置信息,其中敏感信息主要是Db配置,分解本次需求: (1)数据库配置信息分离(主要是Db信息). ...

  7. DFS做题小结

    一.深入理解DFS 采用递归写法 深度优先,思路就是沿着一条路一直走,直到走到死胡同,原路返回,返回到有多条道路的地方换其他路走.直到这条支路全部都访问过了,按照原路返回,回到起点,如果起点还有别的支 ...

  8. NO4——并查集

    int find(int x) { int r = x; while(father[r]!=r) r = father[r]; return r; } /* int find(int x) { if( ...

  9. Python-爬取"我去图书馆"座位编码

    原文地址:http://fanjiajia.cn/2018/11/22/Python-%E7%88%AC%E5%8F%96%E2%80%9D%E6%88%91%E5%8E%BB%E5%9B%BE%E4 ...

  10. 手动修改PHP页面返回状态码

    <?php //比如当前页面要返回404状态码 header("HTTP/1.1 404 Not Found"); header("Status: 404 Not ...