#LCA0000075. Jamie và cây (Jamie and Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Jamie và cây (Jamie and Tree)
Jamie and Tree
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A tree has vertices with integer values and initially root . Process three commands in order: 1 r changes the current root to ; 2 u v x finds under the current root, then adds to every vertex in the subtree of under that root; 3 v prints the sum of values in the subtree of under the current root. Changing the root does not change stored values, but it changes subsequent subtree interpretations.
Input
First line: . Second line: values . Then edges and commands in the forms described above.
Output
For each type-3 command print one integer: the subtree sum.
Subtasks
General constraints: ; ; ; các tổng có thể cần số nguyên 64 bit.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 6
1 2 3 4 5
1 2
1 3
3 4
3 5
3 1
2 2 4 3
3 3
1 4
2 2 5 2
3 3
Output
15
21
31
Explanation
The first query returns the whole-tree sum . The subsequent update changes values in the queried subtree. After rerooting to , a subtree can become the complement of an old subtree.