做法:
树剖。线段树维护区间和,打lazy标记。
其中子树加就是在dfs序上in[x]到out[x]一段加。
/*************************************************************
Problem: bzoj 4034 [HAOI2015]树上操作
User: fengyuan
Language: C++
Result: Accepted
Time: 2344 ms
Memory: 13928 kb
Submit_Time: 2017-12-05 20:28:12
*************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<map>
#include<queue>
#include<string>
#define rep(i, x, y) for (int i = (x); i <= (y); i ++)
#define down(i, x, y) for (int i = (x); i >= (y); i --)
#define mid ((l+r)/2)
#define lc (o<<1)
#define rc (o<<1|1)
#define pb push_back
#define mp make_pair
#define PII pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
using namespace std;
typedef long long LL;
//head
const int N = 100010;
int n, q, cnt, clk;
int a[N], b[N], head[N], depth[N], fa[N], son[N], sz[N], top[N], in[N], out[N];
LL tag[N<<2], sum[N<<2];
struct Edge{
int to, nex;
}e[N<<1];
inline void add(int x, int y)
{
e[++ cnt].to = y;
e[cnt].nex = head[x];
head[x] = cnt;
}
inline void dfs(int u, int last, int s)
{
depth[u] = s; fa[u] = last; sz[u] = 1;
for (int i = head[u]; i; i = e[i].nex){
int v = e[i].to; if (v == last) continue;
dfs(v, u, s+1); sz[u] += sz[v];
if (!son[u] || sz[v] > sz[son[u]]) son[u] = v;
}
}
inline void dfs2(int u, int t)
{
top[u] = t; in[u] = ++ clk; b[clk] = a[u];
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; i; i = e[i].nex){
int v = e[i].to; if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
} out[u] = clk;
}
inline void pushup(int o)
{
sum[o] = sum[lc] + sum[rc];
}
inline void build(int o, int l, int r)
{
if (l == r){
sum[o] = b[l]; return;
}
build(lc, l, mid); build(rc, mid+1, r);
pushup(o);
}
inline void pushdown(int o, int l, int r)
{
if (tag[o] == 0) return;
tag[lc] += tag[o]; tag[rc] += tag[o];
sum[lc] += tag[o]*(mid-l+1);
sum[rc] += tag[o]*(r-mid);
tag[o] = 0;
}
inline void point(int o, int l, int r, int x, int w)
{
if (l == x && r == x){
sum[o] += w; return;
}
pushdown(o, l, r);
if (x <= mid) point(lc, l, mid, x, w);
else point(rc, mid+1, r, x, w);
pushup(o);
}
inline void update(int o, int l, int r, int x, int y, int w)
{
if (l == x && r == y){
sum[o] += 1LL*w*(r-l+1);
tag[o] += w;
return;
}
pushdown(o, l, r);
if (y <= mid) update(lc, l, mid, x, y, w);
else if (x > mid) update(rc, mid+1, r, x, y, w);
else update(lc, l, mid, x, mid, w), update(rc, mid+1, r, mid+1, y, w);
pushup(o);
}
inline LL query(int o, int l, int r, int x, int y)
{
if (l == x && r == y) return sum[o];
pushdown(o, l, r);
if (y <= mid) return query(lc, l, mid, x, y);
else if (x > mid) return query(rc, mid+1, r, x, y);
else return query(lc, l, mid, x, mid) + query(rc, mid+1, r, mid+1, y);
}
inline LL solve(int x, int y)
{
LL ret = 0;
while (top[x] != top[y]){
if (depth[top[x]] < depth[top[y]]) swap(x, y);
ret += query(1, 1, n, in[top[x]], in[x]);
x = fa[top[x]];
}
if (depth[x] > depth[y]) swap(x, y);
return ret + query(1, 1, n, in[x], in[y]);
}
int main()
{
scanf("%d%d", &n, &q);
rep(i, 1, n) scanf("%d", &a[i]);
rep(i, 1, n-1){
int x, y; scanf("%d%d", &x, &y);
add(x, y); add(y, x);
}
dfs(1, 0, 0); dfs2(1, 1);
build(1, 1, n);
while (q --){
int type, x, y;
scanf("%d", &type);
if (type == 1){
scanf("%d%d", &x, &y);
point(1, 1, n, in[x], y);
} else if (type == 2){
scanf("%d%d", &x, &y);
update(1, 1, n, in[x], out[x], y);
} else {
scanf("%d", &x);
printf("%lld\n", solve(1, x));
}
}
return 0;
}