POJ2104 K-th Number #主席树#

K-th Number

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 78400   Accepted: 28315
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

Solution

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 10;
int cnt, a[maxn], root[maxn];
struct node { int l, r, sum; } tree[maxn * 40];
vector<int> v;

inline const 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 << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

inline int getid(int x)
{
    return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}

void update(int l, int r, int p, int pre, int& now)
{
    tree[now = ++cnt] = tree[pre];
    tree[now].sum++;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (p <= mid) update(l, mid, p, tree[pre].l, tree[now].l);
    else update(mid + 1, r, p, tree[pre].r, tree[now].r);
}

int query(int l, int r, int k, int L, int R)
{
    if (l == r) return l;
    int mid = (l + r) >> 1;
    int nl = tree[tree[R].l].sum - tree[tree[L].l].sum;
    return k <= nl ? query(l, mid, k, tree[L].l, tree[R].l) : query(mid + 1, r, k - nl, tree[L].r, tree[R].r);
}

int main()
{
    int n = read(), m = read();
    for (int i = 1; i <= n; i++) v.push_back(a[i] = read());
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for (int i = 1; i <= n; i++) update(1, n, getid(a[i]), root[i - 1], root[i]);
    while (m--)
    {
        int l = read(), r = read(), k = read();
        printf("%d\n", v[query(1, n, k, root[l - 1], root[r]) - 1]);
    }
    return 0;
}
发布了367 篇原创文章 · 获赞 148 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/103265643