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 wocould 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 wocould 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 shocould 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
Northeastern Europe 2004, northern subregion
Idea: discretization, insertion, and query. For details, see the code.
# Include <cstdio> # include <algorithm> using namespace STD; struct s {int Val, ID, Pos;} node [100005]; bool cmpval (struct s, struct s B) {return. val <B. val;} bool cmpid (struct s A, struct s B) {return. ID <B. ID;} int T [100005], ls [4000000], RS [4000000], VV [100005], cnum [4000000], nodenum, CNT, n, m; void insert (int s, int e, int POs, int pre, Int & X) {x = ++ nodenum; LS [x] = ls [pre]; RS [x] = Rs [pre]; cnum [x] = cnum [pre] + 1; if (s! = E) {int mid = (S + E)> 1; if (Pos <= mid) insert (S, mid, POs, ls [pre], ls [x]); else insert (Mid + 1, E, POs, RS [pre], RS [x]) ;}} int query (int A, int B, int S, int e, int K) {If (S = e) return VV [s]; else {int mid = (S + E)> 1; if (cnum [ls [B]-cnum [ls [a]> = k) return query (LS [a], ls [B], S, mid, k); // compare the difference between the left subtree of Tree B and the left subtree of tree A with else return query (RS [a], RS [B], mid + 1, E, K-cnum [ls [B] + cnum [ls [a]);} int main () {int I, A, B, k; while (~ Scanf ("% d", & N, & M) {for (I = 1; I <= N; I ++) {scanf ("% d ", & node [I]. val); node [I]. id = I; VV [I] = node [I]. val;} Sort (vv + 1, vv + n + 1); CNT = unique (vv + 1, vv + n + 1)-vv-1; // deduplicated sort (node + 1, node + n + 1, cmpval); int p = 1; for (I = 1; I <= N; I ++) // discretization {If (node [I]. val! = VV [p]) P ++; node [I]. pos = P;} Sort (node + 1, node + n + 1, cmpid ); T [0] = ls [0] = Rs [0] = cnum [0] = nodenum = 0; for (I = 1; I <= N; I ++) insert (1, CNT, node [I]. POs, t [I-1], t [I]); While (M --) {scanf ("% d", & A, & B, & K ); printf ("% d \ n", query (T [A-1], t [B], 1, CNT, k ));}}}