#C576. Subtree Sum Queries

    ID: 49444 Type: Default 1000ms 256MiB

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:

  1. An integer \(n\) representing the number of nodes in the tree.
  2. A line with \(n\) space-separated integers representing the initial values of the nodes.
  3. An integer \(m\) representing the number of edges.
  4. \(m\) lines each containing two integers \(u\) and \(v\) representing an edge from node \(u\) (parent) to node \(v\) (child).
  5. An integer \(q\) representing the number of queries.
  6. \(q\) lines where each line is either:
    • 1 node_id value for an update query, or
    • 2 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.

## sample
5
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>