# 2020牛客多校七 C. A National Pandemic （树链剖分）

w − d i s ( x , y ) = w − ( d e p [ x ] + d e p [ y ] − 2 ∗ d e p [ l c a ( x , y ) ] ) w-dis(x,y)=w-(dep[x]+dep[y]-2*dep[lca(x,y)])

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
//树链剖分
const int MAXN = 5e5 + 5;
int sum[MAXN << 2], add[MAXN << 2];
int head[MAXN << 1], pre[MAXN], id[MAXN];
int siz[MAXN], son[MAXN], fa[MAXN];
int dep[MAXN], top[MAXN];
ll delta[MAXN];
struct edge {
int to;
int next;
}e[MAXN << 1];
int tot, sign;
/*------------准备阶段--------------*/
void init() {
memset(id, 0, sizeof(id));
memset(siz, 0, sizeof(siz));
memset(son, 0, sizeof(son));
memset(fa, 0, sizeof(fa));
memset(dep, 0, sizeof(dep));
memset(top, 0, sizeof(top));
memset(delta, 0, sizeof(delta));
tot = sign = 0;
}
void addedge(int u, int v) {
}
/*--------------dfs-----------------*/
void dfs1(int u, int f) {    //1 0
dep[u] = dep[f] + 1;
fa[u] = f;
siz[u] = 1;
for (int i = head[u]; i != -1; i = e[i].next) {
int to = e[i].to;
if (to == f) continue;
dfs1(to, u);
siz[u] += siz[to];
if (siz[to] > siz[son[u]]) son[u] = to;
}
}
void dfs2(int u, int Top) {  //1 1
id[u] = ++sign;
pre[sign] = u;
top[u] = Top;
if (son[u]) dfs2(son[u], Top);
for (int i = head[u]; i != -1; i = e[i].next) {
int to = e[i].to;
if (to == son[u] || to == fa[u]) continue;
dfs2(to, to);
}
}
/*--------------线段树--------------*/
struct Node {
int l, r;
int mid() { return (l + r) >> 1; }
} tree[MAXN << 2];
void PushDown(int rt, int m) {
sum[rt << 1] += add[rt] * (m - (m >> 1));
sum[rt << 1 | 1] += add[rt] * (m >> 1);
}
}
void build(int l, int r, int rt) {
tree[rt].l = l;
tree[rt].r = r;
sum[rt] = 0;
if (l == r) {
sum[rt] = 0;
return;
}
int m = tree[rt].mid();
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int c, int l, int r, int rt) {
if (tree[rt].l == l && r == tree[rt].r) {
sum[rt] += c * (r - l + 1);
return;
}
if (tree[rt].l == tree[rt].r) return;
PushDown(rt, tree[rt].r - tree[rt].l + 1);
int m = tree[rt].mid();
if (r <= m) update(c, l, r, rt << 1);
else if (l > m) update(c, l, r, rt << 1 | 1);
else {
update(c, l, m, rt << 1);
update(c, m + 1, r, rt << 1 | 1);
}
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
int query(int pos, int rt) {
if (tree[rt].l == tree[rt].r) {
return sum[rt];
}
PushDown(rt, tree[rt].r - tree[rt].l + 1);
int m = tree[rt].mid();
int res = 0;
if (pos <= m) res = query(pos, rt << 1);
else res = query(pos, rt << 1 | 1);
return res;
}
ll query2(int l, int r, int rt) {
if (l == tree[rt].l && r == tree[rt].r) {
return sum[rt];
}
PushDown(rt, tree[rt].r - tree[rt].l + 1);
int m = tree[rt].mid();
ll res = 0;
if (r <= m) res += query2(l, r, rt << 1);
else if (l > m) res += query2(l, r, rt << 1 | 1);
else {
res += query2(l, m, rt << 1);
res += query2(m + 1, r, rt << 1 | 1);
}
return res;
}
/*------------树链剖分-------------*/
ll getSum(int x, int y) {  //查询x到y路径上所有结点和
ll res = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
res += query2(id[top[x]], id[x], 1);
x = fa[top[x]];
}
if (id[x] > id[y]) swap(x, y);
res += query2(id[x], id[y], 1);
return res;
}
void solve(int x, int y, int c) {  //更新x到y路径上结点值
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(c, id[top[x]], id[x], 1);
x = fa[top[x]];
}
if (id[x] > id[y]) swap(x, y);
update(c, id[x], id[y], 1);
}
ll S, num;
ll queryX(int x) {
return S - num * dep[x] + 2 * getSum(1, x);
}
int n, m, u, v, ind, x, t, w;
int main() {
scanf("%d", &t);
while (t--) {
init();
S = num = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &u, &v);
}
dfs1(1, 0);
dfs2(1, 1);
build(1, n, 1);
while (m--) {
scanf("%d%d", &ind, &x);
if (ind == 1) {
scanf("%d", &w);
S += w - dep[x];
num++;
solve(1, x, 1);
}
else if (ind == 2) {  //与0取min，就是减去自身值
ll ans = queryX(x) - delta[x];
if(ans > 0) delta[x] += ans;  //delta保存当前值
}
else {
ll ans = queryX(x) - delta[x];
printf("%lld\n", ans);
}
}
}
return 0;
}

05-24