#P5669. [SDOI2018] 原题识别-改

[SDOI2018] 原题识别-改

Description

There is a rooted tree with nn nodes. The root is node 11, and node ii has color aia_i. You need to handle mm queries of the following two types:

  • 1 x y1~x~y: Query how many different colors appear on the shortest path from node xx to node yy in the tree.

  • 2 a b2~a~b: Randomly choose a node xx on the path from node aa to the root, and randomly choose a node yy on the path from node bb to the root. Compute the expected value of the answer to query 1 x y1~x~y. (The paths include aa, bb, and the root.)

For query type 22, let the answer be ans\mathrm{ans}. Let cnta\mathrm{cnt_a} be the number of nodes on the path from aa to the root, and let cntb\mathrm{cnt_b} be the number of nodes on the path from bb to the root. You only need to output $\mathrm{ans}\cdot \mathrm{cnt_a}\cdot \mathrm{cnt_b}$.

Input Format

Note: The input format is different from the original problem.

Each test point contains only one test case.

The first line contains two non-negative integers nn, mm, representing the number of nodes and the number of queries.

The next line contains nn positive integers. The ii-th positive integer is aia_i, representing the color of node ii.

The next n1n-1 lines each contain two integers uu, vv, indicating that there is an edge between node uu and node vv. It is guaranteed that these edges form a tree.

The next mm lines each contain one query. The format and meaning of the queries are given in the description.

Output Format

Output mm lines. The ii-th line contains one non-negative integer, representing the answer to the ii-th query.

5 3
1 4 4 5 4
1 2
2 3
3 4
2 5
1 2 3
2 1 3
1 3 2

1
5
1
10 5
3 4 3 8 9 3 2 8 5 7
1 2
2 3
3 4
4 5
5 6
4 7
4 8
8 9
8 10
1 1 10
2 3 5
2 7 5
2 5 4
1 8 6
4
34
61
45
3

Hint

For all data, it is guaranteed that 1a1,a2,,ann1051\leq a_1, a_2, \ldots, a_n\leq n\leq 10^5, 1m2×1051\leq m\leq 2\times 10^5. It is guaranteed that the input edges form a tree.

Subtask 11 (3030 points): There are no type 22 queries.

Subtask 22 (3030 points): For every edge, v=u+1v=u+1.

Subtask 33 (4040 points): There are no additional constraints.

Translated by ChatGPT 5