Hiring process8 levels
  1. 1 IQ Test
  2. 2 Additional Online Test (If Required)
  3. 3 Office Interview with Hiring Manager
  4. 4 Unpaid Test Task (Motivation Check)
  5. 5 Paid Test Task and Follow-Up Interview
  6. 6 Iterative Paid Test Tasks
  7. 7 Office Verification
  8. 8 Final Interview with the Founder

Problems > Departments and Budgets

Departments and Budgets

Difficulty: 2000 6000 ms 128 MB

fenwick trees euler-tour

A rooted tree (root 1) has a value per node. Support U v x (assign node v's value to x) and Q v (print the SUM over v's whole subtree).

Input: the first line has N and Q; the second line has the N initial values; the next N−1 lines have edges; the next Q lines are operations.

Output: one line per Q operation.

Constraints:

  • 1 ≤ N, Q ≤ 105
  • |values| ≤ 104

Sample Tests

Input 1
3 3
1 2 3
1 2
1 3
Q 1
U 2 10
Q 1
Output 1
6
14
Input 2
1 1
5
Q 1
Output 2
5

Read the Editorial Statistics

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.
Ctrl+Enter submit · Ctrl+Space autocomplete · F11 fullscreen