luogu#P5055. 【模板】可持久化文艺平衡树

【模板】可持久化文艺平衡树

Background

This is a template problem.

Problem Description

You need to implement a data structure to maintain a sequence, supporting the following operations (for every past historical version):

  1. Insert a number xx after the pp-th number.
  2. Delete the pp-th number.
  3. Reverse the interval [l,r][l,r]. For example, if the original sequence is {5,4,3,2,1}\{5,4,3,2,1\}, after reversing [2,4][2,4], it becomes {5,2,3,4,1}\{5,2,3,4,1\}.
  4. Query the sum of all numbers in the interval [l,r][l,r].

Different from an ordinary balanced tree, every operation is performed based on some historical version, and generates a new version (operation 44 keeps the original version unchanged). The new version is numbered as the index of this operation.

This problem is strictly online.

Input Format

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

The next nn lines each start with two integers vi,optiv_i, \mathrm{opt}_i. Here, viv_i is the index of the past version it is based on (0vi<i0 \le v_i < i), and opti\mathrm{opt}_i is the operation type (1opti41 \le \mathrm{opt}_i \le 4).

If opti=1\mathrm{opt}_i=1, then two more integers pi,xip_i, x_i follow, meaning: insert number xx after the pip_i-th number.
If opti=2\mathrm{opt}_i=2, then one more integer pip_i follows, meaning: delete the pip_i-th number.
If opti=3\mathrm{opt}_i=3, then two more integers li,ril_i, r_i follow, meaning: reverse the interval [li,ri][l_i, r_i].
If opti=4\mathrm{opt}_i=4, then two more integers li,ril_i, r_i follow, meaning: query the sum of the interval [li,ri][l_i, r_i].

Strict online rule:
Let the answer of the last operation of type 44 before the current operation be lastanslastans (if there is no previous type 44 operation, then lastans=0lastans=0).
Then pi,xip_i,x_i or li,ril_i,r_i of this operation are each XORed bitwise with lastanslastans to obtain the real pi,xip_i,x_i or li,ril_i,r_i.

Output Format

For each query operation with type 44, output one line with one number: the sum of the interval.

10
0 1 0 1
1 1 1 2
2 4 1 2
3 1 2 0
4 4 2 1
5 3 5 7
6 4 5 6
4 1 7 1
8 3 4 6
9 4 4 1
3
4
5
10

Hint

:::info[Sample Explanation] The sample input:

10
0 1 0 1
1 1 1 2
2 4 1 2
3 1 1 3
4 4 1 2
5 3 1 3
6 4 1 2
4 1 2 4
8 3 1 3
9 4 1 4

After all operations, a total of 1111 versions are generated:

Version 00 (initial version): \varnothing

Version 11: {1}\{1\}

Version 22: {1,2}\{1,2\}

Version 33: {1,2}\{1,2\} (this operation queries the sum on [1,2]\left[1,2\right] and outputs 33.)

Version 44: {1,3,2}\{1,3,2\}

Version 55: {1,3,2}\{1,3,2\} (this operation queries the sum on [1,2]\left[1,2\right] and outputs 44.)

Version 66: {2,3,1}\{2,3,1\}

Version 77: {2,3,1}\{2,3,1\} (this operation queries the sum on [1,2]\left[1,2\right] and outputs 55.)

Version 88: {1,3,4,2}\{1,3,4,2\}

Version 99: {4,3,1,2}\{4,3,1,2\}

Version 1010: {4,3,1,2}\{4,3,1,2\} (this operation queries the sum on [1,4]\left[1,4\right] and outputs 1010.) ::::

Strict online: the following constraints on pi,xi,li,rip_i, x_i, l_i, r_i are after XORing with lastanslastans.

  • For 66 test points, n5000n \le 5000.
  • For another 66 test points, vi=i1v_i = i - 1.
  • Users are welcome to strengthen the testdata. You may contact the administrator or the problem setter.

For 100%100\% of the data, 1n2×1051 \le n \le 2 \times {10}^5, xi<106|x_i| < {10}^6.

Assume the sequence length of the referenced historical version is len1len \ge 1, then:
If opti=1\mathrm{opt}_i=1, then 0pilen0 \le p_i \le len.
If opti=2\mathrm{opt}_i=2, then 1pilen1 \le p_i \le len.
If opti=3\mathrm{opt}_i=3, then 1lirilen1 \le l_i \le r_i \le len.
If opti=4\mathrm{opt}_i=4, then 1lirilen1 \le l_i \le r_i \le len.

Assume the sequence length of the referenced historical version is 00, then:
opti=1\mathrm{opt}_i=1, pi=0p_i=0.

Translated by ChatGPT 5