#P4693. [Ynoi Easy Round 2016] 炸脖龙 II

[Ynoi Easy Round 2016] 炸脖龙 II

Description

You are playing a galgame, and suddenly you hear that the Soviet Union has collapsed. So you realize that you will never see a living Soviet person again. Feeling depressed, you start writing a data structure problem:

This is a simulation problem.

You need to maintain a rooted ordered tree with nn nodes (that is, the children of each node have a left-to-right order). Initially, the root is 11. You need to support the following operations:

A(x,y): Perform path compression on xx, i.e., for every node on the path from xx to the root except the root, set its parent to the root. The order of these nodes among the root’s children remains the same as their order on the original path from shallow to deep.

B(x,y): Take the root’s children x,yx,y (xx is to the left of yy) and all nodes between them, and expand them in order into a single path. xx remains connected to the root. All children that these involved nodes originally had are moved to the right side of the path.

C(x,y): Set the root of the tree to be xx. At this time, nodes that were on the left side of the original path from the old root to xx remain on the left and their relative order does not change; similarly for the right side. After xx becomes the root, the children of xx are placed on the right side of the path from xx to the original root.

D(x,y): Add a number to the weight of every node on the path from xx to the root, and then query the sum of weights on the path from xx to the root.

E(x,y): It is guaranteed that xx and yy have the same parent and that xx is to the left of yy. For every node between xx and yy (including x,yx,y) among the children of xx’s parent, add x+yx+y to its weight, and then query the sum of weights of these nodes.

F(x,y): Add yy as a child of xx, at the far right. This operation is performed before all other operations and is used to describe the initial shape of the tree.

Input Format

The first line contains two integers n,qn,q, representing the number of nodes in the tree and the number of operations.

The next qq lines each describe an operation.

Output Format

For each D or E operation, output one line: the result modulo 2322^{32}.

20 30
F(1,2)
F(1,3)
F(1,7)
F(1,9)
F(9,5)
F(3,8)
F(3,20)
F(3,6)
F(8,15)
F(8,19)
F(20,14)
F(20,12)
F(6,13)
F(6,18)
F(13,4)
F(12,11)
F(12,17)
F(17,10)
F(17,16)
E(3,9)
E(8,6)
A(12,0)
D(4,6)
E(2,7)
B(3,12)
D(4,6)
C(8,0)
D(13,5)
D(1,7)
E(12,14)
36
42
56
89
95
105
90
61

Hint

Idea: ccz181078, Solution: ccz181078, Code: ccz181078, Data: ccz181078

Constraints: 2n1052\leq n\leq 10^5nq2×105n\leq q\leq 2\times 10^50x,yn0\leq x,y \leq n.

All operations are guaranteed to be valid.

For symmetry, all operations have two parameters x,yx,y, but actually yy is not needed in operations A and C. The testdata guarantees that y=0y=0 in these cases.

The first n1n-1 operations are all F operations, guaranteeing that after these n1n-1 operations you get a tree rooted at 11, and no more F operations will appear afterwards.

Note that operations A, B, C, and F all have rules about the order of children. You may refer to the sample explanation for an intuitive understanding.

There are 1010 test groups.

Translated by ChatGPT 5