#P9136. [THUPC 2023 初赛] 种苹果

[THUPC 2023 初赛] 种苹果

Problem Description

A farmer planted an apple tree, and it is full of apples of all sizes. Summer is a great season for fruit trees to grow: the tree keeps growing new branches and new apples, and the existing apples keep getting bigger.

To observe and record the growth of the apples so that he can roughly estimate the future harvest profit, the farmer carried out long-term careful observation and research. At the very beginning of the whole recording period, there are nn apples on the tree. The farmer numbers them as 1n1 \sim n. There are n1n-1 branches connecting these apples. Each branch has exactly one apple at each end, so the whole apple tree forms a true tree structure. The farmer also estimates the value of each apple: the initial value of the ii-th apple is aia_i, which means the net profit if he picks it and sells it at that moment. Due to costs, aia_i may be negative.

During the recording period, there are mm events worth recording. All events are of the following types:

1 u v w1\ u\ v\ w: A new apple grows in the middle of the branch that originally connects apple uu and apple vv. Suppose there were kk apples originally; then it becomes k+1k+1. The farmer numbers the new apple as k+1k+1, and its value is ww. The original branch between uu and vv is split into two branches: one connects uu and k+1k+1, and the other connects k+1k+1 and vv.

2 u w2\ u\ w: A new branch and a new apple grow on the tree. Suppose there were kk apples originally; then it becomes k+1k+1. The farmer numbers the new apple as k+1k+1, and its value is ww. The new branch connects apple uu and k+1k+1.

3 u v w3\ u\ v\ w: The values of some apples change. The values of all apples on the whole branch segment connecting apple uu and apple vv (i.e., the shortest path between uu and vv in the tree structure, including uu and vv themselves) are increased by ww. Since the change may also be caused by lack of nutrition or pests and diseases, ww may be negative.

4 u v w4\ u\ v\ w: The farmer wants to conduct a sampling survey on the tree to study his possible profit. He defines apples with value at least ww as “high-quality apples”, and chooses the whole branch segment connecting apple uu and apple vv (same meaning as above). He wants to count how many “high-quality apples” are on this segment.

However, there are too many apples and the farmer cannot count them, so he asks you for help. Note: since the farmer cannot predict the future, you must answer the queries online.

Input Format

Line 11: Two positive integers n,mn, m.

Line 22: nn integers aia_i.

Next n1n-1 lines: Each line contains two positive integers ui,viu_i, v_i, describing all initial branches on the tree.

Next mm lines: Each line contains 33 or 44 integers, describing all events in chronological order, with the format as stated in the description.

To enforce online processing, let the answer to the previous type 44 event be lastanslastans (initially lastans=0lastans = 0). Then, in all events, the input u,v,wu, v, w must be XORed with lastanslastans to obtain the real u,v,wu, v, w.

Output Format

For each type 44 event, output one line with a non-negative integer, representing the number of “high-quality apples” in this survey.

5 6
1 3 3 2 2
1 2
1 3
2 4
2 5
4 3 4 2
3 2 6 2
4 0 7 1
1 5 6 1
2 2 7
4 0 3 0

3
4
2

Hint

Sample Explanation 1

For this sample, after removing the online XOR, the data is as follows:

5 6
1 3 3 2 2
1 2
1 3
2 4
2 5
4 3 4 2
3 1 5 1
4 3 4 2
1 1 2 5
2 6 3
4 4 7 4

Constraints

For all testdata, n,m2×105n, m \leq 2 \times 10^5, ai,w109|a_i|, |w| \leq 10^9. It is guaranteed that any apple index involved at any time is valid. It is guaranteed that the initial n1n-1 branches form a tree structure. For all type 11 events, it is guaranteed that the branch connecting apples uu and vv exists at the time the event happens.

Source

From the preliminary round of the 2023 Tsinghua University Student Programming Contest and Collegiate Invitational Contest (THUPC2023).

Resources such as solutions can be found at https://github.com/THUSAAC/THUPC2023-Pre.

Translated by ChatGPT 5