K-th Number

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 45560   Accepted: 15154
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.

Source

Northeastern Europe 2004, Northern Subregion
 
据说是和快排有关, 奈何代码太销魂。
//代码异常销魂
#include <cstdio>
#include <algorithm>
#define N 100000+1
#define M 100000;
using namespace std;
int n, m, a[N], sa[N], t[20][N], s[20][N];
inline int read()
{
    int x=0, f=1; char ch=getchar();
    while(ch < '0'||ch > '9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f; 
}
void build(int h, int l, int r)
{
    int mid=(l+r)>>1,num=0;  
    for(int i=l;i<=r;i++)  
    if (t[h][i]<=mid)    
    {          
        t[h+1][l+num]=t[h][i];  
        s[h][i]=++num;     
    } 
    else  
    {  
        t[h+1][mid+1+i-l-num]=t[h][i];  
        s[h][i]=num;
    }  
    if(l==r)return; 
    build(h+1,l,mid);    
    build(h+1,mid+1,r);
}
int find(int h, int l, int r, int x, int y, int k)
{
    if(l==r) return t[h][l];
    int mid=(l+r)>>1, ll, rr;
    ll=(l==x)?0:s[h][x-1]; rr=s[h][y];
    if(rr-ll>=k) return find(h+1, l, mid, l+ll, l+rr-1, k);
    else return find(h+1, mid+1, r, mid+1+x-l-ll, mid+1+y-l-rr, k-(rr-ll));  
} 
bool cmp(int x, int y)
{
    return a[x]<a[y];
}
int main()
{
    n=read(); m=read();
    for(int i=1; i<=n; i++) {a[i]=read(); sa[i]=i;}
    sort(sa+1, sa+n+1, cmp);
    for(int i=1; i<=n; i++)
        t[0][sa[i]]=i;
    build(0, 1, n);
    for(int i=1; i<=m; i++)
    {
        int x, y, k;
        x=read(); y=read(); k=read();
        printf("%d\n", a[sa[find(0, 1, n, x, y, k)]]);        
    } 
    return 0;
}

 

posted on 2016-03-21 19:37  cleverbiger  阅读(201)  评论(0)    收藏  举报