K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 68136 | Accepted: 24078 | |
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.
解析:
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
using namespace std;
const int Max=100010;
int n,m,q,s;
int num[Max],a[Max],sum[Max*20],lc[Max*20],rc[Max*20],root[Max*20];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void pre()
{
sort(a+1,a+n+1);
m=unique(a+1,a+n+1)-a-1;
for(int i=1;i<=n;i++) num[i] = lower_bound(a+1,a+m+1,num[i]) - a;
}
inline void build(int fa,int &now,int l,int r,int num)
{
now = ++s;
sum[now] = sum[fa] + 1;
lc[now] = lc[fa],rc[now] = rc[fa];
if(l==r) return;
int mid = l + r >> 1;
if(num <= mid) build(lc[fa],lc[now],l,mid,num);
else build(rc[fa],rc[now],mid+1,r,num);
}
inline int Q(int root1,int root2,int l,int r,int k)
{
if(l==r) return l;
int mid = l + r >> 1,num = sum[lc[root1]] - sum[lc[root2]];
if(num >= k) return Q(lc[root1],lc[root2],l,mid,k);
else return Q(rc[root1],rc[root2],mid+1,r,k-num);
}
inline void print(int x)
{
if(x < 0) putchar('-'),x=-x;
if(x > 9) print(x/10);
putchar('0' + x % 10);
}
int main()
{
n=get_int(),q=get_int();
for(int i=1;i<=n;i++) num[i] = a[i] = get_int();
pre();
for(int i=1;i<=n;i++) build(root[i-1],root[i],1,m,num[i]);
while(q--)
{
int l=get_int(),r=get_int(),k=get_int();
print(a[Q(root[r],root[l-1],1,m,k)]),putchar('\n');
}
return 0;
}