#P3676. 小清新数据结构题
小清新数据结构题
Description
Long long ago, there was a tree with nodes, and each node had a weight.
There are operations. Each operation either modifies the weight of a node or, given a node, asks for the sum of squares of the subtree weight sums when this node is taken as the root.
(If this statement is not very clear, please refer to the sample explanation.)
Input Format
The first line contains two integers .
The next lines each contain two integers and , indicating there is an edge between and in the tree. It is guaranteed that no edges are duplicated.
The next line contains integers, where the -th integer is the weight of node .
The next lines each contain two or three numbers. If the first number is , then there are two more numbers and , meaning the weight of node is modified to . If the first number is , then there is one more number , meaning you should query the sum of squares of the subtree weight sums when is the root.
Output Format
For each query, output the answer.
4 5
1 2
2 3
2 4
4 3 2 1
2 2
1 1 3
2 3
1 2 4
2 4
121
140
194
Hint
Sample explanation:
This is a star graph, with edges between and .
Initially, the node weights are .
For the first query with root , the subtrees of each contain only themselves, and the subtree of contains all nodes. Therefore, the subtree weight sums of are their own weights , and the subtree weight sum of is . Thus .
Next, modify the weight of node to , so the node weights become .
For the second query with root , the subtrees of and each contain only themselves; the subtree of contains ; and the subtree of contains all nodes. The subtree weight sums are , so .
Next, modify the weight of node to , so the node weights become .
For the third query with root , the subtree weight sums of and are and , the subtree weight sum of is , and the subtree weight sum of is . Thus .
Constraints:
- For 10% of the testdata, .
- For 40% of the testdata, .
- For another 30% of the testdata, the root in every query is .
- For 100% of the testdata, , and each input node weight satisfies .
It is recommended to use fast I/O, although the official solution without fast I/O can also pass.
Translated by ChatGPT 5
京公网安备 11011102002149号