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 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).
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<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<map>
using namespace std;
const int maxn = 100005;
const int maxnn= 2500005;
int sca[maxn],T[maxn],ls[maxnn],rs[maxnn],sum[maxnn];
//sca[]输入,T[]每棵线段树根的编号,ls左儿子,rs[]右儿子,sum[]节点对应区间内出现的数的个数
int cnt,n;
vector<int> vec;
void init()//初始化
{
n=cnt=0;
vec.clear();
memset(sum,0,sizeof(sum));
}
int getind(int x)//获取离散化后的值
{
return lower_bound(vec.begin(),vec.end(),x)-vec.begin();
}
int build(int l,int r)//递归建树
{
int now=++cnt;
if(l<r)
{
int mid=(l+r)/2;
ls[now]=build(l,mid);
rs[now]=build(mid+1,r);
}
return now;
}
void build_new(int mark,int val)//对新插入的值建树
{
T[mark]=++cnt;
int now=T[mark],bef=T[mark-1];
sum[now]=sum[bef]+1;
int l=0,r=n;
while(ls[bef]||rs[bef])
{
int mid=(l+r)/2;
if(val<=mid)//当新插入的值在左区间时
{
ls[now]=++cnt,rs[now]=rs[bef];//新建左儿子,右儿子是上一颗树的右儿子
sum[ls[now]]=sum[ls[bef]]+1;
now=ls[now],bef=ls[bef];
r=mid;
}
else//同理
{
ls[now]=ls[bef],rs[now]=++cnt;
sum[rs[now]]=sum[rs[bef]]+1;
now=rs[now],bef=rs[bef];
l=mid+1;
}
}
}
int query(int a,int b,int l,int r,int k)
{
if(l==r)
return l;
int lm=sum[ls[b]]-sum[ls[a]];//记录一下左区间出现的数的个数
int mid=(l+r)/2;
if(k<=lm)//第k大的数在左区间
return query(ls[a],ls[b],l,mid,k);
else//同理
return query(rs[a],rs[b],mid+1,r,k-lm);
}
int main()
{
int N,m;
while(scanf("%d %d",&N,&m)!=-1)
{
init();
for(int i=1; i<=N; i++)
{
scanf("%d",&sca[i]);
vec.push_back(sca[i]);
}
sort(vec.begin(),vec.end());//离散化
vec.erase(unique(vec.begin(),vec.end()),vec.end());
n=vec.size();
T[0]=build(0,n);//注意对T[0]的赋值不能少
for(int i=1; i<=N; i++)
build_new(i,getind(sca[i]));
for(int i=0; i<m; i++)
{
int a,b,k;
scanf("%d %d %d",&a,&b,&k);
printf("%d\n",vec[query(T[a-1],T[b],0,n,k)]);
}
}
return 0;
}