#P6157. 有趣的游戏

有趣的游戏

Description

The game is played on a tree of size nn. Each node has a weight, and the weight of node ii is wiw_i.

Each time, the system provides a path. Xiao A can pick two nodes x,yx, y on this path with different weights. His score is wxmodwyw_x \bmod w_y. Then Xiao B will pick two nodes from the entire tree that Xiao A has not picked before, and the scoring method is the same as Xiao A's.

To keep the game challenging, the system sometimes increases the weight of a node.

Of course, Xiao A will try his best to maximize his score, and he wants to know what this maximum value is. At the same time, he also wants to know, when his own score is maximized, what Xiao B's maximum score is.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

The next n1n-1 lines each contain two integers a,ba, b, indicating that there is an edge between aa and bb.

The next line contains nn integers, where the ii-th number is the weight of node ii.

The next line contains an integer qq.

The next qq lines each contain three integers opt,x,yopt, x, y.

If opt=0opt=0, increase wxw_x by yy.

If opt=1opt=1, it means the system provides a path from xx to yy.

Output Format

For each query with opt=1opt=1, output one line with two integers suma,sumbsuma, sumb, representing Xiao A's maximum score, and Xiao B's maximum score under this condition.

If Xiao A cannot pick two nodes with different weights, output only one number 1-1.

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

Hint

Explanation of the samples:

First time: Xiao A chooses nodes 33 and 22, scoring 3mod4=33 \bmod 4 = 3. Xiao B chooses nodes 66 and 11, scoring 4mod5=44 \bmod 5 = 4.

Second time: Xiao A chooses nodes 22 and 11, scoring 4mod5=44 \bmod 5 = 4. Xiao B chooses nodes 77 and 66, scoring 3mod4=33 \bmod 4 = 3.

Third time: Xiao A chooses nodes 22 and 11, scoring 4mod5=44 \bmod 5 = 4. Xiao B chooses nodes 77 and 66, scoring 3mod4=33 \bmod 4 = 3.

Fourth time: the weight of node 22 becomes 55.

Fifth time: Xiao A chooses nodes 55 and 11, scoring 1mod5=11 \bmod 5 = 1. Xiao B chooses nodes 66 and 22, scoring 4mod5=44 \bmod 5 = 4.

Sixth time: the only nodes Xiao A can choose are 1,21, 2, and both have weight 55, so there is no valid choice.

This problem uses bundled testdata.

Subtasks n,qn,q Special Properties Score
Subtask1 103\leq 10^3 None 1010
Subtask2 105\leq 10^5 Tree shape, node weights are random 1515
Subtask3 At most 55 different node weights, and no modifications
Subtask4 The tree is a chain, and for node ii (i>1)(i>1) its parent is i1i-1 2525
Subtask5 None 3535

Constraints: for all data, 1n,q1051 \leq n,q \leq 10^5, 1wi1041 \leq w_i \leq 10^4, the added value is a positive integer not greater than 10310^3, and the input is a valid tree. It is guaranteed that at any time, the number of distinct values is at least 44.

Translated by ChatGPT 5