K-th Number
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.
给定n个数,q次询问,求[l,r] 区间第k大的数,标准的主席树问题。
看了好久了主席树,终于看懂了,先水下题。感觉这篇文章适合入门(对于我来说)。
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 using namespace std; 5 const int N = 1e5+10; 6 int a[N], b[N], root[N*20], ls[N*20], rs[N*20], sum[N*20]; 7 int sz, n, q, ql, qr, k, cnt; 8 void Build(int l, int r, int &o){ 9 o = ++cnt; 10 sum[o] = 0; 11 if(l == r) return; 12 int m = (l+r) >> 1; 13 Build(l, m, ls[o]); 14 Build(m+1, r, rs[o]); 15 } 16 void update(int &o, int last, int l, int r, int pos) { 17 o = ++ cnt; 18 ls[o] = ls[last]; 19 rs[o] = rs[last]; 20 sum[o] = sum[last] + 1; 21 if(l == r) return; 22 int m = (l+r) >> 1; 23 if(pos <= m) update(ls[o], ls[last], l, m, pos); 24 else update(rs[o], rs[last], m+1, r, pos); 25 } 26 27 int query(int ss, int tt, int l, int r, int k) { 28 if(l == r) return l; 29 int m = (l+r) >> 1; 30 int tot = sum[ls[tt]] - sum[ls[ss]]; 31 if(k <= tot) return query(ls[ss], ls[tt], l, m, k); 32 else return query(rs[ss], rs[tt], m+1, r, k-tot); 33 } 34 int main() { 35 scanf("%d%d", &n, &q); 36 for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), b[i] = a[i]; 37 sort(b+1, b+1+n); 38 sz = unique(b+1,b+1+n)-(b+1); 39 cnt = 0; 40 Build(1, sz, root[0]); 41 for(int i = 1; i <= n; i ++) a[i] = lower_bound(b+1,b+1+sz,a[i]) - b; 42 for(int i = 1; i <= n; i ++) update(root[i], root[i-1], 1, sz, a[i]); 43 // for(int i = 1; i <= n; i ++) printf("%d ",root[i] );printf(" "); 44 while(q--) { 45 scanf("%d%d%d", &ql, &qr, &k); 46 int ans = query(root[ql-1], root[qr], 1, sz, k); 47 printf("%d ",b[ans]); 48 } 49 return 0; 50 }