#P3676. 小清新数据结构题

    ID: 2656 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>点分治洛谷原创O2优化树链剖分,树剖Link-Cut Tree,LCT概率论,统计洛谷月赛

小清新数据结构题

Description

Long long ago, there was a tree with nn nodes, and each node had a weight.

There are qq 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 n,qn, q.

The next n1n - 1 lines each contain two integers aa and bb, indicating there is an edge between aa and bb in the tree. It is guaranteed that no edges are duplicated.

The next line contains nn integers, where the ii-th integer is the weight of node ii.

The next qq lines each contain two or three numbers. If the first number is 11, then there are two more numbers xx and yy, meaning the weight of node xx is modified to yy. If the first number is 22, then there is one more number xx, meaning you should query the sum of squares of the subtree weight sums when xx 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 22 and 1,3,41, 3, 4.

Initially, the node weights are 4,3,2,14, 3, 2, 1.

For the first query with root 22, the subtrees of 1,3,41, 3, 4 each contain only themselves, and the subtree of 22 contains all nodes. Therefore, the subtree weight sums of 1,3,41, 3, 4 are their own weights 4,2,14, 2, 1, and the subtree weight sum of 22 is 4+3+2+1=104 + 3 + 2 + 1 = 10. Thus 42+22+12+102=1214^2 + 2^2 + 1^2 + 10^2 = 121.

Next, modify the weight of node 11 to 33, so the node weights become 3,3,2,13, 3, 2, 1.

For the second query with root 33, the subtrees of 11 and 44 each contain only themselves; the subtree of 22 contains 1,2,41, 2, 4; and the subtree of 33 contains all nodes. The subtree weight sums are 3,1,7,93, 1, 7, 9, so 32+12+72+92=1403^2 + 1^2 + 7^2 + 9^2 = 140.

Next, modify the weight of node 22 to 44, so the node weights become 3,4,2,13, 4, 2, 1.

For the third query with root 44, the subtree weight sums of 11 and 33 are 33 and 22, the subtree weight sum of 22 is 3+4+2=93 + 4 + 2 = 9, and the subtree weight sum of 44 is 3+4+2+1=103 + 4 + 2 + 1 = 10. Thus 32+22+92+102=1943^2 + 2^2 + 9^2 + 10^2 = 194.

Constraints:

  • For 10% of the testdata, n,q2000n, q \leq 2000.
  • For 40% of the testdata, n,q6×104n, q \leq 6 \times 10^4.
  • For another 30% of the testdata, the root in every query is 11.
  • For 100% of the testdata, 1n,q2×1051 \leq n, q \leq 2 \times 10^5, and each input node weight satisfies 10weight10-10 \leq \text{weight} \leq 10.

It is recommended to use fast I/O, although the official solution without fast I/O can also pass.

Translated by ChatGPT 5