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):
- Insert a number after the -th number.
- Delete the -th number.
- Reverse the interval . For example, if the original sequence is , after reversing , it becomes .
- Query the sum of all numbers in the interval .
Different from an ordinary balanced tree, every operation is performed based on some historical version, and generates a new version (operation 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 , the total number of operations.
The next lines each start with two integers . Here, is the index of the past version it is based on (), and is the operation type ().
If , then two more integers follow, meaning: insert number after the -th number.
If , then one more integer follows, meaning: delete the -th number.
If , then two more integers follow, meaning: reverse the interval .
If , then two more integers follow, meaning: query the sum of the interval .
Strict online rule:
Let the answer of the last operation of type before the current operation be (if there is no previous type operation, then ).
Then or of this operation are each XORed bitwise with to obtain the real or .
Output Format
For each query operation with type , 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 versions are generated:
Version (initial version):
Version :
Version :
Version : (this operation queries the sum on and outputs .)
Version :
Version : (this operation queries the sum on and outputs .)
Version :
Version : (this operation queries the sum on and outputs .)
Version :
Version :
Version : (this operation queries the sum on and outputs .) ::::
Strict online: the following constraints on are after XORing with .
- For test points, .
- For another test points, .
- Users are welcome to strengthen the testdata. You may contact the administrator or the problem setter.
For of the data, , .
Assume the sequence length of the referenced historical version is , then:
If , then .
If , then .
If , then .
If , then .
Assume the sequence length of the referenced historical version is , then:
, .
Translated by ChatGPT 5