#SRD0000114. Truy vấn trên cây (Queries on the Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Truy vấn trên cây (Queries on the Tree)
Queries on the Tree
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a directed tree with vertices numbered , rooted at vertex . Every edge points from a parent to a child, and all vertices are reachable from the root. Initially, every vertex contains coins.
The depth of a vertex is its distance from the root, measured in edges; the root has depth . The subtree of contains and all its descendants.
Process operations in order:
1 L Y: add coins to every vertex at depth exactly across the entire tree.2 X: output the total number of coins in the subtree rooted at at the current time.
Answers may be large; use 64-bit integer arithmetic.
Input
- The first line contains integers .
- Each of the next lines contains
u v, a directed edge from parent to child . - The final lines contain
1 L Yor2 X.
Vertices are 1-indexed, the root is vertex 1, and edges can appear in any order.
Output
For every operation 2 X, print the total number of coins in the subtree of on its own line.
Subtasks
-
Subtask 1 (15 points): , ; rooted directed tree at 1; , , , where is the maximum depth.
-
Subtask 2 (20 points): , ; rooted directed tree at 1; , , .
-
Subtask 3 (25 points): , ; rooted directed tree at 1; , , .
-
Subtask 4 (40 points): , ; rooted directed tree at 1; , , .
Examples
Example 1
Input
5 4
1 2
1 3
3 4
3 5
1 1 2
1 2 3
2 3
2 1
Output
8
10
Explanation
The first update adds 2 to vertices 2 and 3; the second adds 3 to vertices 4 and 5. Subtree 3 totals ; the whole tree totals .