#P9887. Capoo on tree
Capoo on tree
Capoo on tree
Problem Description
Bugcat Capoo planted an apple tree at his doorstep, which has a total of nodes, and on the -th node, an apple of size grows. Capoo observes and records the growth of the apples every day. He discovers that: On the -th day, if it's raining, all apples of size on the chain from node to node on the tree would grow by unit; If it's cloudy, all apples of size on the chain from node to node on the tree would shrink by unit; If it's sunny, then Capoo goes out to check the quality of the apples on the tree. Capoo believes that only apples exceeding size are up to standard apples. He would climb from node to node on the tree, checking all the apples along the path. When Capoo continuously encounters qualifying apples, he gets very happy and records the distance from the starting position to the starting node , then stops checking. Otherwise, if there is no set of consecutive up-standard apples on the entire path, Capoo gets disappointed and creates a scene. However, Capoo inadvertently lost all the records of . Now, he comes to you with growth records of a total of days, hoping to find out the distance from the recorded position to the starting point that he recorded each sunny day he went out to inspect, or to confirm if such a position does not exist. Formally speaking, for an unrooted tree of size with node having a weight of , you need to support the following operations: If we represent the path from to on the tree as
$$path_T(u_i, v_i) = \{p_0, p_1, \cdots, p_k| p_0 = u, p_k = v, (p_i, p_{i + 1})\in E\} $$That is, we use to represent the -th point on the path, then operations can be expressed as:
- , for each point satisfying , update to
- , for each point satisfying , update to
- , seek the minimum that satisfies
Input
The first line contains a positive integer , indicating the number of test cases. For each of the next test cases, the format is: The first line contains two positive integers , which represent the total number of nodes on the tree and the number of operations, respectively. The next line contains integers, where the -th integer represents the weight of node . The next lines consist of two integers each, indicating an edge on the tree. The following lines represent operations, where the first integer of each line indicates the type of operation.
- If , then the next three integers represent increasing the weight of all nodes with value on the path from to by one.
- If , then the next three integers represent decreasing the weight of all nodes with value on the path from to by one.
- If , then the next four integers ) ask for the distance from node to the closest chain with a length of and node weights greater than or equal to on the path from to . It is guaranteed that .
Output
For each query, output a line with a positive integer indicating the calculated distance. If the corresponding chain does not exist, output -1.
Sample Input
2
5 5
1 2 3 4 5
1 2
1 3
2 4
2 5
3 4 3 4 2
1 4 3 1
3 4 3 4 2
2 4 3 2
3 4 3 4 2
5 5
1 1 1 1 1
1 2
2 3
3 4
4 5
1 1 5 1
2 2 3 2
1 1 5 2
2 2 4 3
3 1 5 2 2
Sample Output
-1
0
-1
3
Source
2023“钉耙编程”中国大学生算法设计超级联赛(9)