#P9776. [HUSTFC 2023] 狭义线段树

[HUSTFC 2023] 狭义线段树

Problem Description

You plan to build a binary tree with (2n1)(2n - 1) nodes, among which there are nn leaf nodes. Specifically, the pseudocode for building this tree is shown in the figure.

1

It is not hard to see that node 11 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 nn leaf nodes, in increasing order of node indices, as leaf 11, leaf 22, \dots, leaf nn. Leaf nodes are governed by the nodes above them. More precisely, if leaf ii is in the subtree of node jj, then node jj governs leaf ii, and we write g(j,i)=1g(j,i)=1; otherwise, if node jj does not govern leaf ii, then g(j,i)=0g(j,i)=0. Note that a leaf node also governs itself.

Meanwhile, you think a good binary tree should have node weights. For node ii, define its node weight as viv_i. Initially, the node weights of all nodes are 00.

In a dream, you are modifying this binary tree. You will perform qq operations on this binary tree in order. The format and description of each operation are as follows:

  • 1 s t v1\ s\ t\ v: For all integers i (i[s,t])i\ (i\in [s,t]), add vv to the node weight of node ii.
  • 2 s t v2\ s\ t\ v: Let S=i[s,t]Si\mathcal{S}={\textstyle \bigcup_{i\in [s,t]}}S_i, where SiS_i denotes the set of leaf nodes governed by node ii. Then, for all leaf nodes in S\mathcal{S}, add vv to their node weights. Note that S\mathcal{S} is a set without duplicates, i.e., in this operation, each leaf node is modified at most once.
  • 3 l r3\ l\ r: Compute i=lrf(i)mod998244353\sum^{r}_{i=l}f(i)\bmod 998\,244\,353, where f(i)f(i) is the sum of the node weights of all nodes that govern leaf ii, i.e., f(i)=g(j,i)=1vjf(i)=\sum_{g(j,i)=1}{v_j}.

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 n (3n105)n\ (3\le n\le 10^5), representing the number of leaf nodes in the binary tree.

The second line contains (2n2)(2n-2) integers, where the ii-th integer represents the parent index of node i+1i+1. It is guaranteed that the given tree shape matches the node indices and the description in the statement.

The third line contains an integer q (3q105)q\ (3\le q\le 10^5), representing the number of operations.

In the next qq lines, the first integer opt (opt[1,3])opt\ (opt\in [1, 3]) indicates the operation type. If opt=1opt=1 or opt=2opt=2, 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 opt=3opt=3, it is followed by two integers l,r (1lrn)l,r\ (1\le l\le r\le n), representing a query operation. The meanings of all parameters are as described above.

Output Format

For each operation with opt=3opt=3, 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