#C576. Subtree Sum Queries
Subtree Sum Queries
Subtree Sum Queries
You are given a tree with \(n\) nodes. Each node has an initial integer value. The tree is given as a list of edges where each edge represents a parent-child relationship. You need to process a sequence of queries. Each query is in one of the following forms:
- Update Query (Type 1): Increase the value of a specified node by a given integer.
- Subtree Sum Query (Type 2): Compute the sum of values in the subtree rooted at a specified node.
For each subtree sum query, output the computed sum on a new line. Note: The node indices are 1-indexed.
inputFormat
The input is given via standard input and has the following format:
- An integer \(n\) representing the number of nodes in the tree.
- A line with \(n\) space-separated integers representing the initial values of the nodes.
- An integer \(m\) representing the number of edges.
- \(m\) lines each containing two integers \(u\) and \(v\) representing an edge from node \(u\) (parent) to node \(v\) (child).
- An integer \(q\) representing the number of queries.
- \(q\) lines where each line is either:
1 node_id value
for an update query, or2 node_id
for a subtree sum query.
outputFormat
For each subtree sum query (Type 2), output the computed sum on a new line via standard output.
## sample5
1 2 3 4 5
4
1 2
1 3
2 4
2 5
3
2 1
1 3 5
2 1
15
20
</p>