#P5076. Order Statistic Set Operations
Order Statistic Set Operations
Order Statistic Set Operations
You are required to implement a data structure to maintain a set of integers (each with absolute value at most ). Initially, the set is empty. You need to support the following operations (with at most operations in total):
- Given an integer , report its rank, defined as the number of elements in the set strictly less than , plus one. (Note that may not be present in the set.)
- Query the element with rank (). It is guaranteed that there are at least elements in the set.
- Find the predecessor of in the set (i.e. the largest number in the set that is strictly less than ). If no such number exists, output .
- Find the successor of in the set (i.e. the smallest number in the set that is strictly greater than ). If no such number exists, output .
- Insert the integer into the set. It is guaranteed that is not present in the set before insertion.
It is guaranteed that for operations 1, 3 and 4 the set is non-empty when the operation is executed.
inputFormat
The first line contains an integer (), the number of operations. Each of the following lines contains two integers and , where:
- If , you should output the rank of .
- If , you should output the element whose rank is .
- If , you should output the predecessor of .
- If , you should output the successor of .
- If , you should insert into the set.
outputFormat
For each query operation (operations 1, 2, 3, and 4), output the result on a new line.
sample
5
5 10
5 20
1 10
3 20
4 10
1
10
20
</p>