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. Java学习笔记九:Java的循环跳转语句

    Java的循环跳转语句 一:Java循环跳转语句之break: 生活中,我们经常会因为某些原因中断既定的任务安排.如在参加 10000 米长跑时,才跑了 500 米就由于体力不支,需要退出比赛.在 J ...

  2. Python3爬虫(十一) 爬虫与反爬虫

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.重要概念 二.爬虫反爬虫进化论

  3. 关于VSCode如何缩进两个空格

    使用VSCode编写vue的时候,由于缩进问题经常报错.(默认缩进4个空格,实际规范上是两个空格) 更改VSCode的缩进格式. 但是此时你在编写代码的时候却发现任然缩进4格,此时因为vscode默认 ...

  4. 如何在Moodle中显示PPT课件

    Moodle中目前是不直接支持PPT的,所以需要曲线救国: 1.安装 iSpring Free 8版本,免费版,其实是一个PPT的插件,在PPT的工具栏中有显示. 2.打开PPT后,直接在该工具中进行 ...

  5. Moodle 3.4中添加小组、大组、群

    Moodle在高中应用时经常要用到年级.班级和小组,我们可以用群.大组.小组来代替. 小组设置:网站首页-->现有课程-->右上角的设置按钮-->更多-->用户-->小组 ...

  6. 配置ORACLE的PRO*C环境

    1.访问数据库的方法    在ORACLE数据库管理和系统中,有三种访问数据库的方法:    ⑴.用SQL*Plus, 它有SQL命令以交互的应用程序访问数据库:    ⑵.用第四代语言应用开发工具开 ...

  7. 设置socket接收和发送超时的一种方式

    Linux环境设置Socket接收和发送超时: 须如下定义:struct timeval timeout = {3,0};  //设置发送超时setsockopt(socket,SOL_SOCKET, ...

  8. 「题目代码」P1029~P1033(Java)

    1029 C基础-求解方程 import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { ...

  9. npx 命令介绍

    这个是在 npmv5.2.0引入的一条命令(查看),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 为什么引入这个命令 举个例子,我们开发中要运行 parcel 命令来打包:par ...

  10. Vue学习(一):Vue实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...