PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000141. Giá trị nhỏ thứ k trong đoạn (Range Kth Smallest)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

3 lộ trình

Giá trị nhỏ thứ k trong đoạn (Range Kth Smallest)

Range Kth Smallest

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of NN nonnegative integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1}. The sequence is static.

Each query consists of integers (l,r,k)(l,r,k). Take the elements from index ll through index r−1r-1, and sort these elements into nondecreasing order. Find the element at index kk, using zero-based indexing, in the sorted sequence; equivalently, the (k+1)(k+1)-th smallest value.

Equal elements are counted as separate occurrences; do not remove duplicates. The original sequence is also indexed from 00, and query intervals are half-open [l,r)[l,r).

Answer all queries in input order.

Input

  • The first line contains integers N,QN,Q.
  • The second line contains NN integers a0,a1,…,aN−1a_0,a_1,\ldots,a_{N-1}.
  • Each of the next QQ lines contains integers lj,rj,kjl_j,r_j,k_j for query jj.

Output

Print QQ lines. Line jj contains the (kj+1)(k_j+1)-th smallest value in [lj,rj)[l_j,r_j), counting repeated values separately.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤1001\le N,Q\le100; 0≤ai≤1090\le a_i\le10^9; 0≤lj<rj≤N0\le l_j<r_j\le N; 0≤kj<rj−lj0\le k_j<r_j-l_j for every valid index.
  • Subtask 2 (30 points): 1≤N,Q≤2 0001\le N,Q\le2\,000; 0≤ai≤1090\le a_i\le10^9; 0≤lj<rj≤N0\le l_j<r_j\le N; 0≤kj<rj−lj0\le k_j<r_j-l_j for every valid index.
  • Subtask 3 (50 points): 1≤N,Q≤200 0001\le N,Q\le200\,000; 0≤ai≤1090\le a_i\le10^9; 0≤lj<rj≤N0\le l_j<r_j\le N; 0≤kj<rj−lj0\le k_j<r_j-l_j for every valid index.

Examples

Example 1

Input

5 3
1 4 0 1 3
0 5 2
1 3 1
3 4 0

Output

1
4
1

Explanation

The sequence is 1 4 0 1 3. Sorting [0,5) yields 0 1 1 3 4; rank k=2k=2 has value 1. The elements of [1,3) are 4 0; sorting gives 0 4, and rank k=1k=1 is 4. Interval [3,4) contains only 1, so rank k=0k=0 returns 1. Both occurrences of value 1 in the first query are counted separately.