#K90822. Sorted Operations Queries
Sorted Operations Queries
Sorted Operations Queries
You are given a sequence of operations to perform on an initially empty array. There are two types of operations:
- add x: Insert the integer x into the array such that the array remains sorted in non-decreasing order. You can use a binary search insertion method to accomplish this efficiently.
- query k: Output the kth smallest element in the array. Here, the kth smallest element is defined as the element at position k when the array is sorted in non-decreasing order, i.e. if A is the sorted array then the answer is
\(A[k-1]\).
The input begins with an integer n indicating the number of operations. The following n lines each contain one operation. For every query operation, print the result on a new line.
Note: All operations must be processed in the given order.
inputFormat
The first line of the input contains a single integer n (\(1 \le n \le 10^5\)) that denotes the number of operations. Each of the following n lines contains an operation in one of the following two forms:
add x
: where x is an integer (\(|x| \le 10^9\)).query k
: where k is an integer (\(1 \le k \le current\ number\ of\ added\ elements\)).
Input should be read from standard input (stdin).
outputFormat
For each query
operation, output the corresponding result on a new line. The output should be written to standard output (stdout).
5
add 10
add 5
query 1
add 20
query 2
5
10
</p>