题目链接

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 36890 Accepted: 11860
Case Time Limit: 2000MS

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.
题意很简单,给定长度为n的数组,有m次查询,每次长须L到R中第k大的数。
很早便想A掉这道题,开始用平方分割写的这题,妥妥的TLE. 后来知道这类题目应该
使用归并树,或者划分树,或是更高级的主席树,刚刚对归并树有一点了解,便来1A了他。
做法就是:如果x是某个区间的第k大数,那么区间内不超过x的数不小于k个。
而且区间内小于x的数不足k个。
二分x, 查询区间内不大于x的数的个数。
Accepted Code:
 /*************************************************************************
> File Name: 2104.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月02日 星期六 19时25分26秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
const int maxn = ;
int n, m;
int a[maxn], nums[maxn];
vector<int> dat[maxn<<]; void build(int o, int l, int r) {
if (r - l == ) {
dat[o].push_back(a[l]);
} else {
int mid = (l + r) / ;
build(lson(o), l, mid);
build(rson(o), mid, r);
dat[o].resize(r-l);
merge(dat[lson(o)].begin(), dat[lson(o)].end(), dat[rson(o)].begin(), dat[rson(o)].end(), dat[o].begin());
}
} int query(int L, int R, int x, int o, int l, int r) {
if (l >= R || r <= L) return ;
else if (l >= L && r <= R) return upper_bound(dat[o].begin(), dat[o].end(), x) - dat[o].begin();
else {
int md = (l + r) / ;
int lc = query(L, R, x, lson(o), l, md);
int rc = query(L, R, x, rson(o), md, r);
return lc + rc;
}
} int main(void) {
while (~scanf("%d %d", &n, &m)) {
for (int i = ; i < n; i++) scanf("%d", a + i);
build(, , n);
sort(a, a + n);
while (m--) {
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
l--; r--;
int lb = -, ub = n-;
while (ub - lb > ) {
int md = (lb + ub) / ;
int c = query(l, r+, a[md], , , n);
if (c >= k) ub = md;
else lb = md;
}
printf("%d\n", a[ub]);
}
} return ;
}

最新文章

  1. iOS block
  2. rsa互通密钥对生成及互通加解密(c#,java,php)
  3. JavaScriptSerializer序列化时间处理
  4. Counting Sequences_线段树***
  5. java微信开发
  6. poj 1845(等比数列前n项和及高速幂)
  7. 用linux的shell脚本把目录下面的所有文件的文件内容中的小写字母改成大写字母
  8. Android系统五大布局详解Layout
  9. python命令行解析工具argparse模块【3】
  10. EasyUI - DataGrid 组建 - [ 组件加载和分页 ]
  11. ios 给view添加一个渐变的背景色
  12. 关闭ipv6的方法
  13. kettle web化
  14. DevExpress--TreeList节点添加图片
  15. BZOJ4259: 残缺的字符串(FFT 字符串匹配)
  16. springmvc访问项目默认先访问后台再返回首页
  17. ABP框架使用Mysql数据库
  18. JavaEE笔记(十三)
  19. Photoshop做32位带Alpha通道的bmp图片
  20. Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied) nginx 启动失败

热门文章

  1. Linux常见问题解答--如何修复“tar:Exiting with failure status due to previous errors”
  2. 23-css补充
  3. nowcoder牛客wannafly挑战赛20
  4. 如何使用Junit进行单元测试
  5. vue 生产环境和测试环境的配置
  6. ES6 class继承
  7. js 之观察者模式
  8. 2018-2-13-win10-uwp-设置启动窗口大小--获取窗口大小
  9. 创建Hadoop用户
  10. Vuejs实战项目步骤一