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

#LCA0000086. Cộng giá trị qua một phía của cạnh (Through Path)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cộng giá trị qua một phía của cạnh (Through Path)

Through Path

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

An nn-vertex tree has edges numbered by input order; edge ii joins ai,bia_i,b_i. All vertex values start at zero. In operation (t,e,x)(t,e,x), consider the two components obtained by temporarily deleting edge ee. Add xx to every vertex in the component containing aea_e when t=1t=1, otherwise the component containing beb_e when t=2t=2. All updates accumulate; no edge is permanently removed.

Input

First line: nn, followed by n−1n-1 edge lines ai,bia_i,b_i; then qq and qq lines t,e,xt,e,x.

Output

Print nn lines, with the final value at vertex vv on line vv.

Subtasks

  • Subtask 1 (20 points): 2≤n≤352\le n\le 35; 1≤q≤351\le q\le 35; 1≤ai,bi≤n1\le a_i,b_i\le n; 1≤e≤n−11\le e\le n-1; t∈{1,2}t\in\{1,2\}; 1≤x≤1091\le x\le10^9; edges form a tree.

  • Subtask 2 (30 points): 2≤n≤12002\le n\le 1200; 1≤q≤12001\le q\le 1200; 1≤ai,bi≤n1\le a_i,b_i\le n; 1≤e≤n−11\le e\le n-1; t∈{1,2}t\in\{1,2\}; 1≤x≤1091\le x\le10^9; edges form a tree.

  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤q≤2000001\le q\le 200000; 1≤ai,bi≤n1\le a_i,b_i\le n; 1≤e≤n−11\le e\le n-1; t∈{1,2}t\in\{1,2\}; 1≤x≤1091\le x\le10^9; edges form a tree.

Examples

Input

5
1 2
2 3
2 4
4 5
4
1 1 1
1 4 10
2 1 100
2 2 1000

Output

11
110
1110
110
100

Explanation

Operation 1 adds 11 to vertex 11; operation 2 adds 1010 to vertices 1,2,3,41,2,3,4; the final two operations add 100100 to the component of vertex 22 and 10001000 to vertex 33.