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

趁热赶紧来道主席树裸题,感觉有些明白了
主席树,可持久化线段树,核心思想就是维护线段树的历史状态
在这道题中,我们以数字大小为位置,位置上存的是出没出现过(1/0),统计加和
我们依次将数字加入线段树,这样就会形成n个历史版本
再加上我们维护的数据具有可减性,所以对于(l,r),每个节点位置上只要用r这颗线段树减去l线段树对应的值,就是相当于只维护了(l,r)这个区间内的元素的线段树,自然就可以在上面愉快的跳跃了~
这次的主席树大部分是自己写的了2333,看看吧~
 #include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,siz,tot;
int a[N],b[N],rt[N];
struct node{
int l,r,sum;
}t[*N];
int insert(int k,int x,int l,int r){
t[++tot]=t[k];k=tot;
if(l==r){
t[k].l=t[k].r=;
t[k].sum++;
return tot;
}
int mid=(l+r)>>;
if(x<=mid) t[k].l=insert(t[k].l,x,l,mid);
else t[k].r=insert(t[k].r,x,mid+,r);
t[k].sum=t[t[k].l].sum+t[t[k].r].sum;
return k;
}
int query(int lk,int rk,int x,int l,int r){
if(l==r)return l;
int mid=(l+r)>>;
if(t[t[rk].l].sum-t[t[lk].l].sum>=x)return query(t[lk].l,t[rk].l,x,l,mid);
else return query(t[lk].r,t[rk].r,x-(t[t[rk].l].sum-t[t[lk].l].sum),mid+,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
siz=unique(b+,b++n)-b-;
for(int i=;i<=n;i++)a[i]=lower_bound(b+,b++n,a[i])-b;
tot=;
for(int i=;i<=n;i++)
rt[i]=insert(rt[i-],a[i],,n);
for(int i=,l,r,k;i<=m;i++){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(rt[l-],rt[r],k,,n)]);
}
return ;
}

最新文章

  1. Win 10 文件浏览器无法打开
  2. 解决ReSharper自动删除换行
  3. activti表结构
  4. 转connect() to unix:/var/run/php-fpm.sock failed (11: Resource temporarily unavailable)
  5. 2个比较经典的PHP加密解密函数分享
  6. Mustache.js使用笔记(内容属于转载总结)
  7. ti processor sdk linux am335x evm /bin/setup-package-install.sh hacking
  8. AFNetWorking 关于manager.requestSerializer.timeoutInterval 不起作用的问题
  9. JAVA_file(2)
  10. 《Thinking In Java》阅读笔记
  11. 进程锁Lock
  12. Linux内核分析 期末总结
  13. 出现“java.lang.AssertionError: SAM dictionaries are not the same”报错
  14. 几种Linux 查询外网出口IP的方法(转)
  15. python3 subprocess模块
  16. Docker compose自动化部署
  17. day1 作业二:多级菜单操作
  18. Python,OpenGL生命游戏
  19. 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化
  20. c#本地缓存实现

热门文章

  1. 在win7下,easyphp安装过程中MSVCR110.DLL没有被指定在WINDOWS上运行,或者它包含错误
  2. mongodb drop不释放磁盘空间
  3. UVa - 11283 - PLAYING BOGGLE
  4. 第三篇:SpringBoot - 数据库结构版本管理与迁移
  5. 仿支付宝/微信的password输入框效果GridPasswordView解析
  6. python清除数据库错误日志
  7. spring 源码解析
  8. Unity 使用C/C++ 跨平台终极解决方式(PC,iOS,Android,以及支持C/C++的平台)
  9. h5-6 canvas
  10. ASP.NET Razor - C# and VB Code Syntax