Pondo Subtree Add


You are given a tree with nn vertices, rooted at vertex 11. Each vertex ii has an integer value aia_i. You must process qq queries of two types:

  • 1 v x: add xx to the value of every vertex in the subtree of vv (including vv itself)
  • 2 u: output the current value of vertex uu

Use the enter/exit Euler tour from Pondo Euler Tour. The subtree of vv occupies the contiguous range [first[v],last[v]][\mathrm{first}[v], \mathrm{last}[v]] on that tour, so a subtree add is a range add and a vertex query is a point query at first[u]\mathrm{first}[u].

The Fenwick tree below supports range add and point query. range_add(l, r, x) adds xx to every index in [l,r][l, r] (inclusive, 0-indexed). point(i) returns the value at index ii.

Code 1
class Fenwick:
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (n + 1)

    def add(self, i, delta):
        while i <= self.n:
            self.bit[i] += delta
            i += i & -i

    def prefix(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def range_add(self, left, right, delta):
        self.add(left + 1, delta)
        self.add(right + 2, -delta)

    def point(self, i):
        return self.prefix(i + 1)

Input

The first line contains two integers nn and qq.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, the initial values of the vertices.

Each of the next n1n - 1 lines contains two integers uu and vv, denoting an undirected edge between uu and vv.

Each of the next qq lines describes a query and is one of the following:

  • 1 v x: add xx to every vertex in the subtree of vv
  • 2 u: query the value of vertex uu

Vertices are numbered 11 through nn. The edges form a tree.

Output

For each query of type 2, print a line containing the current value of that vertex.

Constraints

  • 1n,q1051 \le n, q \le 10^5
  • 1u,vn1 \le u, v \le n
  • 109ai,x109-10^9 \le a_i, x \le 10^9

Example 1

Input 1
8 9
0 0 0 0 0 0 0 0
1 2
2 3
2 4
1 5
5 6
6 7
6 8
1 2 10
2 3
2 5
1 1 5
2 3
2 7
1 6 -3
2 7
2 1
Output 1
10
0
15
5
2
5
Explanation

The tree, rooted at 11, looks like this:

Code 2
      1
     / \
    2   5
   / \   \
  3   4   6
         / \
        7   8
  • Adding 1010 to the subtree of 22 changes vertices 2,3,42, 3, 4.
  • Vertex 33 is then 1010, and vertex 55 is still 00.
  • Adding 55 to the subtree of 11 changes every vertex.
  • Vertex 33 becomes 1515 and vertex 77 becomes 55.
  • Adding 3-3 to the subtree of 66 changes vertices 6,7,86, 7, 8, so vertex 77 becomes 22.
  • Vertex 11 is 55.

Example 2

Input 2
5 7
1 2 3 4 5
1 2
1 3
3 4
3 5
2 1
1 3 10
2 4
2 2
1 1 -1
2 5
2 1
Output 2
1
14
2
14
0

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.