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

#CC0000070. Số nhỏ thứ k trong một đoạn (MKTHNUM - K-th Number)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số nhỏ thứ k trong một đoạn (MKTHNUM - K-th Number)

MKTHNUM - K-th Number

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array of nn pairwise distinct integers, each query (l,r,k)(l,r,k) asks for the kk-th smallest value among the elements in the inclusive subarray [l,r][l,r].

Input

The first line contains n,qn,q. The second contains nn distinct integers. Each of the next qq lines contains l,r,kl,r,k.

Output

For each query, print the original value of the kk-th smallest element, not its compressed rank.

Subtasks

Original constraints: 1≤n≤1051\le n\le10^5; 1≤q≤50001\le q\le5000; ∣ai∣≤109|a_i|\le10^9; all aia_i distinct; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤r−l+11\le k\le r-l+1.

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤q≤201\le q\le20; ∣ai∣≤50|a_i|\le50; all elements distinct; 1≤l≤r≤n1\le l\le r\le n, 1≤k≤r−l+11\le k\le r-l+1.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤q≤5001\le q\le500; ∣ai∣≤100000|a_i|\le100000; all elements distinct; 1≤l≤r≤n1\le l\le r\le n, 1≤k≤r−l+11\le k\le r-l+1.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le100000; 1≤q≤50001\le q\le5000; ∣ai∣≤1000000000|a_i|\le1000000000; all elements distinct; 1≤l≤r≤n1\le l\le r\le n, 1≤k≤r−l+11\le k\le r-l+1.

Examples

Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Output

5
6
3

Explanation

The subarray [5,2,6,3][5,2,6,3] sorts to [2,3,5,6][2,3,5,6], whose third smallest element is 55.