Problem:
Range Query on a Tree

Write a program which manipulates a weighted rooted tree $T$ with the following operations:

$add(v,w)$: add $w$ to the edge which connects node $v$ and its parent

$getSum(u)$: report the sum of weights of all edges from the root to node $u$

The given tree $T$ consists of $n$ nodes and every node has a unique ID from $0$ to $n-1$ respectively where ID of the root is $0$.
Note that all weights are initialized to zero.

Input

The input is given in the following format.

$n$

$node_0$

$node_1$

$node_2$

$:$

$node_{n-1}$

$q$

$query_1$

$query_2$

$:$

$query_{q}$

The first line of the input includes an integer $n$, the number of nodes in the tree.

In the next $n$ lines,the information of node $i$ is given in the following format:

ki c1 c2 ... ck

$k_i$ is the number of children of node $i$, and $c_1$ $c_2$ ... $c_{k_i}$ are node IDs of 1st, ... $k$th child of node $i$.

In the next line, the number of queries $q$ is given. In the next $q$ lines, $i$th query is given in the following format:

0 v w

or

1 u

The first integer represents the type of queries.'0' denotes $add(v, w)$ and '1' denotes $getSum(u)$.

Constraints

All the inputs are given in integers

$ 2 \leq n \leq 100000 $

$ c_j < c_{j+1} $   $( 1 \leq j \leq k-1 )$

$ 2 \leq q \leq 200000 $

$ 1 \leq u,v \leq n-1 $

$ 1 \leq w \leq 10000 $

Output

For each $getSum$ query, print the sum in a line.

Sample Input 1

6
2 1 2
2 3 5
0
0
0
1 4
7
1 1
0 3 10
1 2
0 4 20
1 3
0 5 40
1 4

Sample Output 1

0
0
10
60

Sample Input 2

4
1 1
1 2
1 3
0
6
0 3 1000
0 2 1000
0 1 1000
1 1
1 2
1 3

Sample Output 2

1000
2000
3000

Sample Input 3

2
1 1
0
4
0 1 1
1 1
0 1 1
1 1

Sample Output 3

1
2

Solution in Ruby (judge verdict: Accepted)

class SegmentTree
  attr_accessor :disp
  def inspect
    @disp
  end
  def initialize(arr, l = 0, r = arr.size - 1)
    @l = l
    @r = r
    if l == r
      @agg = arr[l]
    else
      c = (l + r) / 2
      @left_child = self.class.new(arr, l, c)
      @right_child = self.class.new(arr, c + 1, r)
      @agg = aggregate(@left_child.agg, @right_child.agg)
    end
  end

  attr_reader :agg, :l, :r

  def update(i, &block)
    if @l == @r
      @agg = block.call(@agg)
    elsif i <= @left_child.r
      @left_child.update(i, &block)
      @agg = aggregate(@left_child.agg, @right_child.agg)
    else
      @right_child.update(i, &block)
      @agg = aggregate(@left_child.agg, @right_child.agg)
    end
  end

  def query(l = @l, r = @r)
    return @agg if l == @l && r == @r
    if r <= @left_child.r
      return @left_child.query(l, r)
    elsif @right_child.l <= l
      return @right_child.query(l, r)
    else
      aggl = @left_child.query(l, @left_child.r)
      aggr = @right_child.query(@right_child.l, r)
      return aggregate(aggl, aggr)
    end
  end

  def aggregate(x, y)
    x + y
  end
end

n = gets.to_i
tree = Hash[(0..n-1).map {|i| [i, {}] }]
tree_inv = Hash[(0..n-1).map {|i| [i, nil] }]
out_degree = Hash[(0..n-1).map {|i| [i, 0] }]
n.times do |i|
  k, *neighbours = gets.split.map(&:to_i)
  neighbours.each do |j|
    tree[i][j] = 0
    out_degree[i] += 1
    tree_inv[j] = i
  end
end

subtree_size = Hash[(0..n-1).map {|i| [i, 1] }]
queue = []
out_degree.each do |i, k|
  if k == 0
    queue << i
  end
end
until queue.empty?
  i = queue.shift
  tree[i].each_key do |j|
    subtree_size[i] += subtree_size[j]
  end
  j = tree_inv[i]
  if j
    out_degree[j] -= 1
    queue << j if out_degree[j] == 0
  end
end

queue = [0]
paths = []
position = {}
seg_to_path = {}
until queue.empty?
  i = queue.shift
  path = [i]
  loop do
    heavy = true
    tree[i].each_key.sort_by {|j| -subtree_size[j]}.each do |j|
      if heavy
        path << j
        i = j
        heavy = false
      else
        queue << j
      end
    end
    break if heavy
  end
  path.unshift tree_inv[path[0]] if tree_inv[path[0]]
  weights = path.each_cons(2).map {|i, j| tree[i][j] }
  seg = SegmentTree.new(weights)
  seg_to_path[seg] = path
  (1..path.size - 1).each do |i|
    position[path[i]] = [seg, i - 1]
  end
  seg.disp = path
  paths << path
end

q = gets.to_i
q.times do
  t, i, w = gets.split.map(&:to_i)
  if t == 1
    sum = 0
    loop do
      seg, j = position[i]
      break if !seg
      sum += seg.query(0, j)
      path = seg_to_path[seg]
      i = path[0]
    end
    p sum
  else
    seg, j = position[i]
    seg.update(j) do |x|
      x + w
    end
  end
end