#P3369. 【模板】普通平衡树

【模板】普通平衡树

Description

You need to dynamically maintain a multiset MM and support the following operations:

  1. Insert a number xx into MM.
  2. Delete a number xx from MM (if there are multiple equal numbers, delete only one).
  3. Query how many numbers in MM are less than xx, and then add one to the result (i.e., the rank of xx).
  4. Query the number whose rank is xx when MM is sorted in ascending order.
  5. Query the predecessor of xx in MM (the predecessor is defined as the maximum number that is less than xx).
  6. Query the successor of xx in MM (the successor is defined as the minimum number that is greater than xx).

For operations 3, 5, and 6, it is not guaranteed that xx exists in the current multiset.

For operations 5 and 6, the answer is guaranteed to exist.

Input Format

The first line contains nn, the number of operations. Each of the next nn lines contains two integers opt\text{opt} and xx, where opt\text{opt} denotes the operation index (1opt61 \leq \text{opt} \leq 6).

Output Format

For operations 3, 4, 5, and 6, output one integer per line, the corresponding answer.

10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
106465
84185
492737

Hint

Constraints

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, x107|x| \le 10^7.

Source: Tyvj1728, originally titled: Ordinary Balanced Tree.

Special thanks!

Translated by ChatGPT 5