#P9776. [HUSTFC 2023] 狭义线段树
[HUSTFC 2023] 狭义线段树
Problem Description
You plan to build a binary tree with nodes, among which there are leaf nodes. Specifically, the pseudocode for building this tree is shown in the figure.

It is not hard to see that node is the root, and the indices of all nodes are the same as their DFS order. Also, you consider leaf nodes very important, so you rename these leaf nodes, in increasing order of node indices, as leaf , leaf , , leaf . Leaf nodes are governed by the nodes above them. More precisely, if leaf is in the subtree of node , then node governs leaf , and we write ; otherwise, if node does not govern leaf , then . Note that a leaf node also governs itself.
Meanwhile, you think a good binary tree should have node weights. For node , define its node weight as . Initially, the node weights of all nodes are .
In a dream, you are modifying this binary tree. You will perform operations on this binary tree in order. The format and description of each operation are as follows:
- : For all integers , add to the node weight of node .
- : Let , where denotes the set of leaf nodes governed by node . Then, for all leaf nodes in , add to their node weights. Note that is a set without duplicates, i.e., in this operation, each leaf node is modified at most once.
- : Compute , where is the sum of the node weights of all nodes that govern leaf , i.e., .
You want to add more operations, but the 8 a.m. bell wakes you up. Still, you decide to implement this whimsical idea.
Input Format
The first line contains an integer , representing the number of leaf nodes in the binary tree.
The second line contains integers, where the -th integer represents the parent index of node . It is guaranteed that the given tree shape matches the node indices and the description in the statement.
The third line contains an integer , representing the number of operations.
In the next lines, the first integer indicates the operation type. If or , it is followed by three integers $s,t,v\ (1\le s\le t\le 2n-1,\ 0\le v<998\,244\,353)$, representing a modification operation. If , it is followed by two integers , representing a query operation. The meanings of all parameters are as described above.
Output Format
For each operation with , output one line with one integer, representing the result of this query.
5
1 2 3 3 2 1 7 7
5
1 2 4 3
3 1 5
2 5 7 5
3 2 5
3 1 5
18
29
38
Hint
Translated by ChatGPT 5