#FT0000177. Lan truyền giá trị trên cây (Propagating Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Lan truyền giá trị trên cây (Propagating Tree)
Propagating Tree
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices numbered through and is rooted at vertex . Vertex initially holds . Process commands in order. Command 1 x val adds to vertex , subtracts from its children, adds it to grandchildren, and keeps alternating signs throughout the subtree: vertex in the subtree of receives . Command 2 x prints the current value at vertex . Updates accumulate, and values may become negative.
Input
The first line contains ; the second has initial values . The next lines describe an undirected tree. The last lines are either 1 x val or 2 x.
Output
For every type-2 command print the current value of the specified vertex on its own line. Type-1 commands produce no output.
Subtasks
- Subtask 1 (20 points): ; ; ; exactly undirected edges connecting vertices into one tree rooted at 1; every command is
1 x valwith , , or2 xwith ; at least one2. - Subtask 2 (30 points): ; ; ; exactly undirected edges connecting vertices into one tree rooted at 1; every command is
1 x valwith , , or2 xwith ; at least one2. - Subtask 3 (50 points): ; ; ; exactly undirected edges connecting vertices into one tree rooted at 1; every command is
1 x valwith , , or2 xwith ; at least one2.
Examples
Example 1
Input
5 5
1 2 1 1 2
1 2
1 3
2 4
2 5
1 2 3
1 1 2
2 1
2 2
2 4
Output
3
3
0
Explanation
1 2 3: signed changes on affected vertices: 2:+3, 5:-3, 4:-3. New values by vertex order: [1, 5, 1, -2, -1]. No output.1 1 2: signed changes on affected vertices: 1:+2, 3:-2, 2:-2, 5:+2, 4:+2. New values by vertex order: [3, 3, -1, 0, 1]. No output.2 1: current value at vertex 1 is 3.2 2: current value at vertex 2 is 3.2 4: current value at vertex 4 is 0.
Example 2
Input
4 6
1 1 1 1
1 2
2 3
3 4
1 2 5
2 2
2 3
1 1 2
2 3
2 4
Output
6
-4
-2
4
Explanation
1 2 5: signed changes on affected vertices: 2:+5, 3:-5, 4:+5. New values by vertex order: [1, 6, -4, 6]. No output.2 2: current value at vertex 2 is 6.2 3: current value at vertex 3 is -4.1 1 2: signed changes on affected vertices: 1:+2, 2:-2, 3:+2, 4:-2. New values by vertex order: [3, 4, -2, 4]. No output.2 3: current value at vertex 3 is -2.2 4: current value at vertex 4 is 4.