This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2667"
#include "../../DataStructure/LazySegmentTree.hpp"
#include "../../Tree/HLDecomposition.hpp"
#include "../../Template/Template.hpp"
struct ops {
using T = pll;
using F = ll;
static T op(T x, T y) { return {x.fi + y.fi, x.se + y.se}; }
static inline T e = {0, 0};
static T mapp(F f, T x) { return {x.fi, x.se + x.fi * f}; }
static F comp(F f, F g) { return f + g; }
static inline F id = 0;
};
void solve() {
LL(N, Q);
Graph gr(N);
rep(i, N - 1) {
LL(C, D);
gr.add_edge(C, D);
}
HLDecomposition hld(gr);
LazySegmentTree<ops> seg(N);
rep(i, N) { seg.set(i, {1, 0}); }
rep(_, Q) {
LL(t);
if (t == 1) {
LL(v, x);
auto sg = hld.subtree(v);
seg.apply(sg.lf, sg.ri, x);
seg.set(hld[v], {1, seg.get(hld[v]).se - x});
} else {
LL(u, v);
ll lca = hld.lca(u, v);
pll wei = seg.get(hld[lca]);
seg.set(hld[lca], ops::e);
ll ans = 0;
for (auto &[lf, ri, _] : hld.path(u, v)) {
ans += seg.prod(lf, ri).se;
}
out(ans);
seg.set(hld[lca], wei);
}
}
}
int main() { solve(); }
#line 1 "Verify/verify-aoj/2667.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2667"
#line 2 "DataStructure/LazySegmentTree.hpp"
#include <bit>
#include <cstdint>
#include <queue>
#include <stack>
#include <vector>
template <class M>
struct LazySegmentTree {
using T = typename M::T;
using F = typename M::F;
int32_t siz;
std::vector<T> tree;
std::vector<F> del;
LazySegmentTree(int32_t sz) {
siz = sz;
tree = std::vector<T>(siz << 1, M::e);
del = std::vector<F>(siz << 1, M::id);
}
LazySegmentTree(std::vector<T> def) {
siz = def.size();
tree = std::vector<T>(siz << 1, M::e);
del = std::vector<F>(siz << 1, M::id);
for (int32_t i = 0; i < siz; i++) {
tree[i + siz] = def[i];
}
for (int32_t i = siz - 1; i > 0; i--) {
tree[i] = M::op(tree[i << 1], tree[(i << 1) + 1]);
}
}
inline T _get(int32_t pos) { return tree[pos]; }
void _calc(int32_t p) {
p >>= 1;
while (p > 0) {
tree[p] = M::op(_get(p << 1), _get((p << 1) + 1));
p >>= 1;
}
}
inline void _del_segment(int32_t p) {
tree[p << 1] = M::mapp(del[p], tree[p << 1]);
del[p << 1] = M::comp(del[p], del[p << 1]);
tree[(p << 1) + 1] = M::mapp(del[p], tree[(p << 1) + 1]);
del[(p << 1) + 1] = M::comp(del[p], del[(p << 1) + 1]);
del[p] = M::id;
}
void _delay(int32_t p) {
int32_t length = 32 - std::countl_zero((uint32_t)p);
for (int32_t i = length - 1; i >= 1; i--) {
_del_segment(p >> i);
}
}
void set(int32_t p, T v) {
p += siz;
_delay(p);
tree[p] = v;
del[p] = M::id;
_calc(p);
}
T get(int32_t p) {
_delay(p + siz);
return _get(p + siz);
}
void apply(int32_t lf, int32_t ri, F f) {
lf += siz;
ri += siz;
int32_t dl = lf >> (std::countr_zero((uint32_t)lf));
int32_t dr = ri >> (std::countr_zero((uint32_t)ri));
_delay(dl);
_delay(dr - 1);
while (lf < ri) {
if (lf & 1) {
tree[lf] = M::mapp(f, tree[lf]);
del[lf] = M::comp(f, del[lf]);
lf++;
}
if (ri & 1) {
ri--;
tree[ri] = M::mapp(f, tree[ri]);
del[ri] = M::comp(f, del[ri]);
}
lf >>= 1;
ri >>= 1;
}
_calc(dl);
_calc(dr - 1);
}
T prod(int32_t lf, int32_t ri) {
lf += siz;
ri += siz;
int32_t dl = lf >> (std::countr_zero((uint32_t)lf));
int32_t dr = ri >> (std::countr_zero((uint32_t)ri));
_delay(dl);
_delay(dr - 1);
T rel = M::e;
T rer = M::e;
while (lf < ri) {
if (lf & 1) {
rel = M::op(rel, _get(lf));
lf++;
}
if (ri & 1) {
ri--;
rer = M::op(_get(ri), rer);
}
lf >>= 1;
ri >>= 1;
}
return M::op(rel, rer);
}
template <class F>
int32_t max_right(int32_t lf, F f) {
lf += siz;
int32_t ri = siz << 1;
int32_t dl = lf >> (std::countr_zero((uint32_t)lf));
int32_t dr = ri >> (std::countr_zero((uint32_t)ri));
_delay(dl);
_delay(dr - 1);
std::queue<int32_t> lfp;
std::stack<int32_t> rip;
while (lf < ri) {
if (lf & 1) {
lfp.push(lf);
lf++;
}
if (ri & 1) {
ri--;
rip.push(ri);
}
lf >>= 1;
ri >>= 1;
}
T val = M::e;
while (!lfp.empty()) {
int32_t i = lfp.front();
lfp.pop();
if (!f(M::op(val, _get(i)))) {
while (i < siz) {
_del_segment(i);
i <<= 1;
if (f(M::op(val, _get(i)))) {
val = M::op(val, _get(i));
i++;
}
}
return i - siz;
}
val = M::op(val, _get(i));
}
while (!rip.empty()) {
int32_t i = rip.top();
rip.pop();
if (!f(M::op(val, _get(i)))) {
while (i < siz) {
_del_segment(i);
i <<= 1;
if (f(M::op(val, _get(i)))) {
val = M::op(val, _get(i));
i++;
}
}
return i - siz;
}
val = M::op(val, _get(i));
}
return siz;
}
template <class F>
int32_t min_left(int32_t ri, F f) {
ri += siz;
int32_t lf = siz;
int32_t dl = lf >> (std::countr_zero((uint32_t)lf));
int32_t dr = ri >> (std::countr_zero((uint32_t)ri));
_delay(dl);
_delay(dr - 1);
std::queue<int32_t> rip;
std::stack<int32_t> lfp;
while (lf < ri) {
if (lf & 1) {
lfp.push(lf);
lf++;
}
if (ri & 1) {
ri--;
rip.push(ri);
}
lf >>= 1;
ri >>= 1;
}
T val = M::e;
while (!rip.empty()) {
int32_t i = rip.front();
rip.pop();
if (!f(M::op(val, _get(i)))) {
while (i < siz) {
_del_segment(i);
i <<= 1;
i++;
if (f(M::op(_get(i), val))) {
val = M::op(_get(i), val);
i--;
}
}
return i - siz + 1;
}
val = M::op(_get(i), val);
}
while (!lfp.empty()) {
int32_t i = lfp.top();
lfp.pop();
if (!f(M::op(val, _get(i)))) {
while (i < siz) {
_del_segment(i);
i <<= 1;
i++;
if (f(M::op(_get(i), val))) {
val = M::op(_get(i), val);
i--;
}
}
return i - siz + 1;
}
val = M::op(_get(i), val);
}
return 0;
}
int32_t size() { return siz; }
};
#line 5 "Tree/HLDecomposition.hpp"
#line 4 "Graph/Graph.hpp"
template <class T = int32_t>
struct Edge {
int32_t from, to;
T cost;
int32_t idx;
Edge() = default;
Edge(int32_t from, int32_t to, T cost = 1, int32_t idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int32_t() { return to; }
void reverse() { std::swap(from, to); }
};
template <class T = int32_t>
struct Graph {
std::vector<std::vector<Edge<T>>> gr;
int32_t eds = 0;
Graph() = default;
Graph(int32_t n) { gr.resize(n); }
void add_edge(int32_t from, int32_t to, T cost = 1, bool directed = false) {
gr[from].emplace_back(from, to, cost, eds);
if (!directed) {
gr[to].emplace_back(to, from, cost, eds);
}
eds++;
}
void add_directed_edge(int32_t from, int32_t to, T cost = 1) {
gr[from].emplace_back(from, to, cost, eds);
eds++;
}
inline std::vector<Edge<T>> &operator[](const int32_t &p) { return gr[p]; }
int32_t size() { return gr.size(); }
};
template <class T>
Graph<T> reverse_edges(Graph<T> &gr) {
Graph<T> ret(gr.size());
for (int32_t i = 0; i < gr.size(); i++) {
for (Edge<T> j : gr[i]) {
ret[j].emplace_back(j);
ret[j].back().reverse();
}
}
return ret;
}
#line 7 "Tree/HLDecomposition.hpp"
struct HLDecomposition {
struct Segment {
int32_t lf, ri;
bool rev;
};
int32_t sz;
std::vector<int32_t> tree_sz;
std::vector<int32_t> depth;
std::vector<int32_t> order;
std::vector<int32_t> path_roots;
std::vector<int32_t> parent;
std::vector<int32_t> out;
template <class T>
void _build(int32_t pos, Graph<T> &tree) {
order[pos] = sz;
sz++;
int32_t mx = -1, mp = -1;
for (int32_t i : tree[pos]) {
if (i == parent[pos]) continue;
if (mx < tree_sz[i]) {
mx = tree_sz[i];
mp = i;
}
}
if (mx == -1) {
out[pos] = sz;
return;
}
path_roots[mp] = path_roots[pos];
_build(mp, tree);
for (int32_t i : tree[pos]) {
if (i == parent[pos]) continue;
if (i == mp) continue;
path_roots[i] = i;
_build(i, tree);
}
out[pos] = sz;
}
template <class T>
int32_t _calc_sz(int32_t pos, Graph<T> &tree) {
if (tree_sz[pos] != -1) return tree_sz[pos];
tree_sz[pos] = 1;
for (int32_t i : tree[pos]) {
if (parent[pos] != i) {
parent[i] = pos;
depth[i] = depth[pos] + 1;
tree_sz[pos] += _calc_sz(i, tree);
}
}
return tree_sz[pos];
}
template <class T>
HLDecomposition(Graph<T> &tree, int32_t root = 0) {
sz = tree.size();
tree_sz.resize(sz, -1);
depth.resize(sz, -1);
parent.resize(sz, -1);
depth[root] = 0;
_calc_sz(root, tree);
order.resize(sz, -1);
out.resize(sz, -1);
path_roots.resize(sz, -1);
sz = 0;
path_roots[root] = root;
_build(root, tree);
}
int32_t operator[](int32_t p) { return order[p]; }
Segment subtree(int32_t pos) { return {order[pos], out[pos], false}; }
std::vector<Segment> path(int32_t s, int32_t t) {
std::vector<Segment> ret;
std::stack<Segment> right;
while (path_roots[s] != path_roots[t]) {
if (depth[path_roots[s]] > depth[path_roots[t]]) {
ret.emplace_back(
Segment{order[path_roots[s]], order[s] + 1, true});
s = parent[path_roots[s]];
} else {
right.push({order[path_roots[t]], order[t] + 1, false});
t = parent[path_roots[t]];
}
}
if (depth[s] < depth[t]) {
ret.emplace_back(Segment{order[s], order[t] + 1, false});
} else {
ret.emplace_back(Segment{order[t], order[s] + 1, true});
}
while (!right.empty()) {
ret.push_back(right.top());
right.pop();
}
return ret;
}
int32_t lca(int32_t s, int32_t t) {
while (path_roots[s] != path_roots[t]) {
if (depth[path_roots[s]] > depth[path_roots[t]]) {
s = parent[path_roots[s]];
} else {
t = parent[path_roots[t]];
}
}
if (depth[s] < depth[t]) return s;
return t;
}
};
#line 2 "Template/Template.hpp"
#include <bits/stdc++.h>
using namespace std;
#line 8 "Template/InOut.hpp"
inline void scan() {}
inline void scan(int32_t &a) { std::cin >> a; }
inline void scan(uint32_t &a) { std::cin >> a; }
inline void scan(int64_t &a) { std::cin >> a; }
inline void scan(uint64_t &a) { std::cin >> a; }
inline void scan(char &a) { std::cin >> a; }
inline void scan(float &a) { std::cin >> a; }
inline void scan(double &a) { std::cin >> a; }
inline void scan(long double &a) { std::cin >> a; }
inline void scan(std::vector<bool> &vec) {
for (int32_t i = 0; i < vec.size(); i++) {
int a;
scan(a);
vec[i] = a;
}
}
inline void scan(std::string &a) { std::cin >> a; }
template <class T>
inline void scan(std::vector<T> &vec);
template <class T, size_t size>
inline void scan(std::array<T, size> &vec);
template <class T, class L>
inline void scan(std::pair<T, L> &p);
template <class T, size_t size>
inline void scan(T (&vec)[size]);
template <class T>
inline void scan(std::vector<T> &vec) {
for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(std::deque<T> &vec) {
for (auto &i : vec) scan(i);
}
template <class T, size_t size>
inline void scan(std::array<T, size> &vec) {
for (auto &i : vec) scan(i);
}
template <class T, class L>
inline void scan(std::pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
template <class T, size_t size>
inline void scan(T (&vec)[size]) {
for (auto &i : vec) scan(i);
}
template <class T>
inline void scan(T &a) {
std::cin >> a;
}
inline void in() {}
template <class Head, class... Tail>
inline void in(Head &head, Tail &...tail) {
scan(head);
in(tail...);
}
inline void print() { std::cout << ' '; }
inline void print(const bool &a) { std::cout << a; }
inline void print(const int32_t &a) { std::cout << a; }
inline void print(const uint32_t &a) { std::cout << a; }
inline void print(const int64_t &a) { std::cout << a; }
inline void print(const uint64_t &a) { std::cout << a; }
inline void print(const char &a) { std::cout << a; }
inline void print(const char a[]) { std::cout << a; }
inline void print(const float &a) { std::cout << a; }
inline void print(const double &a) { std::cout << a; }
inline void print(const long double &a) { std::cout << a; }
inline void print(const std::string &a) {
for (auto &&i : a) print(i);
}
template <class T>
inline void print(const std::vector<T> &vec);
template <class T, size_t size>
inline void print(const std::array<T, size> &vec);
template <class T, class L>
inline void print(const std::pair<T, L> &p);
template <class T, size_t size>
inline void print(const T (&vec)[size]);
template <class T>
inline void print(const std::vector<T> &vec) {
if (vec.empty()) return;
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
std::cout << ' ';
print(*i);
}
}
template <class T>
inline void print(const std::deque<T> &vec) {
if (vec.empty()) return;
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
std::cout << ' ';
print(*i);
}
}
template <class T, size_t size>
inline void print(const std::array<T, size> &vec) {
print(vec[0]);
for (auto i = vec.begin(); ++i != vec.end();) {
std::cout << ' ';
print(*i);
}
}
template <class T, class L>
inline void print(const std::pair<T, L> &p) {
print(p.first);
std::cout << ' ';
print(p.second);
}
template <class T, size_t size>
inline void print(const T (&vec)[size]) {
print(vec[0]);
for (auto i = vec; ++i != end(vec);) {
std::cout << ' ';
print(*i);
}
}
template <class T>
inline void print(const T &a) {
std::cout << a;
}
inline void out() { std::cout << '\n'; }
template <class T>
inline void out(const T &t) {
print(t);
std::cout << '\n';
}
template <class Head, class... Tail>
inline void out(const Head &head, const Tail &...tail) {
print(head);
std::cout << ' ';
out(tail...);
}
inline void Yes(bool i = true) { out(i ? "Yes" : "No"); }
inline void No(bool i = true) { out(i ? "No" : "Yes"); }
inline void Takahashi(bool i = true) { out(i ? "Takahashi" : "Aoki"); }
inline void Aoki(bool i = true) { out(i ? "Aoki" : "Takahashi"); }
inline void Alice(bool i = true) { out(i ? "Alice" : "Bob"); }
inline void Bob(bool i = true) { out(i ? "Bob" : "Alice"); }
inline void First(bool i = true) { out(i ? "First" : "Second"); }
inline void Second(bool i = true) { out(i ? "Second" : "First"); }
inline void Possible(bool i = true) { out(i ? "Possible" : "Impossible"); }
inline void Impossible(bool i = true) { out(i ? "Impossible" : "Possible"); }
inline void fls() { std::flush(std::cout); }
struct IOsetup {
IOsetup() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(16);
}
} iosetup;
#line 9 "Template/Util.hpp"
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using uint = uint32_t;
using pll = std::pair<ll, ll>;
using pii = std::pair<int32_t, int32_t>;
using vl = std::vector<ll>;
using vvl = std::vector<std::vector<ll>>;
using pdd = std::pair<ld, ld>;
using tuplis = std::array<ll, 3>;
template <class T>
using pq = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr ll LINF = (1LL << 62) - (1LL << 31);
constexpr int32_t INF = INT_MAX >> 1;
constexpr ll MINF = 1LL << 40;
constexpr ld DINF = std::numeric_limits<ld>::infinity();
constexpr int32_t MODD = 1000000007;
constexpr int32_t MOD = 998244353;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.1415926535897932;
const ll four[] = {0, 1, 0, -1, 0};
const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
template <class T>
ll sum(const T &a) {
return accumulate(std::begin(a), std::end(a), 0LL);
}
template <class T>
ld dsum(const T &a) {
return accumulate(std::begin(a), std::end(a), 0.0L);
}
template <class T>
auto min(const T &a) {
return *min_element(std::begin(a), std::end(a));
}
template <class T>
auto max(const T &a) {
return *max_element(std::begin(a), std::end(a));
}
#line 1 "Template/Macro.hpp"
#define _overload3(_1, _2, _3, name, ...) name
#define _overload4(_1, _2, _3, _4, name, ...) name
#define _rep1(i, n) for (int64_t i = 0; i < (n); i++)
#define _rep2(i, a, b) for (int64_t i = (a); i < (b); i++)
#define _rep3(i, a, b, c) for (int64_t i = (a); i < (b); i += (c))
#define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define _rrep1(i, n) for (int64_t i = (n) - 1; i >= 0; i--)
#define _rrep2(i, a, b) for (int64_t i = (b) - 1; i >= (a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep2, _rrep1)(__VA_ARGS__)
#define each(i, ...) for (auto&& i : __VA_ARGS__)
#define all(i) std::begin(i), std::end(i)
#define rall(i) std::rbegin(i), std::rend(i)
#define len(x) ((int64_t)(x).size())
#define fi first
#define se second
#define uniq(x) x.erase(unique(all(x)), std::end(x))
#define vec(type, name, ...) vector<type> name(__VA_ARGS__);
#define vv(type, name, h, ...) std::vector<std::vector<type>> name(h, std::vector<type>(__VA_ARGS__));
#define INT(...) int32_t __VA_ARGS__; in(__VA_ARGS__)
#define LL(...) int64_t __VA_ARGS__; in(__VA_ARGS__)
#define ULL(...) uint64_t __VA_ARGS__; in(__VA_ARGS__)
#define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; in(__VA_ARGS__)
#define LD(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define VEC(type, name, size) std::vector<type> name(size); in(name)
#define VV(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w)); in(name)
#line 5 "Verify/verify-aoj/2667.test.cpp"
struct ops {
using T = pll;
using F = ll;
static T op(T x, T y) { return {x.fi + y.fi, x.se + y.se}; }
static inline T e = {0, 0};
static T mapp(F f, T x) { return {x.fi, x.se + x.fi * f}; }
static F comp(F f, F g) { return f + g; }
static inline F id = 0;
};
void solve() {
LL(N, Q);
Graph gr(N);
rep(i, N - 1) {
LL(C, D);
gr.add_edge(C, D);
}
HLDecomposition hld(gr);
LazySegmentTree<ops> seg(N);
rep(i, N) { seg.set(i, {1, 0}); }
rep(_, Q) {
LL(t);
if (t == 1) {
LL(v, x);
auto sg = hld.subtree(v);
seg.apply(sg.lf, sg.ri, x);
seg.set(hld[v], {1, seg.get(hld[v]).se - x});
} else {
LL(u, v);
ll lca = hld.lca(u, v);
pll wei = seg.get(hld[lca]);
seg.set(hld[lca], ops::e);
ll ans = 0;
for (auto &[lf, ri, _] : hld.path(u, v)) {
ans += seg.prod(lf, ri).se;
}
out(ans);
seg.set(hld[lca], wei);
}
}
}
int main() { solve(); }