This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_E"
#include "../../template.h"
#include "../LazySegTree.h"
#include "../HeavyLight_adamant.h"
using ll = long long;
struct S {
ll sum, sz;
};
S op(S l, S r) {
return S { l.sum + r.sum, l.sz + r.sz };
}
S e() { return S {0, 0}; }
S mapping(ll f, S s) {
return S { s.sum + s.sz * f, s.sz };
}
ll composition(ll f, ll g) { return f + g; }
ll id() { return 0; }
void solve() {
int n; cin >> n;
vector<vector<int>> adj(n);
REP(i,n) {
int k; cin >> k;
while (k--) {
int j; cin >> j;
adj[i].push_back(j);
adj[j].push_back(i);
}
}
HLD hld(adj, 0);
vector<S> nodes;
for (int i = 0; i < n; i++) nodes.push_back(S{0, 1});
LazySegTree<S, op, e, ll, mapping, composition, id> st(nodes);
int q; cin >> q;
while (q--) {
int typ; cin >> typ;
if (typ == 0) {
int u, val; cin >> u >> val;
hld.apply_path(u, 0, true, [&] (int l, int r) {
st.apply(l, r + 1, val);
});
} else {
int u; cin >> u;
cout << hld.prod_path_commutative<S, op, e>(
0, u, true, [&] (int l, int r) {
return st.prod(l, r+1);
}).sum << '\n';
}
}
}
#line 1 "DataStructure/test/aizu_grl_5_e_hld_edge.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_E"
#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
out << '{';
for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
out << (it == beg ? "" : ", ") << *it;
}
return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
return print_tuple_utils<0, tuple<U...>>(out, t);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
return uniform_int_distribution<long long> (0, r-1)(rng);
}
template<typename T>
vector<T> read_vector(int n) {
vector<T> res(n);
for (int& x : res) cin >> x;
return res;
}
void solve();
int main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
#line 1 "DataStructure/LazySegTree.h"
// Lazy Segment Tree, copied from AtCoder {{{
// Source: https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp
// Doc: https://atcoder.github.io/ac-library/master/document_en/lazysegtree.html
//
// Notes:
// - Index of elements from 0
// - Range queries are [l, r-1]
// - composition(f, g) should return f(g())
//
// Tested:
// - https://oj.vnoi.info/problem/qmax2
// - https://oj.vnoi.info/problem/lites
// - (range set, add, mult, sum) https://oj.vnoi.info/problem/segtree_itmix
// - (range add (i-L)*A + B, sum) https://oj.vnoi.info/problem/segtree_itladder
// - https://atcoder.jp/contests/practice2/tasks/practice2_l
// - https://judge.yosupo.jp/problem/range_affine_range_sum
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
template<
class S, // node data type
S (*op) (S, S), // combine 2 nodes
S (*e) (), // identity element
class F, // lazy propagation tag
S (*mapping) (F, S), // apply tag F on a node
F (*composition) (F, F), // combine 2 tags
F (*id)() // identity tag
>
struct LazySegTree {
LazySegTree() : LazySegTree(0) {}
explicit LazySegTree(int n) : LazySegTree(vector<S>(n, e())) {}
explicit LazySegTree(const vector<S>& v) : _n((int) v.size()) {
log = ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
lz = std::vector<F>(size, id());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
// 0 <= p < n
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
// 0 <= p < n
S get(int p) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
// Get product in range [l, r-1]
// 0 <= l <= r <= n
// For empty segment (l == r) -> return e()
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return e();
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() {
return d[1];
}
// 0 <= p < n
void apply(int p, F f) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = mapping(f, d[p]);
for (int i = 1; i <= log; i++) update(p >> i);
}
// Apply f on all elements in range [l, r-1]
// 0 <= l <= r <= n
void apply(int l, int r, F f) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return;
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
{
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l2;
r = r2;
}
for (int i = 1; i <= log; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
// Binary search on SegTree to find largest r:
// f(op(a[l] .. a[r-1])) = true (assuming empty array is always true)
// f(op(a[l] .. a[r])) = false (assuming op(..., a[n]), which is out of bound, is always false)
template <bool (*g)(S)> int max_right(int l) {
return max_right(l, [](S x) { return g(x); });
}
template <class G> int max_right(int l, G g) {
assert(0 <= l && l <= _n);
assert(g(e()));
if (l == _n) return _n;
l += size;
for (int i = log; i >= 1; i--) push(l >> i);
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!g(op(sm, d[l]))) {
while (l < size) {
push(l);
l = (2 * l);
if (g(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
// Binary search on SegTree to find smallest l:
// f(op(a[l] .. a[r-1])) = true (assuming empty array is always true)
// f(op(a[l-1] .. a[r-1])) = false (assuming op(a[-1], ..), which is out of bound, is always false)
template <bool (*g)(S)> int min_left(int r) {
return min_left(r, [](S x) { return g(x); });
}
template <class G> int min_left(int r, G g) {
assert(0 <= r && r <= _n);
assert(g(e()));
if (r == 0) return 0;
r += size;
for (int i = log; i >= 1; i--) push((r - 1) >> i);
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!g(op(d[r], sm))) {
while (r < size) {
push(r);
r = (2 * r + 1);
if (g(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
vector<S> d;
vector<F> lz;
void update(int k) {
d[k] = op(d[2*k], d[2*k+1]);
}
void all_apply(int k, F f) {
d[k] = mapping(f, d[k]);
if (k < size) lz[k] = composition(f, lz[k]);
}
void push(int k) {
all_apply(2*k, lz[k]);
all_apply(2*k+1, lz[k]);
lz[k] = id();
}
};
// }}}
// Examples {{{
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_D
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_E
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_F
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_G
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_H
// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_I
// supports:
// - set a(l -> r) to val; val > NOT_SET
// - add a(l -> r) += val
// - find sum a(l -> r)
// - find min a(l -> r)
struct RangeSetAddMinSumOps {
struct S { long long sum, min, sz; };
static S op(S l, S r) { return S { l.sum + r.sum, min(l.min, r.min), l.sz + r.sz }; }
static S e() { return S {0LL, INT_MAX, 0}; }
static const long long NOT_SET = -1000111000;
struct F { long long set, add; };
static S mapping(F f, S s) {
if (f.set == NOT_SET) {
return S {
s.sum + f.add * s.sz,
s.min + f.add,
s.sz,
};
}
return S {
(f.set + f.add) * s.sz,
f.set + f.add,
s.sz,
};
}
static F composition(F f, F g) {
if (f.set == NOT_SET) {
return F { g.set, g.add + f.add };
}
return f;
}
static F id() {
return F { NOT_SET, 0 };
}
};
// }}}
#line 1 "DataStructure/HeavyLight_adamant.h"
// Index from 0
// Best used with SegTree.h
//
// Usage:
// HLD hld(g, root);
// // build segment tree. Note that we must use hld.order[i]
// vector<T> nodes;
// for (int i = 0; i < n; i++)
// nodes.push_back(initial_value[hld.order[i]])
// SegTree<S, op, e> st(nodes);
//
// // Update single vertex
// st.set(hld.in[u], new_value)
//
// // Update path
// hld.apply_path(from, to, is_edge, [&] (int l, int r) {
// st.apply(l, r+1, F);
// });
//
// // Query path
// hld.prod_path_commutative<S, op, e> (from, to, is_edge, [&] (int l, int r) {
// return st.prod(l, r+1);
// });
//
// Tested:
// - (vertex, path) https://judge.yosupo.jp/problem/vertex_add_path_sum
// - (vertex, path, non-commutative) https://judge.yosupo.jp/problem/vertex_set_path_composite
// - (vertex, subtree) https://judge.yosupo.jp/problem/vertex_add_subtree_sum
// - (vertex, path, non-commutative, 1-index) https://oj.vnoi.info/problem/icpc21_mt_l
// - (vertex, path) https://oj.vnoi.info/problem/qtree3
//
// - (edge, path) https://oj.vnoi.info/problem/qtreex
// - (edge, path) https://oj.vnoi.info/problem/lubenica
// - (edge, path) https://oj.vnoi.info/problem/pwalk
// - (edge, path, lazy) https://oj.vnoi.info/problem/kbuild
// - (edge, path, lazy) https://oj.vnoi.info/problem/onbridge
//
// - (lca) https://oj.vnoi.info/problem/fselect
// - (kth_parent) https://cses.fi/problemset/task/1687
// HeavyLight {{{
struct HLD {
HLD(const vector<vector<int>>& _g, int root)
: n(_g.size()), g(_g),
parent(n), depth(n), sz(n),
dfs_number(0), nxt(n), in(n), out(n), order(n)
{
assert(0 <= root && root < n);
// init parent, depth, sz
// also move most heavy child of u to g[u][0]
depth[root] = 0;
dfs_sz(root, -1);
// init nxt, in, out
nxt[root] = root;
dfs_hld(root);
}
int lca(int u, int v) const {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
while (true) {
if (in[u] > in[v]) swap(u, v); // in[u] <= in[v]
if (nxt[u] == nxt[v]) return u;
v = parent[nxt[v]];
}
}
// return k-th parent
// if no such parent -> return -1
int kth_parent(int u, int k) const {
assert(0 <= u && u < n);
if (depth[u] < k) return -1;
while (true) {
int v = nxt[u];
if (in[u] - k >= in[v]) return order[in[u] - k];
k -= in[u] - in[v] + 1;
u = parent[v];
}
}
// return k-th vertex on path from u -> v (0 <= k)
// if k > distance -> return -1
int kth_vertex_on_path(int u, int v, int k) const {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
int l = lca(u, v);
int ul = depth[u] - depth[l];
if (k <= ul) return kth_parent(u, k);
k -= ul;
int vl = depth[v] - depth[l];
if (k <= vl) return kth_parent(v, vl - k);
return -1;
}
int dist(int u, int v) const {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
int l = lca(u, v);
return depth[u] + depth[v] - 2*depth[l];
}
// apply f on vertices on path [u, v]
// edge = true -> apply on edge
//
// f(l, r) should update segment tree [l, r] INCLUSIVE
void apply_path(int u, int v, bool edge, const function<void(int, int)> &f) {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
if (u == v && edge) return;
while (true) {
if (in[u] > in[v]) swap(u, v); // in[u] <= in[v]
if (nxt[u] == nxt[v]) break;
f(in[nxt[v]], in[v]);
v = parent[nxt[v]];
}
if (u == v && edge) return;
f(in[u] + edge, in[v]);
}
// get prod of path u -> v
// edge = true -> get on edges
//
// f(l, r) should query segment tree [l, r] INCLUSIVE
// f must be commutative. For non-commutative, use getSegments below
template<class S, S (*op) (S, S), S (*e)()>
S prod_path_commutative(
int u, int v, bool edge,
const function<S(int, int)>& f) const {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
if (u == v && edge) {
return e();
}
S su = e(), sv = e();
while (true) {
if (in[u] > in[v]) { swap(u, v); swap(su, sv); }
if (nxt[u] == nxt[v]) break;
sv = op(sv, f(in[nxt[v]], in[v]));
v = parent[nxt[v]];
}
if (u == v && edge) {
return op(su, sv);
} else {
return op(su, op(sv, f(in[u] + edge, in[v])));
}
}
// f(l, r) modify seg_tree [l, r] INCLUSIVE
void apply_subtree(int u, bool edge, const function<void(int, int)>& f) {
assert(0 <= u && u < n);
f(in[u] + edge, out[u] - 1);
}
// f(l, r) queries seg_tree [l, r] INCLUSIVE
template<class S>
S prod_subtree_commutative(int u, bool edge, const function<S(S, S)>& f) {
assert(0 <= u && u < n);
return f(in[u] + edge, out[u] - 1);
}
// Useful when functions are non-commutative
// Return all segments on path from u -> v
// For this problem, the order (u -> v is different from v -> u)
vector< pair<int,int> > getSegments(int u, int v) const {
assert(0 <= u && u < n);
assert(0 <= v && v < n);
vector< pair<int,int> > upFromU, upFromV;
int fu = nxt[u], fv = nxt[v];
while (fu != fv) { // u and v are on different chains
if (depth[fu] >= depth[fv]) { // move u up
upFromU.push_back({u, fu});
u = parent[fu];
fu = nxt[u];
} else { // move v up
upFromV.push_back({fv, v});
v = parent[fv];
fv = nxt[v];
}
}
upFromU.push_back({u, v});
reverse(upFromV.begin(), upFromV.end());
upFromU.insert(upFromU.end(), upFromV.begin(), upFromV.end());
return upFromU;
}
// return true if u is ancestor
bool isAncestor(int u, int v) const {
return in[u] <= in[v] && out[v] <= out[u];
}
// private:
int n;
vector<vector<int>> g;
vector<int> parent; // par[u] = parent of u. par[root] = -1
vector<int> depth; // depth[u] = distance from root -> u
vector<int> sz; // sz[u] = size of subtree rooted at u
int dfs_number;
vector<int> nxt; // nxt[u] = vertex on heavy path of u, nearest to root
vector<int> in, out; // subtree(u) is in range [in[u], out[u]-1]
vector<int> order; // euler tour
void dfs_sz(int u, int fu) {
parent[u] = fu;
sz[u] = 1;
// remove parent from adjacency list
auto it = std::find(g[u].begin(), g[u].end(), fu);
if (it != g[u].end()) g[u].erase(it);
for (int& v : g[u]) {
depth[v] = depth[u] + 1;
dfs_sz(v, u);
sz[u] += sz[v];
if (sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
}
}
void dfs_hld(int u) {
order[dfs_number] = u;
in[u] = dfs_number++;
for (int v : g[u]) {
nxt[v] = (v == g[u][0] ? nxt[u] : v);
dfs_hld(v);
}
out[u] = dfs_number;
}
};
// }}}
#line 6 "DataStructure/test/aizu_grl_5_e_hld_edge.test.cpp"
using ll = long long;
struct S {
ll sum, sz;
};
S op(S l, S r) {
return S { l.sum + r.sum, l.sz + r.sz };
}
S e() { return S {0, 0}; }
S mapping(ll f, S s) {
return S { s.sum + s.sz * f, s.sz };
}
ll composition(ll f, ll g) { return f + g; }
ll id() { return 0; }
void solve() {
int n; cin >> n;
vector<vector<int>> adj(n);
REP(i,n) {
int k; cin >> k;
while (k--) {
int j; cin >> j;
adj[i].push_back(j);
adj[j].push_back(i);
}
}
HLD hld(adj, 0);
vector<S> nodes;
for (int i = 0; i < n; i++) nodes.push_back(S{0, 1});
LazySegTree<S, op, e, ll, mapping, composition, id> st(nodes);
int q; cin >> q;
while (q--) {
int typ; cin >> typ;
if (typ == 0) {
int u, val; cin >> u >> val;
hld.apply_path(u, 0, true, [&] (int l, int r) {
st.apply(l, r + 1, val);
});
} else {
int u; cin >> u;
cout << hld.prod_path_commutative<S, op, e>(
0, u, true, [&] (int l, int r) {
return st.prod(l, r+1);
}).sum << '\n';
}
}
}