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 ;
}

最新文章

  1. SpringMVC注解汇总(一)-定义
  2. Android_AsyncTask异步任务机制
  3. 【代码笔记】iOS-判断中英文混合的字符长度的两种方法
  4. Python类的特点 (1):构造函数与方法
  5. JavaSE复习_1 Java的基本格式和运算符
  6. ERP登录(八)
  7. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
  8. 移动web开发中遇到的一些问题收纳
  9. C++建立动态二维数组
  10. Spring.net-业务层仓储
  11. eclipse 组态xdebug
  12. Centos7.0 下挂载磁盘
  13. SQL Server分页存储过程通用存储过程
  14. python利用requests库模拟post请求时json的使用
  15. transmission跳过文件校验功能实现
  16. .NET代码混淆——开源.net 混淆器ConfuserEx介绍
  17. &lt;target&gt;.ID 和 &lt;source&gt;.ID 的属性冲突: DataType 属性不匹配
  18. easyui combobox 动态加载数据C#
  19. mac 无法验证副本
  20. Linux下使用Nohup后台运行程序

热门文章

  1. Go语言中的UDP应用
  2. C++ vector的reserve和resize详解
  3. 关于 js 对象 转 字符串 和 深拷贝 的探讨
  4. List中的FindAll用法
  5. 初步学习pg_control文件之九
  6. Bootstrap4用法
  7. 听雷哥浅谈Redis
  8. Ubuntu下使用Git_1
  9. Qt 实现脉搏检测-2,简陋的功能产品
  10. Kotlin怎样使用Android的Dagger2