#P7826. 「RdOI R3」RBT

「RdOI R3」RBT

Description

You have a rooted tree with root 11, with nodes numbered 1n1\sim n. Node ii has an integer weight aia_i. At the same time, every node is colored red or blue. Initially, all nodes are red. You need to maintain this tree and process qq operations. There are several types of operations, with the following formats:

  • 1 x v: Increase the weight of every node in the subtree of node xx by vv, and take modulo pp.
  • 2 x v: Set axa_x to vv. Note that this does not assign the entire subtree.
  • 3 x: Color node xx blue. Let node jj be the predecessor of node xx in the ranking by node index (not by weight) among its red sibling nodes. Delete the edge connecting node xx to its parent. Then attach node xx as a child of node jj. If node xx has no red siblings, or all its red siblings have indices greater than xx, then do nothing.
  • 4 x: Let SS be the set of weights in the subtree of xx whose occurrence counts are odd. Output the sum of the kk-th power of each number in the set, modulo 998244353998244353. That is, (zSzk)mod998244353(\sum_{z\in S}z^k)\bmod 998244353.

In particular, the testdata guarantees that each node can be operated by operation 3 at most once. In other words, there will be no case where operation 3 is applied to a blue node.

Input Format

The first line contains four integers n,q,p,kn,q,p,k.
The second line contains nn integers, representing the initial a1,a2,,ana_1,a_2,\cdots,a_n.
The next n1n-1 lines each contain two integers f,tf,t, representing an undirected tree edge ftf \leftrightarrow t.
The next qq lines each contain two or three integers op,x(,v)op,x(,v).

Output Format

For each query operation, output one integer per line as the answer.

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

10
5
6
4
12

Hint

Sample Explanation

Sample #1

  • Query 4 1: The weights in the subtree are 3,2,1,2,1,3,2,1,3,43,2,1,2,1,3,2,1,3,4. The set SS is {1,2,3,4}\{1,2,3,4\}, so the answer is 1010.
  • Update 1 3 1: The weights of all nodes become 3,2,2,2,1,4,2,1,3,43,2,2,2,1,4,2,1,3,4.
  • Update 2 1 2: The weights of all nodes become 2,2,2,2,1,4,2,1,3,42,2,2,2,1,4,2,1,3,4.
  • Query 4 1: The weights in the subtree are 2,2,2,2,1,4,2,1,3,42,2,2,2,1,4,2,1,3,4. The set SS is {2,3}\{2,3\}, so the answer is 55.
  • Query 4 3: The nodes in the subtree are 3,63,6, with weights 2,42,4. The set SS is {2,4}\{2,4\}, so the answer is 66.
  • Query 4 6: Since this is a leaf node, the subtree contains only itself with weight 44. The set SS is {4}\{4\}, so the answer is 44.
  • Update 1 4 1: The weights of all nodes become 2,2,2,3,1,4,2,1,3,42,2,2,3,1,4,2,1,3,4.
  • Update 3 7: Color 77 blue, delete the edge 272\leftrightarrow 7, and attach 77 as a child of 55.
  • Update 1 5 1: The weights of all nodes become 2,2,2,3,2,4,3,2,4,52,2,2,3,2,4,3,2,4,5.
  • Query 4 5: The nodes in the subtree are 5,7,8,9,105,7,8,9,10, with weights 2,3,2,4,52,3,2,4,5. The set SS is {3,4,5}\{3,4,5\}, so the answer is 1212.

Constraints

This problem uses bundled testdata.

For all testdata, 1xn1051\le x\le n\le 10^5, 1q1051\le q \le 10^5, 0ai,v<p5000 \le a_i, v < p \le 500, p0p\ne 0, 0k1090\le k \le 10^9.

subtask score special constraints
11 1010 op,ai,x,vop,a_i,x,v and the initial tree shape are generated uniformly at random
22 9090 none

Translated by ChatGPT 5