#SRD0000103. Cây con và cấp số cộng (Arithmetic Subtrees)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cây con và cấp số cộng (Arithmetic Subtrees)
Arithmetic Subtrees
Source: DMOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a tree with vertices numbered from to , rooted at vertex . For , its parent is where . The depth is the number of edges from root ; hence and . All vertex values are initially .
Process operations:
UPDATE u a b c: for every vertex in the subtree of with , add to its value. This is an increment, not an assignment.QUERY u a b: output the sum of current values of vertices in the subtree of satisfying .
If no vertices qualify, including when , a query returns and an update changes nothing.
Input
- The first line contains .
- The next line contains integers .
- Each of the next lines is either
UPDATE u a b corQUERY u a b.
Output
For each QUERY, output the required integer sum (which may need a 64-bit integer type) on its own line.
Subtasks
- Subtask 1 (5 points): , ; ; , .
- Subtask 2 (10 points): , ; for every (chain); , .
- Subtask 3 (10 points): , ; ; all operations satisfy , ; , .
- Subtask 4 (10 points): , ; ; all operations satisfy ; , .
- Subtask 5 (65 points): , ; ; , .
Examples
Input
5 4
0 1 1 3
UPDATE 0 1 2 397
QUERY 0 2 2
UPDATE 3 0 3 688
QUERY 1 1 3
Output
1588
2673
Explanation
The root 0 has depth 0; vertices 1, 2, 3 and 4 have depths 1, 2, 2 and 3. The first update adds to vertices at depth 1 or 2, producing . QUERY 0 2 2 adds the two depth-2 values: . UPDATE 3 0 3 688 adds 688 only to vertex 4. The last query sums the values of vertices 1, 2, 3 and 4 in the subtree of 1: .