9126: Capoo on tree

内存限制:512 MB 时间限制:20 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:0

题目描述

    Bugcat Capoo planted an apple tree at his doorstep, which has a total of n nodes, and on the i-th node, an apple of size ai grows. Capoo observes and records the growth of the apples every day. He discovers that:
    On the i-th day, if it's raining, all apples of size xi on the chain from node ui to node vi on the tree would grow by 1 unit;
    If it's cloudy, all apples of size xi on the chain from node ui to node vi on the tree would shrink by 1 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 yi are up to standard apples. He would climb from node ui to node vi on the tree, checking all the apples along the path. When Capoo continuously encounters li qualifying apples, he gets very happy and records the distance from the starting position pi to the starting node ui,then stops checking. Otherwise, if there is no set of li consecutive up-standard apples on the entire path,Capoo gets disappointed and creates a scene.
    However, Capoo inadvertently lost all the records of pi. Now, he comes to you with growth records of a total of m days, hoping to find out the distance disi from the recorded position pi 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 (V, E) of size n with node i having a weight of ai, you need to support the following operations:
    If we represent the path from ui to vi on the tree as
                        pathT (ui, vi) = {p0, p1, · · · , pk|p0 = u, pk = v,(pi, pi+1) ∈ E}
    That is, we use pi to represent the i-th point on the path, then operations can be expressed as:
    • 1 ui vi xi, for each point pj satisfying apj = xi, update apj to apj + 1
    • 2 ui vi xi, for each point pj satisfying apj = xi, update apj to apj − 1
    • 3 ui vi li yi, seek the minimum j that satisfies ∀k ∈ [0, li), apj+k ≥ yi

输入格式

    The first line contains a positive integer T(1 ≤ T ≤ 100), indicating the number of test cases.
    For each of the next T test cases, the format is:
    The first line contains two positive integers n(2 ≤ n ≤ 105), m(1 ≤ m ≤ 105), which represent the total number of nodes on the tree and the number of operations, respectively.
    The next line contains n integers, where the i-th integer ai (1 ≤ ai ≤ n) represents the weight of node i.
    The next n−1 lines consist of two integers ui, vi(1 ≤ ui, vi ≤ n) each, indicating an edge on the tree.
    The following m lines represent m operations, where the first integer op(1 ≤ op ≤ 3) of each line indicates the type of operation.
    • If op = 1, then the next three integers ui, vi, xi(1 ≤ ui, vi ≤ n, 1 ≤ xi < n) represent increasing the weight of all nodes with value xi on the path from ui to vi by one.
    • If op = 2, then the next three integers ui, vi, xi(1 ≤ ui, vi ≤ n, 1 < xi ≤ n) represent decreasing the weight of all nodes with value xi on the path from ui to vi by one.
    • If op = 3, then the next four integers ui, vi, li, yi(1 ≤ ui, vi, li, yi ≤ n) ask for the distance from node ui to the closest chain with a length of li and node weights greater than or equal to yi on the path from ui to vi.
    It is guaranteed that ∑n ≤ 106,∑m ≤ 106.

输出格式

    For each query, output a line with a positive integer dis indicating the calculated distance. If the corresponding chain does not exist, output -1.

输入样例 复制

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

输出样例 复制

-1
0
-1
3

分类标签