POJ2104(可持久化线段树)
2017-08-07 21:47
183 查看
K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 58759 | Accepted: 20392 | |
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//2017-08-07 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define ll long long #define mid ((l+r)>>1) using namespace std; const int N = 100010; const int M = N * 30; struct node{//第i棵线段树的节点维护插入i个数字,每个区间的数字个数。 int lson, rson, sum; }tree[M]; int root , arr , arr2 , tot; int n, m, q; void init(){//将原数列排序并去重 tot = 0; for(int i = 1; i <= n; i++) arr2[i] = arr[i]; sort(arr2+1, arr2+1+n); m = unique(arr2+1, arr2+1+n)-arr2-1; } int getID(int x){ return lower_bound(arr2+1, arr2+1+m, x) - arr2; } int build(int l, int r){ int rt = tot++; tree[rt].sum = 0; if(l != r){ tree[rt].lson = build(l, mid); tree[rt].rson = build(mid+1, r); } return rt; } int update(int rt, int pos, int value){ int newroot = tot++, tmp = newroot; tree[newroot].sum = tree[rt].sum + value; int l = 1, r = m; while(l < r){ if(pos <= mid){ tree[newroot].lson = tot++; tree[newroot].rson = tree[rt].rson; newroot = tree[newroot].lson; rt = tree[rt].lson; r = mid; }else{ tree[newroot].rson = tot++; tree[newroot].lson = tree[rt].lson; newroot = tree[newroot].rson; rt = tree[rt].rson; l = mid+1; } tree[newroot].sum = tree[rt].sum + value; } return tmp; } int query(int lroot, int rroot, int k){ int l = 1, r = m; while(l < r){ if(tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum >= k){ r = mid; lroot = tree[lroot].lson; rroot = tree[rroot].lson; }else{ l = mid + 1; k -= tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum; lroot = tree[lroot].rson; rroot = tree[rroot].rson; } } return l; } int main() { while(scanf("%d%d", &n, &q)!=EOF){ for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); init(); root[n+1] = build(1, m); for(int i = n; i > 0; i--){ int pos = getID(arr[i]); root[i] = update(root[i+1], pos, 1); } while(q--){ int l, r, k; scanf("%d%d%d", &l, &r, &k); printf("%d\n", arr2[query(root[l], root[r+1], k)]); } } return 0; }
相关文章推荐
- POJ 2104 kth number 主席树(可持久化线段树)[指针实现]
- 可持久化线段树|主席树 POJ 2104 区间第k大的数
- POJ 2104 可持久化线段树
- POJ 2104 K-th Number(可持久化线段树)
- POJ 2104 可持久化线段树
- POJ 2104 K-th Number(可持久化线段树-求第K大)
- poj 2104 可持久化线段树
- poj-2104 K-th Number[主席树/函数式线段树/可持久化线段树]
- ACM练级日志:可持久化线段树初级-POJ 2104
- poj 2104 K-th Number(可持久化线段树)
- 【可持久化线段树】poj 2104 静态区间第k大
- poj 2104 可持久化线段树 区间K大 不修改
- 主席树(可持久化线段树)讲解 [POJ 2104] K-th Number
- poj2104 K-th Number 离散+可持久化线段树
- POJ 2104 K-th Number (可持久化线段树)
- POJ 2104 K-th Number 可持久化线段树
- POJ 2104 K-th Number (可持久化线段树)
- POJ 2104 可持久化线段树 求区间第k大
- POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)
- [poj]-2104-K-th Number-可持久化线段树