K-th Number

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
代码:
#include<stdio.h>
#include
<stdlib.h>
#define maxn 100010
struct T
{
    
int l, r, dep;
    T 
* pl, *pr;
}fn[maxn 
* 3];
int th = 0, a, b, sort[30][maxn], s[maxn], s1, s2;
* build(int l, int r, int dep)
{
    T 
* p = &fn[th++];
    p
->= l, p->= r, p->dep = dep;
    
if (l == r)
    {
        sort[dep][l] 
= s[l];
    }
    
else
    {
        
int m = (l + r) / 2, i, j, k;
        p
->pl = build(l, m, dep + 1), p->pr = build(m + 1, r, dep + 1);
        
for (k = i = l, j = m + 1;i <= m && j <= r;)
        {
            
if (sort[dep + 1][i] < sort[dep + 1][j])
            {
                sort[dep][k
++= sort[dep + 1][i++];
            }
            
else
            {
                sort[dep][k
++= sort[dep + 1][j++];
            }
        }
        
for (; i <= m; i++)
        {
            sort[dep][k
++= sort[dep + 1][i];
        }
        
for (; j <= r; j++)
        {
            sort[dep][k
++= sort[dep + 1][j];
        }
    }
    
return p;
}
int query(T * p, int num)
{
    
int m, l, r, t;
//确定所询问区间里排在num前的数的个数
    if (a <= p->&& p-><= b)
    {
        l 
= p->l, r = p->r;
        
if (sort[p->dep][p->r] <= num)
        {
            
return r - l + 1;
        }
        
while (l < r)
        {
            m 
= (l + r) / 2;
            
if (sort[p->dep][m] > num)
            {
                r 
= m;
            }
            
else
            {
                l 
= m + 1;
            }
        }
        
return r - p->l;
    }
    
else
    {
        m 
= (p->+ p->r) / 2;
        t 
= 0;
        
if (a <= m)
        {
            t 
+= query(p->pl, num);
        }
        
if (b > m)
        {
            t 
+= query(p->pr, num);
        }
        
return t;
    }
}
int main()
{
    
int n, m, i, l, r, mid, pre, c;
    scanf(
"%d%d"&n, &m);
    
for (i = 1; i <= n; i++)
    {
        scanf(
"%d"&s[i]);
    }
    T 
* tree = build(1, n, 0);
    
while (m--)
    {
        scanf(
"%d%d%d"&a, &b, &c);
        l 
= 1, r = n;
        
while (l < r)//二分枚举数来确定其名次
        {
            mid 
= (l + r) / 2;
            pre 
= query(tree, sort[0][mid]);
            
if (pre >= c)
            {
                r 
= mid;
            }
            
else
            {
                l 
= mid + 1;
            }
        }
        printf(
"%d\n", sort[0][l]);
    }
    system(
"pause");
    
return 0;
}
/*
7 100
5 3 2 9 8 1 6

*/