#P9398. 「DBOI」Round 1 烟花

    ID: 9835 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学O2优化树论组合数学排列组合期望

「DBOI」Round 1 烟花

Background

Memory itself is a punishment, punishing those who are unwilling to move forward, trapping them in that little alley, unable to ever get out.

There are fireworks every year, but only the fireworks of that year were the most beautiful.

"I want to make a wish to the fireworks, to wish that we will always be together."

"Even if you don't make a wish, we will always be together."

Later on, the dead were buried on that mountain, and the living were trapped in that alley by memories. Today, when we hear this story, we only want to set off the fireworks from the story one more time, for those who never got to watch fireworks once more with the person beside them.

Problem Description

Fireworks bloom in the night sky and connect into one whole scene. We view these connected fireworks as a rooted tree with nn nodes, where the root is the earliest lit firework 11.

Fireworks have two colors, red and blue. For convenience, we color this tree in black and white. Initially there are pp constraints. Each constraint has the form (xi,yi)(x_i,y_i), meaning that in the subtree of node xix_i, the number of black nodes must be exactly yiy_i. Back then, they called a subtree that satisfies all constraints of all constrained nodes inside its subtree a balanced-good subtree. Clearly, to make a subtree a balanced-good subtree, there may be multiple coloring methods.

You need to answer the following two types of queries:

  • Z k c: For node kk, choose a number ff uniformly at random from [0,c][0,c], then directly add ff unconstrained children to node kk, keeping all other children unchanged. Ask for the expected number of coloring methods that make the subtree rooted at kk a balanced-good subtree.
  • H k: Remove the constraints of all constrained children of kk, and ask for the number of coloring methods that make the subtree rooted at kk a balanced-good subtree.

We do not need to light more fireworks, so all queries are independent of each other; no query will actually modify the original tree.

We know that we may not be able to fully reproduce what happened back then. History is too fragmented, and the possible firework combinations are countless. Therefore, you only need the answer modulo the large prime 998244353998244353.

Input Format

The first line contains two integers n,pn,p, representing the number of nodes in the tree and the number of constraints.

The next n1n-1 lines each contain two numbers u,vu,v, indicating that there is a direct edge between uu and vv. These n1n-1 lines describe the structure of the tree.

The next pp lines each contain two numbers (xi,yi)(x_i,y_i), indicating a constraint: in the subtree rooted at node xix_i, there must be exactly yiy_i black nodes in its subtree.

The next line contains an integer qq, representing the number of queries.

The next qq lines each contain one character plus 11 or 22 integers, representing the queries described in the statement.

Output Format

To reduce the output size, suppose the answer to the ii-th query (with ii starting from 11) is ansians_i. You only need to output one line: the xor-sum of all i×ansii\times ans_i. The final xor-sum and each i×ansii\times ans_i do not need to be taken modulo 998244353998244353, but each query answer ansians_i is the value after taking modulo 998244353998244353.

Note: the standard solution does not depend on the output method. That is, the standard solution can obtain the answer to each query independently.

14 5
1 2
1 3
4 1
5 2
2 6
3 7
3 8
9 4
12 6
11 6
6 10
8 13
14 8
2 3
10 0
7 1
13 1
14 0
10
Z 2 5
H 14
Z 7 3
Z 7 1
H 6
Z 1 9
H 1
H 8
H 12
Z 10 1
665340330

Hint

Sample Explanation

As shown in the figure, the fireworks of Sample #1 form a tree with 1414 nodes, among which 55 are constrained nodes. Different from the statement, we use red fireworks to represent constrained nodes, and blue fireworks to represent unconstrained nodes. The light-blue number at the top-right of a red firework indicates the required number of black nodes.

Initially, the number of valid firework-lighting methods for the subtree rooted at each node is as follows (from left to right, the ii-th item is the answer for the subtree rooted at node ii):

[320,10,4,4,2,8,1,2,2,1,2,2,1,1][320,10,4,4,2,8,1,2,2,1,2,2,1,1]

Below we give the query answers and some explanations:

  • Z 2 5: After adding ii children to node 22, the number of valid firework-lighting methods in the subtree of node 22 is the (i+1)(i+1)-th term of this sequence: [10,20,35,56,84,120][10,20,35,56,84,120]. The total expectation is 3256\frac{325}{6}. Modulo 998244353998244353, it is 166374113166374113.
  • H 14: Since node 1414 has no children, the answer is still 11.
  • Z 7 3: There are 1010 possible valid firework-lighting plans in total, so the expectation is 52\frac{5}{2}. Modulo 998244353998244353, it is 499122179499122179.
  • The answer to Z 7 1 is 499122178499122178.
  • The answer to H 6 is 1616.
  • The answer to Z 1 9 is 3273632736.
  • H 1: After removing the constraints of all constrained children of 11 (only node 22), there are 10241024 possible valid firework-lighting plans.
  • The answer to H 8 is 88.
  • H 12: It has no children, so the answer does not change; it is still 22.
  • The answer to Z 10 1 is 11.

Finally, the xor-sum of all i×ansii\times ans_i is 665340330665340330.

Constraints

Please pay attention to the impact of constant factors on program efficiency.

This problem uses bundled tests and subtask dependencies.

Let S=3×105S=3\times 10^5.

Subtask\rm Subtask nn qq yiy_i cc Special Property Score Dependencies
11 10\leqslant 10 5\leqslant 5 None 1010 None
22 200\leqslant 200 1515 11
33 S\leqslant S 10\leqslant 10 2020 1,21,2
44 S\leqslant S AA 1515 None
55 BB 2020
66 None 1,2,3,4,51,2,3,4,5

Special property AA: p=0p=0.

Special property BB: there are no Z queries.

For 100%100\% of the data, all input numbers are integers S\leqslant S. In particular, 0pn0\leqslant p\leqslant n, and every node index xx in the input satisfies 1xn1\leqslant x \leqslant n. It is guaranteed that each query character is either Z or H, the input is a tree, and for all constraints we have xixj(ij)x_i\neq x_j(i\neq j).


The last snow of winter arrived as promised, and soon a new spring will come. Everything is waiting to wake up again, but the little alley in Fengcheng will never return to its former prosperity.

More than eighty years have passed. We can no longer find the alley from back then, leaving only this story.

Thank you for the fireworks you set off.

Translated by ChatGPT 5