#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 apples on the tree. The farmer numbers them as . There are 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 -th apple is , which means the net profit if he picks it and sells it at that moment. Due to costs, may be negative.
During the recording period, there are events worth recording. All events are of the following types:
: A new apple grows in the middle of the branch that originally connects apple and apple . Suppose there were apples originally; then it becomes . The farmer numbers the new apple as , and its value is . The original branch between and is split into two branches: one connects and , and the other connects and .
: A new branch and a new apple grow on the tree. Suppose there were apples originally; then it becomes . The farmer numbers the new apple as , and its value is . The new branch connects apple and .
: The values of some apples change. The values of all apples on the whole branch segment connecting apple and apple (i.e., the shortest path between and in the tree structure, including and themselves) are increased by . Since the change may also be caused by lack of nutrition or pests and diseases, may be negative.
: The farmer wants to conduct a sampling survey on the tree to study his possible profit. He defines apples with value at least as “high-quality apples”, and chooses the whole branch segment connecting apple and apple (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 : Two positive integers .
Line : integers .
Next lines: Each line contains two positive integers , describing all initial branches on the tree.
Next lines: Each line contains or 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 event be (initially ). Then, in all events, the input must be XORed with to obtain the real .
Output Format
For each type 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, , . It is guaranteed that any apple index involved at any time is valid. It is guaranteed that the initial branches form a tree structure. For all type events, it is guaranteed that the branch connecting apples and 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