[洛谷P3178] [HAOI2015]树上操作
题目链接:###
题目分析:###
和树链剖分模板没什么区别,子树加就是直接在线段树上\(modify(1, num[u], num[u] + siz[u] - 1, d)\)即可(用dfs序的思想,且注意减掉\(u\)节点自己)
不多讲了,直接上代码吧。
代码:###
#include<bits/stdc++.h>
#define int long long
#define N (100000 + 5)
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
return cnt * f;
}
int first[N], nxt[N * 2], to[N * 2], tot;
void Add(int x, int y) {
nxt[++tot] = first[x];
first[x] = tot;
to[tot] = y;
}
int father[N], son[N], num[N], top[N], id[N], siz[N], dep[N], idx;
void dfs1(int cur, int fa) {
father[cur] = fa, siz[cur] = 1, dep[cur] = dep[fa] + 1;
for (register int i = first[cur]; i; i = nxt[i]) {
int v = to[i];
if (v != fa) {
dfs1(v, cur);
siz[cur] += siz[v];
if (siz[son[cur]] < siz[v]) son[cur] = v;
}
}
}
void dfs2(int cur, int tp) {
num[cur] = ++idx, id[idx] = cur, top[cur] = tp;
if (son[cur]) dfs2(son[cur], tp);
for (register int i = first[cur]; i; i = nxt[i]) {
int v = to[i];
if (!num[v]) dfs2(v, v);
}
}
struct node {
int l, r, sum, tag;
#define l(p) tree[p].l
#define r(p) tree[p].r
#define sum(p) tree[p].sum
#define tag(p) tree[p].tag
}tree[N << 2];
void push_up(int p) {
sum(p) = sum(p << 1) + sum(p << 1 | 1);
}
void push_down(int p) {
sum(p << 1) += tag(p) * (r(p << 1) - l(p << 1) + 1);
sum(p << 1 | 1) += tag(p) * (r(p << 1 | 1) - l(p << 1 | 1) + 1);
tag(p << 1) += tag(p), tag(p << 1 | 1) += tag(p);
tag(p) = 0;
}
int a[N];
void build_tree(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r) {sum(p) = a[id[l]]; return;}
int mid = (l + r) >> 1;
build_tree(p << 1, l, mid);
build_tree(p << 1 | 1, mid + 1, r);
push_up(p);
}
void modify(int p, int l, int r, int d) {
if (l <= l(p) && r >= r(p)) {
sum(p) += d * (r(p) - l(p) + 1), tag(p) += d;
return;
}
push_down(p);
int mid = (l(p) + r(p)) >> 1;
if (l <= mid) modify(p << 1, l, r, d);
if (r > mid) modify(p << 1 | 1, l, r, d);
push_up(p);
}
int query(int p, int l, int r) {
if (l <= l(p) && r >= r(p)) return sum(p);
push_down(p);
int mid = (l(p) + r(p)) >> 1;
long long val = 0;
if (l <= mid) val += query(p << 1, l, r);
if (r > mid) val += query(p << 1 | 1, l, r);
return val;
}
int chain_query(int u, int v) {
long long ans = 0;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ans += query(1, num[top[u]], num[u]);
u = father[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
ans += query(1, num[v], num[u]);
return ans;
}
int n, m, x, y;
int ope;
void solve() {
n = read(); m = read();
for (register int i = 1; i <= n; i++) a[i] = read();
for (register int i = 1; i < n; i++) {
x = read(), y = read();
Add(x, y), Add(y, x);
}
dfs1(1, 0); dfs2(1, 1); build_tree(1, 1, n);
for (register int i = 1; i <= m; i++) {
ope = read();
if (ope == 1) {
x = read(); y = read();
modify(1, num[x], num[x], y);
}
if (ope == 2) {
x = read(); y = read();
modify(1, num[x], num[x] + siz[x] - 1, y);
}
if (ope == 3) {
x = read();
printf("%lld\n", chain_query(1, x));
}
}
}
signed main() {
solve();
return 0;
}