POJ2104(可持久化线段树)
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.
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).
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
1 //2017-08-07 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #define ll long long 7 #define mid ((l+r)>>1) 8 9 using namespace std; 10 11 const int N = 100010; 12 const int M = N * 30; 13 struct node{//第i棵线段树的节点维护插入i个数字,每个区间的数字个数。 14 int lson, rson, sum; 15 }tree[M]; 16 int root[N], arr[N], arr2[N], tot; 17 int n, m, q; 18 19 void init(){//将原数列排序并去重 20 tot = 0; 21 for(int i = 1; i <= n; i++) 22 arr2[i] = arr[i]; 23 sort(arr2+1, arr2+1+n); 24 m = unique(arr2+1, arr2+1+n)-arr2-1; 25 } 26 27 int getID(int x){ 28 return lower_bound(arr2+1, arr2+1+m, x) - arr2; 29 } 30 31 int build(int l, int r){ 32 int rt = tot++; 33 tree[rt].sum = 0; 34 if(l != r){ 35 tree[rt].lson = build(l, mid); 36 tree[rt].rson = build(mid+1, r); 37 } 38 return rt; 39 } 40 41 int update(int rt, int pos, int value){ 42 int newroot = tot++, tmp = newroot; 43 tree[newroot].sum = tree[rt].sum + value; 44 int l = 1, r = m; 45 while(l < r){ 46 if(pos <= mid){ 47 tree[newroot].lson = tot++; 48 tree[newroot].rson = tree[rt].rson; 49 newroot = tree[newroot].lson; 50 rt = tree[rt].lson; 51 r = mid; 52 }else{ 53 tree[newroot].rson = tot++; 54 tree[newroot].lson = tree[rt].lson; 55 newroot = tree[newroot].rson; 56 rt = tree[rt].rson; 57 l = mid+1; 58 } 59 tree[newroot].sum = tree[rt].sum + value; 60 } 61 return tmp; 62 } 63 64 int query(int lroot, int rroot, int k){ 65 int l = 1, r = m; 66 while(l < r){ 67 if(tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum >= k){ 68 r = mid; 69 lroot = tree[lroot].lson; 70 rroot = tree[rroot].lson; 71 }else{ 72 l = mid + 1; 73 k -= tree[tree[lroot].lson].sum - tree[tree[rroot].lson].sum; 74 lroot = tree[lroot].rson; 75 rroot = tree[rroot].rson; 76 } 77 } 78 return l; 79 } 80 81 int main() 82 { 83 while(scanf("%d%d", &n, &q)!=EOF){ 84 for(int i = 1; i <= n; i++) 85 scanf("%d", &arr[i]); 86 init(); 87 root[n+1] = build(1, m); 88 for(int i = n; i > 0; i--){ 89 int pos = getID(arr[i]); 90 root[i] = update(root[i+1], pos, 1); 91 } 92 while(q--){ 93 int l, r, k; 94 scanf("%d%d%d", &l, &r, &k); 95 printf("%d\n", arr2[query(root[l], root[r+1], k)]); 96 } 97 } 98 99 return 0; 100 }