#P6779. [Ynoi2009] rla1rmdq

[Ynoi2009] rla1rmdq

Problem Description

You are given a tree with nn nodes. The tree edges have weights, and there is a sequence aa of length nn.

Define the parent of node xx as fa(x)fa(x). The root rtrt satisfies fa(rt)=rtfa(rt)=rt.

Define the depth dep(x)dep(x) of node xx as the sum of all edge weights on the simple path from xx to the root.

There are mm operations:

1 l r: For all lirl \le i \le r, set ai:=fa(ai)a_i := fa(a_i).

2 l r: Query, for all lirl \le i \le r, the minimum value of dep(ai)dep(a_i).

Input Format

The first line contains three integers nn mm rtrt separated by spaces, where rtrt is the index of the root.

Then follow n1n-1 lines, each containing three integers u,v,wu,v,w separated by spaces, representing an edge between uu and vv with weight ww.

Then one line contains nn integers separated by spaces, representing the sequence.

Then follow mm lines, each containing three integers separated by spaces, representing one operation.

Output Format

For each operation of type 22, output one line with one integer representing the corresponding answer.

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

Hint

Idea: yummy, Solution: nzhtl1477&memset0, Code: nzhtl1477, Data: nzhtl1477

Constraints: For 100%100\% of the testdata, 1n,m21051\le n,m\le 2\cdot 10^5, 1ain1\le a_i\le n, and edge weights are in [0,109][0,10^9].

Translated by ChatGPT 5