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. node解析post表单信息

    一共有4种解析方式 urlencoded.json.text .raw 发起请求的form表单中可以设置三种数据编码方式 application/x-www-form-urlencoded.multi ...

  2. Overview of the High Efficiency Video Coding (HEVC) Standard阅读笔记

    1.INTRODUCTION High Efficiency Video Coding(HEVC) <-> H.265 MPEG-4 Advanced Video Coding(AVC) ...

  3. python--基本类型之列表

    Lest(列表): 定义和创建列表: 列表:是python以及其他语言中最常用的数据结构之一.python用 [] 来解析列表列表是可变的.--可以改变列表的内容可以用切片 a=['张三','李四', ...

  4. Spring + MySQL + Mybatis + Redis【二级缓存】

    一.Redis环境 Redis 官网 :http://redis.io/ windows下载:https://github.com/dmajkic/redis/downloads 1.文件解压缩 2. ...

  5. 【转】odoo11新功能及绿色版汇总

    昆山-Jeffery 11:34:00 ,odoo11 新功能: 评论:看到截图,感觉美工上又有所提高 官方的发布说明:https://www.odoo.com/nl_NL/page/odoo-11- ...

  6. idea 常用设置

    1.修改为Eclipse快捷键 File -> Settings -> Keymap => Keymaps改为 Eclipse copy   2.显示行号: File -> S ...

  7. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  8. 高德API+.NET解决租房问题(新增诚信房源)

    作者:李国宝链接:https://zhuanlan.zhihu.com/p/22105008(欢迎点赞)来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 之前有小伙伴反应 ...

  9. 【MVC】 小问题

    [MVC] 小问题 1. url 传参中文乱码 : encodeURIComponent 转码 2. RedirectToAction 重定向 : ajax 调用无效, 直接 url 访问有效 3. ...

  10. 汇编指令MOVSX与MOVZX

    MOVSX 操作数A ,操作数B MOVZX 操作数A ,操作数B 相同点:操作数B 空间必须小于 操作数A 1.格式与MOV基本相同 2.能完成小存储单元向大存储单元的数据传送 比如 movsx e ...