PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000187. So sánh tổng trọng số hai phía của cạnh (Compare Tree Weights)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

So sánh tổng trọng số hai phía của cạnh (Compare Tree Weights)

Compare Tree Weights

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A tree has NN vertices and its edges are indexed from 11 to N−1N-1 in input order. Initially every vertex has weight 1. Process QQ queries: 1 x w increases vertex xx's weight by ww; 2 y asks for the absolute difference between total vertex weights in the two components that would result from removing edge yy. A type-2 query does not actually delete the edge: future queries use the same original tree. Weight increments accumulate and answers may exceed 32-bit range.

Input

The first line contains NN, followed by N−1N-1 edges U_i V_i in edge-index order. Next is QQ, followed by QQ commands 1 x w or 2 y.

Output

For each type-2 query print ∣S1−S2∣|S_1-S_2| on a separate line. Type-1 commands produce no output.

Subtasks

  • Subtask 1 (20 points): 2≤N≤352\le N\le35; 1≤Q≤601\le Q\le60; exactly N−1N-1 numbered undirected edges form a tree; each command is 1 x w (1≤x≤N1\le x\le N, 1≤w≤10001\le w\le1000) or 2 y (1≤y<N1\le y<N); at least one type-2 query.
  • Subtask 2 (30 points): 2≤N≤50002\le N\le5000; 1≤Q≤50001\le Q\le5000; exactly N−1N-1 numbered undirected edges form a tree; each command is 1 x w (1≤x≤N1\le x\le N, 1≤w≤10001\le w\le1000) or 2 y (1≤y<N1\le y<N); at least one type-2 query.
  • Subtask 3 (50 points): 2≤N≤3000002\le N\le300000; 1≤Q≤3000001\le Q\le300000; exactly N−1N-1 numbered undirected edges form a tree; each command is 1 x w (1≤x≤N1\le x\le N, 1≤w≤10001\le w\le1000) or 2 y (1≤y<N1\le y<N); at least one type-2 query.

Examples

Example 1

Input

6
1 2
1 3
2 4
4 5
4 6
5
2 1
1 1 3
2 1
1 4 10
2 5

Output

2
1
17

Explanation

  1. 2 1: hypothetical edge removal yields component weights 2 and 4; print ∣2−4∣=2|2-4|=2; keep the edge.
  2. 1 1 3: vertex 1 now weighs 4; total weight 9.
  3. 2 1: hypothetical edge removal yields component weights 5 and 4; print ∣5−4∣=1|5-4|=1; keep the edge.
  4. 1 4 10: vertex 4 now weighs 11; total weight 19.
  5. 2 5: hypothetical edge removal yields component weights 18 and 1; print ∣18−1∣=17|18-1|=17; keep the edge.

Example 2

Input

2
1 2
5
2 1
1 1 5
2 1
1 2 3
2 1

Output

0
5
2

Explanation

  1. 2 1: hypothetical edge removal yields component weights 1 and 1; print ∣1−1∣=0|1-1|=0; keep the edge.
  2. 1 1 5: vertex 1 now weighs 6; total weight 7.
  3. 2 1: hypothetical edge removal yields component weights 6 and 1; print ∣6−1∣=5|6-1|=5; keep the edge.
  4. 1 2 3: vertex 2 now weighs 4; total weight 10.
  5. 2 1: hypothetical edge removal yields component weights 6 and 4; print ∣6−4∣=2|6-4|=2; keep the edge.