#P5499. [LnOI2019] Abbi 并不想研学
[LnOI2019] Abbi 并不想研学
Description
[Original Problem]
Given a tree with nodes. All leaf nodes are numbers, and all non-leaf nodes are symbols + or *.
First, perform Heavy-Light Decomposition (HLD) on the tree. Note: if there is a tie in subtree size, choose the child with the smaller index as the heavy child.
The value of a node is defined as follows: if the node is a leaf, its value is the number on the node; if the node is not a leaf, then its value is the result of applying + or * (depending on the node’s symbol) to the values of all nodes on the heavy chains of all its children that are not on the same heavy chain as this node.
Another way to describe it is: let the set of children of node be , the parent of node be , and the set of nodes on the heavy chain containing node be .
Define:
$$Charge_{i}=\cup_{k\in D_{i},\ k\not\in P_{i}}P_{k}$$Let be the symbol of this node. Then the value of this node is:
$$V_{i}= \begin{cases} \sum_{j\in Charge_{i}}V_{j} &C_{i}=`+'\\ \prod_{j\in Charge_{i}}V_{j} &C_{i}=`\times' \end{cases}$$The data does not guarantee that every non-leaf node has at least one child outside its chain. If there is no valid child, ignore this node.
You need to support three operations:
-
Change the value of a leaf node.
-
Change the symbol of a non-leaf node to
+or*. -
Query, for the heavy chain containing a given node, the sum and the product of the values of all nodes on that heavy chain.
To prevent overflow, take all values modulo .
Input Format
The first line contains two integers and , meaning the number of students and the number of requests.
Then input line with integers, meaning the parent of node is .
Then input one line with integers describing each node: if the node is a leaf, it is a number representing ; otherwise it is a number, where 0 means + and 1 means *.
Then input lines. Each line contains an integer and an index , meaning the request type is and the operated node index is . If , then input another integer as the new value.
Output Format
For each operation of type , output one line containing two integers , meaning the sum and the product of the values of all nodes on the heavy chain containing this node.
8 5
1 2 3 3 2 2 7
1 0 1 3 4 5 0 6
3 1
2 2
3 1
1 4 1
3 1
18 132
37 360
35 120
Hint
For of the testdata, .
For of the testdata, .
The testdata guarantees that at any time there is no node on the tree whose value is .
Translated by ChatGPT 5
京公网安备 11011102002149号