POJ

来源:互联网 发布:南开大学VPN网络 编辑:程序博客网 时间:2024/06/15 05:31

K-th Number

 POJ - 2104


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 10 9 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.


Source
POJ - 2104


My Solution
题意:给出一个数组,每次询问这个数组的区间[L, R]内第k大的数是什么。

主席树基础题
主席树,又称可持久化线段树,是对于数组的每个前缀a[1...i]建立一颗线段树,并且a[1...i]的线段树和a[1...i+1]的线段树的区别是只有树上的一条链不同,所以每次对于一颗新的线段树其实只是添加一条长度为logn的链。
它的核心是寻找公共节点和主席树的加减性。
时间复杂度每次查询和修改都是logn,并且空间复杂度大概是O(nlogn)所以一般开20*MAXN的数组应该没有问题。
这里推荐一篇讲解主席树讲得很好的博客 树状结构之主席树

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int MAXN = 1e5 + 8;//Chairman_Treeint tot, sz;int a[MAXN], b[MAXN], root[20*MAXN], ls[20*MAXN], rs[20*MAXN], sum[20*MAXN];inline void _build(int &o, int l, int r){    o = ++ tot;    sum[o] = 0;    if(l == r) return;    int mid = (l + r) >> 1;    _build(ls[o], l, mid);    _build(rs[o], mid + 1, r);}inline void update(int &o, int l, int r, int last, int p){    o = ++ tot;    ls[o] = ls[last];    rs[o] = rs[last];    sum[o] = sum[last] + 1;    if(l == r) return;    int mid = (l + r) >> 1;    if(p <= mid) update(ls[o], l, mid, ls[last], p);    else update(rs[o], mid + 1, r, rs[last], p);}inline int _query(int ss, int tt, int l, int r, int k){    if(l == r) return l;    int mid = (l + r) >> 1;    int  cnt = sum[ls[tt]] - sum[ls[ss]];    if(k <= cnt) return _query(ls[ss], ls[tt], l, mid, k);    else return _query(rs[ss], rs[tt], mid + 1, r, k - cnt);}int main(){    #ifdef LOCAL    freopen("a.txt", "r", stdin);    //freopen("a.out", "w", stdout);    int T = 1;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    int n, m, i, ql, qr, x, ind;    cin >> n >> m;    for(i = 1; i <= n; i++){        cin >> a[i];        b[i] = a[i];    }    sort(b + 1, b + n + 1);    sz = unique(b + 1, b + n + 1) - (b + 1);    for(i = 1; i <= n; i++){        a[i] = lower_bound(b + 1, b + sz + 1, a[i]) - b; //It is based on 1~n..    }    tot = 0;    _build(root[0], 1, sz);    for(i = 1; i <= n; i++){        update(root[i], 1, sz, root[i-1], a[i]);    }    while(m--){        cin >> ql >> qr >> x;        ind = _query(root[ql - 1], root[qr], 1, sz, x);        cout << b[ind] << "\n";    }    #ifdef LOCAL    cout << endl;    }    #endif // LOCAL    return 0;}


  Thank you!
                                                                                                                                             ------from ProLights




原创粉丝点击