The K-th Number
Time limit1sMemory limit256 MB
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 of size stores distinct integers. For this array, define the function as follows.
: returns the -th smallest value when the subarray is sorted in ascending order.
For example, suppose and consider . The subarray is , which becomes after sorting. The 3rd value in the sorted array is 5, so .
Given the array and several queries , write a program that prints the return value of each query.
Input
The first line contains the array size and the number of queries , separated by a space. (, )
The second line contains the array elements in order. Each element is an integer whose absolute value does not exceed , and all elements are distinct.
Each of the following lines contains the arguments , , of one query. (, )
Output
For each query, print the return value of , one per line.