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 C++14 (judge verdict: Accepted)

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = vector<Edge>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

class Graph {
  vector<Edges> g;
  using iterator = vector<Edges>::iterator;
  using const_iterator = vector<Edges>::const_iterator;

public:
  Graph(const int &size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int &i) const { return g[i]; }
  Edges &operator[](const int &i) { return g[i]; }
  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

class HLDecomposition {
  Graph g;
  int vertexId = 0;
  vector<int> _parent; // _parent[v] := parent of v, _parent[root] = -1
  vector<int> subsize; // subsize[v] := size of subtree rooted at v
  vector<int> depth; // depth[v] := depth of v
  vector<int> next; // next[v] := next vertex of v on heavy-path, next[last] := -1
  vector<int> head; // head[v] := head vertex on heavy-path contains v
  vector<int> treeId; // treeId[v] := id of tree contains v
  vector<int> newVertexId; // newVertexId[v] := reassigned vertex id of v
  vector<int> left, right; // [left[v], right[v]) := segment of subtree

  void decompose(const int &r) {
    stack<pair<int, int>> s;
    _parent[r] = -1;
    depth[r] = 0;
    s.emplace(r, 0);
    while (s.size()) {
      int v = s.top().first;
      int &i = s.top().second;
      if (i < g[v].size()) {
        int u = g[v][i++].dst;
        if (u == _parent[v]) continue;
        _parent[u] = v;
        depth[u] = depth[v] + 1;
        s.emplace(u, 0);
      } else {
        s.pop();
        int mini = 0;
        rep(i, g[v].size()) {
          auto &e = g[v][i];
          if (e.dst == _parent[v]) swap(e, g[v].back());
          if (e.dst == _parent[v]) continue;
          subsize[v] += subsize[e.dst];
          if (subsize[e.dst] > subsize[g[v].front().dst]) swap(e, g[v].front());
        }
      }
    }
  }

  void reassign(const int &root, const int &tid) {
    stack<tuple<int, int, int>> s;
    s.emplace(root, root, 0);
    while (s.size()) {
      int v, h;
      tie(v, h, ignore) = s.top();
      int &i = get<2>(s.top());
      if (i == 0) {
        treeId[v] = tid;
        left[v] = newVertexId[v] = vertexId++;
        head[v] = h;
        if (g[v].size() && g[v][0].dst != _parent[v]) next[v] = g[v][0].dst;
      }
      if (i < g[v].size()) {
        auto &e = g[v][i++];
        if (e.dst == _parent[v]) continue;
        s.emplace(e.dst, next[v] == e.dst ? h : e.dst, 0);
      } else {
        s.pop();
        right[v] = vertexId;
      }
    }
  }

public:
  struct segment {
    int l, r; // [l, r)
    segment(const int &l, const int &r) : l(l), r(r) {}
  };

  vector<segment> vertexSegments(int u, int v) {
    vector<segment> segs;
    while (true) {
      if (newVertexId[u] > newVertexId[v]) swap(u, v);
      segs.emplace_back(max(newVertexId[u], newVertexId[head[v]]), newVertexId[v] + 1);
      if (head[u] == head[v]) break;
      v = _parent[head[v]];
    }
    return segs;
  }

  vector<segment> edgeSegments(int u, int v) {
    vector<segment> segs;
    while (true) {
      if (newVertexId[u] > newVertexId[v]) swap(u, v);
      if (head[u] == head[v]) {
        if (u != v) segs.emplace_back(newVertexId[u] + 1, newVertexId[v] + 1);
        break;
      }
      segs.emplace_back(newVertexId[head[v]], newVertexId[v] + 1);
      v = _parent[head[v]];
    }
    return segs;
  }

  int parent(const int &v) { return _parent[v]; }

  int lca(int u, int v) {
    while (true) {
      if (newVertexId[u] > newVertexId[v]) swap(u, v);
      if (head[u] == head[v]) return u;
      v = _parent[head[v]];
    }
  }

  int distance(const int &u, const int &v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }

  HLDecomposition(const Graph &g, const vector<int> &roots = {0})
      : g(g), _parent(g.size()), subsize(g.size(), 1), depth(g.size()), //
        next(g.size(), -1), head(g.size()), treeId(g.size()), newVertexId(g.size(), -1), //
        left(g.size()), right(g.size()) {
    int treeId = 0;
    for (auto &r : roots) decompose(r), reassign(r, treeId++);
  }
};

template<typename Monoid> class SegTree {
  using T = typename Monoid::value_type;

  Monoid m;
  vector<T> tree; // 1-indexed
  int size = 1;

public:
  SegTree(const int &n = 0) {
    while (size < n) size *= 2;
    tree.assign(size * 2, m.id());
  }

  SegTree(const int &n, const T &x) {
    while (size < n) size *= 2;
    tree.assign(size * 2, x);
    for (int i = size - 1; i >= 1; i--) tree[i] = m(tree[i * 2], tree[i * 2 + 1]);
  }

  template<typename InputIterator> SegTree(InputIterator first, InputIterator last) {
    int n = distance(first, last);
    while (size < n) size *= 2;
    tree.resize(size * 2, m.id());
    copy(first, last, tree.begin() + size);
    for (int i = size - 1; i >= 1; i--) tree[i] = m(tree[i * 2], tree[i * 2 + 1]);
  }

  T fold(int l, int r) { // [l, r)
    T accl = m.id(), accr = m.id();
    for (l += size, r += size; l < r; l /= 2, r /= 2) {
      if (l & 1) accl = m(accl, tree[l++]);
      if (r & 1) accr = m(tree[--r], accr);
    }
    return m(accl, accr);
  }

  void update(int i, const T &x) {
    tree[i += size] = x;
    while (i /= 2) tree[i] = m(tree[i * 2], tree[i * 2 + 1]);
  }

  const T &operator[](int i) const { return tree[i + size]; }
};

template<typename T> class RangeSum {
  struct trait {
    using value_type = T;
    T id() { return T(); }
    T operator()(const T &a, const T &b) { return a + b; }
  };
  SegTree<trait> st;

public:
  RangeSum(const int &n = 0) : st(n) {}
  RangeSum(const int &n, const T &x) : st(n, x) {}
  template<typename InputIterator> RangeSum(InputIterator first, InputIterator last) : st(first, last) {}
  void update(const int &i, const T &x) { st.update(i, x); }
  T sum(const int &l, const int &r) { return st.fold(l, r); }
  T operator[](const int &i) { return st[i]; }
};

main() {
  int n = in();
  Graph g(n);
  rep(i, n) {
    int k = in();
    while (k--) g.addEdge(i, in());
  }
  HLDecomposition hld(g);
  RangeSum<int> rs(n);
  int q = in();
  while (q--) {
    if (in()) {
      int sum = 0;
      for (auto &p : hld.edgeSegments(0, in())) sum += rs.sum(p.l, p.r);
      cout << sum << endl;
    } else {
      int v, w;
      cin >> v >> w;
      for (auto &p : hld.edgeSegments(v, hld.parent(v))) rs.update(p.l, rs[p.l] + w);
    }
  }
}