#P3835. 【模板】可持久化平衡树

    ID: 2796 远端评测题 2000ms 1536MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树平衡树深度优先搜索,DFS可持久化

【模板】可持久化平衡树

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:

  1. Insert xx.

  2. Delete xx (if there are multiple equal numbers, delete only one; if none exists, ignore this operation).

  3. Query the rank of xx (rank is defined as the number of elements strictly less than xx, plus 11).

  4. Query the number whose rank is xx.

  5. Query the predecessor of xx (the predecessor is defined as the largest number strictly less than xx; if it does not exist, output 231+1-2^{31}+1).

  6. Query the successor of xx (the successor is defined as the smallest number strictly greater than xx; if it does not exist, output 23112^{31}-1).

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 00 is the initial state, an empty tree).

Input Format

The first line contains a positive integer nn, the total number of operations.

The next nn lines each contain three integers. On the ii-th line, they are vi,opti,xiv_i, \text{opt}_i, x_i.

viv_i is the index of the past version to base on, opti\text{opt}_i is the operation type, and xix_i is the value involved in the operation.

Output Format

Each line contains one integer, in order, corresponding to the answers for operations 3,4,5,63, 4, 5, 6.

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 28%28\% of the testdata, 1n101 \leq n \leq 10.
For 44%44\% of the testdata, 1n2×1021 \leq n \leq 2 \times 10^2.
For 60%60\% of the testdata, 1n3×1031 \leq n \leq 3 \times 10^3.
For 84%84\% of the testdata, 1n1051 \leq n \leq 10^5.
For 92%92\% of the testdata, 1n2×1051 \leq n \leq 2 \times 10^5.
For 100%100\% of the testdata, 1n5×1051 \leq n \leq 5 \times 10^5, xi109|x_i| \leq 10^9, 0vi<i0 \le v_i < i, 1opt61 \le \text{opt} \le 6.

Empirically, persistent balanced trees with normal constant factors can pass. Please rest assured.

Sample explanation:

There are 1010 operations and 1111 versions. The states of each version are:

  1. [][]
  2. [9][9]
  3. [3,9][3, 9]
  4. [9,10][9, 10]
  5. [3,9][3, 9]
  6. [9,10][9, 10]
  7. [2,9,10][2, 9, 10]
  8. [2,9,10][2, 9, 10]
  9. [2,10][2, 10]
  10. [2,10][2, 10]
  11. [3,9][3, 9]

Translated by ChatGPT 5