#ABC294G. [ABC294G] Distance Queries on a Tree

[ABC294G] Distance Queries on a Tree

Score : 600600 points

Problem Statement

You are given a tree TT with NN vertices. Edge ii (1iN1)(1\leq i\leq N-1) connects vertices uiu _ i and viv _ i, and has a weight of wiw _ i.

Process QQ queries in order. There are two kinds of queries as follows.

  • 1 i w : Change the weight of edge ii to ww.
  • 2 u v:Print the distance between vertex uu and vertex vv.

Here, the distance between two vertices uu and vv of a tree is the smallest total weight of edges in a path whose endpoints are uu and vv.

Constraints

  • 1N2×1051\leq N\leq 2\times10^5
  • 1ui,viN (1iN1)1\leq u _ i,v _ i\leq N\ (1\leq i\leq N-1)
  • 1wi109 (1iN1)1\leq w _ i\leq 10^9\ (1\leq i\leq N-1)
  • The given graph is a tree.
  • 1Q2×1051\leq Q\leq 2\times10^5
  • For each query of the first kind,- 1iN11\leq i\leq N-1, and
    • 1w1091\leq w\leq 10^9.
  • 1iN11\leq i\leq N-1, and
  • 1w1091\leq w\leq 10^9.
  • For each query of the second kind,- 1u,vN1\leq u,v\leq N.
  • 1u,vN1\leq u,v\leq N.
  • There is at least one query of the second kind.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

u1u _ 1 v1v _ 1 w1w _ 1

u2u _ 2 v2v _ 2 w2w _ 2

\vdots

uN1u _ {N-1} vN1v _ {N-1} wN1w _ {N-1}

QQ

query1\operatorname{query} _ 1

query2\operatorname{query} _ 2

\vdots

queryQ\operatorname{query} _ Q

Here, queryi\operatorname{query} _ i denotes the ii-th query and is in one of the following format:

11 ii ww

22 uu vv

Output

Print qq lines, where qq is the number of queries of the second kind. The jj-th line (1jq)(1\leq j\leq q) should contain the answer to the jj-th query of the second kind.

5
1 2 3
1 3 6
1 4 9
4 5 10
4
2 2 3
2 1 5
1 3 1
2 1 5
9
19
11

The initial tree is shown in the figure below.

Each query should be processed as follows.

  • The first query asks you to print the distance between vertex 22 and vertex 33. Edge 11 and edge 22, in this order, form a path between them with a total weight of 99, which is the minimum, so you should print 99.
  • The second query asks you to print the distance between vertex 11 and vertex 55. Edge 33 and edge 44, in this order, form a path between them with a total weight of 1919, which is the minimum, so you should print 1919.
  • The third query changes the weight of edge 33 to 11.
  • The fourth query asks you to print the distance between vertex 11 and vertex 55. Edge 33 and edge 44, in this order, form a path between them with a total weight of 1111, which is the minimum, so you should print 1111.
7
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
3
2 1 6
1 1 294967296
2 1 6
5000000000
4294967296

Note that the answers may not fit into 3232-bit integers.

1
1
2 1 1
0
8
1 2 105
1 3 103
2 4 105
2 5 100
5 6 101
3 7 106
3 8 100
18
2 2 8
2 3 6
1 4 108
2 3 4
2 3 5
2 5 5
2 3 1
2 4 3
1 1 107
2 3 1
2 7 6
2 3 8
2 1 5
2 7 6
2 4 7
2 1 7
2 5 3
2 8 6
308
409
313
316
0
103
313
103
525
100
215
525
421
209
318
519