#P6136. Dynamic Multiset Operations
Dynamic Multiset Operations
Dynamic Multiset Operations
You are given an initially empty multiset \(M\). Your task is to support the following operations dynamically:
- Insert an integer \(x\) into \(M\).
- Delete one occurrence of integer \(x\) from \(M\) (if there are multiple copies, remove only one).
- Query the number of elements in \(M\) that are less than \(x\), then add one to the answer and output the result.
- Query the \(x\)th smallest element in \(M\) when \(M\) is sorted in increasing order.
- Query the predecessor of \(x\) in \(M\), which is defined as the maximum integer strictly less than \(x\) in \(M\).
- Query the successor of \(x\) in \(M\), which is defined as the minimum integer strictly greater than \(x\) in \(M\).
This is an online problem and all operations are guaranteed to be legal. In particular:
- In operation 2, there is always at least one occurrence of \(x\) to delete.
- For operations 4, 5, and 6, the answer is guaranteed to exist. </p>
- 1 x: Insert \(x\) into \(M\).
- 2 x: Delete one occurrence of \(x\) from \(M\).
- 3 x: Output the value \(\mathrm{count}+1\) where \(\mathrm{count}\) is the number of elements in \(M\) that are strictly less than \(x\).
- 4 x: Output the \(x\)th smallest element in \(M\) (1-indexed).
- 5 x: Output the predecessor of \(x\) in \(M\) (largest element less than \(x\)).
- 6 x: Output the successor of \(x\) in \(M\) (smallest element greater than \(x\)).
inputFormat
The first line contains a single integer \(n\) representing the number of operations.
Each of the next \(n\) lines contains two integers: the first integer indicates the type of operation and the second integer is \(x\). The operations are defined as follows:
For operations that require output (types 3, 4, 5, and 6), print the answer on a new line.
outputFormat
For each query operation (types 3, 4, 5, and 6), output the corresponding result on a separate line.
sample
6
1 10
1 20
3 15
4 2
5 20
6 10
2
20
10
20
</p>