题目描述

English Vietnamese 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.

输入输出格式

输入格式:

The first line of the input contains n — the size of the array, and m — the number of questions to answer (1 ≤ n ≤ 100000, 1 ≤ m ≤ 5000).

The second line contains n different integer numbers not exceeding 10^9 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).


SAMPLE INPUT
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

输出格式:


For each question output the answer to it — the k-th number in sorted
a[i ... j] segment. SAMPLE OUTPUT
5
6
3

Note : naive solution will not work!!!

打个版,留给以后复制粘贴2333

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define maxn 100005
using namespace std;
struct node{
node *lc,*rc;
int s;
}nil[maxn*30],*rot[maxn],*cnt;
int a[maxn],num[maxn],n,ky;
int m,le,ri,k;
char ch; node *update(node *u,int l,int r){
node *ret=++cnt;
*ret=*u;
ret->s++; if(l==r) return ret; int mid=l+r>>1;
if(le<=mid) ret->lc=update(ret->lc,l,mid);
else ret->rc=update(ret->rc,mid+1,r); return ret;
} int query(node *u,node *v,int l,int r){
if(l==r) return num[l]; int mid=l+r>>1,c=v->lc->s-u->lc->s;
if(k<=c) return query(u->lc,v->lc,l,mid);
else{
k-=c;
return query(u->rc,v->rc,mid+1,r);
}
} inline void prework(){
cnt=rot[0]=nil->lc=nil->rc=nil;
nil->s=0; for(int i=1;i<=n;i++){
le=a[i];
rot[i]=update(rot[i-1],1,ky);
}
} inline void solve(){
while(m--){
scanf("%d%d%d",&le,&ri,&k);
printf("%d\n",query(rot[le-1],rot[ri],1,ky));
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i),num[i]=a[i];
sort(num+1,num+n+1);
ky=unique(num+1,num+n+1)-num-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(num+1,num+ky+1,a[i])-num; prework();
solve(); return 0;
}

  

最新文章

  1. 【Java每日一题】20161228
  2. SharePoint 2007 Full Text Searching PowerShell and CS file content with SharePoint Search
  3. 获取指定DLL程序集Config 文件
  4. 单元测试中使用Moq对EF的DbSet进行mock
  5. 问题解决——使用串口调试助手发送控制字符 协议指令 &lt;ESC&gt;!?
  6. 设计模式之状态模式(State)
  7. JSP学习笔记(一)
  8. Linux内核态抢占机制分析
  9. jstl的小问题
  10. 50个很棒的Python模块
  11. Mysql获取去重后的总数
  12. elk5.4小白踩坑记录
  13. php 数组排序得方法
  14. linux一些工具的安装(三)
  15. Spring Boot实现文件下载功能
  16. PLSQL远程连接数据库(亲测可试)
  17. 《Linux内核设计与实现》读书笔记 5
  18. Java: String.split(....); 结果很意外
  19. 事件对象——event
  20. Android逆向 Android平台虚拟机

热门文章

  1. Android百度地图开发 百度地图得到当前位置
  2. Set-DnsServerGlobalQueryBlockList
  3. C 语言 习题 1-9
  4. BugKu 2B+基于python的opencv的安装-------CTF 盲水印的套路
  5. python学习-- Django REST framework 序列化数据操作
  6. 菜鸟之路——机器学习之非线性回归个人理解及python实现
  7. Android view相关与自定义View
  8. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)
  9. 二叉树节点个数,叶子个数,第K层个数,最低公共节点
  10. 洛谷 [P3377] 左偏树(可并堆)