poj 2104 K-th number

来源:互联网 发布:蔚来汽车发展前景知乎 编辑:程序博客网 时间:2024/06/05 12:41

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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

#include<cstdio>

#include<cstdlib>
#include<algorithm>
using namespace std;
#define MAX_SIZE 100005
int sorted[MAX_SIZE];//已经排好序的数据
int toleft[25][MAX_SIZE];
int tree[25][MAX_SIZE];
void build_tree(int left, int right, int deep)
{
    int i;
    if (left == right) return ;
    int mid = (left + right) >> 1;
    int same = mid - left + 1; //位于左子树的数据
    for (i = left; i <= right; ++i)
    {//计算放于左子树中与中位数相等的数字个数
        if (tree[deep][i] < sorted[mid])
        {
            --same;
        }
    }
    int ls = left;
    int rs = mid + 1;
    for (i = left; i <= right; ++i)
    {
        int flag = 0;
        if ((tree[deep][i] < sorted[mid]) || (tree[deep][i] == sorted[mid] && same > 0))
        {
            flag = 1;
            tree[deep + 1][ls++] = tree[deep][i];
            if (tree[deep][i] == sorted[mid])
                same--;
        }
        else
        {
            tree[deep + 1][rs++] = tree[deep][i];
        }
        toleft[deep][i] = toleft[deep][i - 1]+flag;
    }
    build_tree(left, mid, deep + 1);
    build_tree(mid + 1, right, deep + 1);
}
int query(int left, int right, int k, int L, int R, int deep)
{
    if (left == right)
        return tree[deep][left];
    int mid = (L + R) >> 1;
    int x = toleft[deep][left - 1] - toleft[deep][L - 1];///位于left左边的放于左子树中的数字个数   [L, left-1]
    int y = toleft[deep][right] - toleft[deep][L - 1];///到right为止位于左子树的个数   [L, right]
    int ry = right - L - y;///到right右边为止位于右子树的数字个数
    int cnt = y - x;///[left,right]区间内放到左子树中的个数
    int rx = left - L - x;///left左边放在右子树中的数字个数
    if (cnt >= k)
    {
        ///printf("sss %d %d %d\n", xx++, x, y);
        return query(L + x, L + y - 1, k, L, mid, deep + 1);
    }
    else
    {
        
        return query(mid + rx  +1 , mid+1+ ry , k - cnt, mid + 1, R, deep + 1);
    }
}
int main()
{
    int m, n;
    int a, b, k;
    int i;
    while (scanf("%d%d", &m, &n) == 2)
    {
        for (i = 1; i <= m; ++i)
        {
            scanf("%d", &sorted[i]);
            tree[0][i] = sorted[i];
        }
        sort(sorted + 1, sorted + 1 + m);
        build_tree(1, m, 0);
        for (i = 0; i < n; ++i)
        {
            scanf("%d%d%d", &a, &b, &k);
            printf("%d\n", query(a, b, k, 1, m, 0));
        }
    }
    return 0;
}
0 0
原创粉丝点击