#P3835. 【模板】可持久化平衡树
【模板】可持久化平衡树
Description
You need to implement a data structure (refer to the problem title) to maintain a multiset of integers, and support the following operations for each historical version:
-
Insert .
-
Delete (if there are multiple equal numbers, delete only one; if none exists, ignore this operation).
-
Query the rank of (rank is defined as the number of elements strictly less than , plus ).
-
Query the number whose rank is .
-
Query the predecessor of (the predecessor is defined as the largest number strictly less than ; if it does not exist, output ).
-
Query the successor of (the successor is defined as the smallest number strictly greater than ; if it does not exist, output ).
Unlike the original balanced tree, every operation here is based on some historical version and produces a new version. (Operations 3, 4, 5, 6 do not modify the original version.)
Each version is numbered by the operation index (version is the initial state, an empty tree).
Input Format
The first line contains a positive integer , the total number of operations.
The next lines each contain three integers. On the -th line, they are .
is the index of the past version to base on, is the operation type, and is the value involved in the operation.
Output Format
Each line contains one integer, in order, corresponding to the answers for operations .
10
0 1 9
1 1 3
1 1 10
2 4 2
3 3 9
3 1 2
6 4 1
6 2 9
8 6 3
4 5 8
9
1
2
10
3
Hint
Constraints
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , , , .
Empirically, persistent balanced trees with normal constant factors can pass. Please rest assured.
Sample explanation:
There are operations and versions. The states of each version are:
Translated by ChatGPT 5
京公网安备 11011102002149号