This page is still under construction.

Parts of this page are still being built. What you see may change.

The K-th Number

Time limit1sMemory limit256 MB

Summary
Given an array of distinct integers and m range queries, return the k-th smallest value inside each queried subarray.
Level

Hard8 of 10

Topics
Binary search, Divide and conquer, Sorting, Segment tree
Solved
No attempts yet

Problem

You want to build a function that sorts a range of an array and then returns the value at a given rank.

An array a[1…n]a[1 \dots n] of size nn stores nn distinct integers. For this array, define the function Q(i,j,k)Q(i, j, k) as follows.

Q(i,j,k)Q(i, j, k): returns the kk-th smallest value when the subarray a[i…j]a[i \dots j] is sorted in ascending order.

For example, suppose a=(1,5,2,6,3,7,4)a = (1, 5, 2, 6, 3, 7, 4) and consider Q(2,5,3)Q(2, 5, 3). The subarray a[2…5]a[2 \dots 5] is (5,2,6,3)(5, 2, 6, 3), which becomes (2,3,5,6)(2, 3, 5, 6) after sorting. The 3rd value in the sorted array is 5, so Q(2,5,3)=5Q(2, 5, 3) = 5.

Given the array aa and several queries Q(i,j,k)Q(i, j, k), write a program that prints the return value of each query.

Input

The first line contains the array size nn and the number of queries mm, separated by a space. (1≤n≤100,0001 \le n \le 100{,}000, 1≤m≤5,0001 \le m \le 5{,}000)

The second line contains the nn array elements in order. Each element is an integer whose absolute value does not exceed 10910^9, and all elements are distinct.

Each of the following mm lines contains the arguments ii, jj, kk of one query. (1≤i≤j≤n1 \le i \le j \le n, 1≤k≤j−i+11 \le k \le j - i + 1)

Output

For each query, print the return value of Q(i,j,k)Q(i, j, k), one per line.

Examples1

  1. Example 1

    Input
    7 3
    1 5 2 6 3 7 4
    2 5 3
    4 4 1
    1 7 3
    
    Expected output
    5
    6
    3