#P4242. 树上的毒瘤

树上的毒瘤

Description

The tree has nn nodes connected by n1n-1 edges. Initially, there is one “tumor” hanging on each node, with color cic_i. Then Salamander will perform qq operations.

Sometimes Salamander will change the colors of all “tumors” on the simple path from one node to another.

For a given set of nodes on the tree, namely the point set S\bm{S}, Salamander defines the weight of a node:

Wi=jST(i,j)W_i=\sum_{j\in S}T(i,j)

where T(i,j)T(i,j) denotes the number of color segments along the path from ii to jj. For example, if the colors along the path from ii to jj are 12233121223312, the number of segments is 55.

Salamander is very interested in the state of the “tumors” on the tree, so sometimes he will specify mm nodes as the set and ask for the weights of these mm nodes.

Input Format

The first line contains two positive integers nn, qq, the number of nodes in the tree and the number of operations.

The second line contains nn space-separated positive integers cic_i, the initial color of the “tumor” on each node.

The next n1n-1 lines each contain two positive integers uu, vv, indicating there is an edge connecting uu and vv.

Then follow qq operations:

  1. If the first integer is 11, then there will be three positive integers uu, vv, yy, meaning that all “tumor” colors on the simple path from node uu to node vv are changed to yy.
  2. If the first integer is 22, then there will be one positive integer mm, the number of specified nodes on the tree. The next line contains mm distinct positive integers separated by spaces, representing the mm nodes in the current query.

Output Format

Output several lines. For each operation of type 22, output the corresponding answer.

10 10
708916891 100649777 100649777 544409200 100649777 47435517 47435517 708916891 644811607 544409200 
3 2
7 1
8 1
1 10
3 4
1 5
9 2
1 2
3 6
2 1
6 
2 6
8 10 9 3 2 4 
2 2
7 8 
2 1
5 
2 2
6 10 
2 3
6 1 4 
2 1
7 
1 9 8 100649777
1 7 9 544409200
2 4
10 9 1 2 
1 
13 17 15 11 11 15 
3 3 
1 
5 5 
7 7 7 
1 
4 4 4 4 

Hint

The input is guaranteed to be valid.

For 30%30\% of the testdata, 1n,q10001 \leq n, q \leq 1000, m5000\sum m \leq 5000.

For 60%60\% of the testdata, 1n,q200001 \leq n, q \leq 20000, m100000\sum m \leq 100000.

For 100%100\% of the testdata, 1n,q1000001 \leq n, q \leq 100000, ci,y109c_i, y \leq 10^9, m200000\sum m \leq 200000, mnm \leq n.

Translated by ChatGPT 5