题目链接:传送门
动态开点+树剖的
模板吧。
都很熟的话
就挺好写的
特别注意在dfs序上修改
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <random>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
int cnt, n, m, rt[B], a[B], b[B];
namespace Seg {
struct node {
int l, r, w, mx;
}tree[A << 1];
void up(int k) {
tree[k].w = tree[tree[k].l].w + tree[tree[k].r].w;
tree[k].mx = max(tree[tree[k].l].mx, tree[tree[k].r].mx);
}
void change(int &k, int l, int r, int pos, int val) {
if (!k) k = ++cnt;
if (l == r) {
tree[k].w = tree[k].mx = val;
return;
}
int m = (l + r) >> 1;
if (pos <= m) change(tree[k].l, l, m, pos, val);
else change(tree[k].r, m + 1, r, pos, val);
up(k);
}
void dele(int &k, int l, int r, int pos) {
if (!k) return;
if (l == r) {
tree[k].w = tree[k].mx = 0;
return;
}
int m = (l + r) >> 1;
if (pos <= m) dele(tree[k].l, l, m, pos);
else dele(tree[k].r, m + 1, r, pos);
up(k);
}
int asksum(int &k, int l, int r, int nowl, int nowr) {
if (!k) return 0;
if (nowl >= l and nowr <= r) return tree[k].w;
int ans = 0, m = (nowl + nowr) >> 1;
if (l <= m) ans += asksum(tree[k].l, l, r, nowl, m);
if (r > m) ans += asksum(tree[k].r, l, r, m + 1, nowr);
return ans;
}
int askmax(int &k, int l, int r, int nowl, int nowr) {
if (!k) return 0;
if (nowl >= l and nowr <= r) return tree[k].mx;
int ans = 0, m = (nowl + nowr) >> 1;
if (l <= m) ans = max(ans, askmax(tree[k].l, l, r, nowl, m));
if (r > m) ans = max(ans, askmax(tree[k].r, l, r, m + 1, nowr));
return ans;
}
}
int dfn[B], pre[B], top[B], siz[B], son[B], dep[B], cntt, fa[B];
namespace Cut {
struct node {
int next, to;
}e[B << 1];
int head[B << 1], num;
void add(int fr, int to) {
e[++num].next = head[fr];
e[num].to = to;
head[fr] = num;
}
void prepare(int fr) {
siz[fr] = 1;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr]) continue;
fa[ca] = fr;
dep[ca] = dep[fr] + 1;
prepare(ca);
siz[fr] += siz[ca];
if (siz[ca] > siz[son[fr]]) son[fr] = ca;
}
}
void dfs(int fr, int tp) {
dfn[fr] = ++cntt, pre[cntt] = fr, top[fr] = tp;
if (!son[fr]) return;
dfs(son[fr], tp);
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == son[fr] or ca == fa[fr]) continue;
dfs(ca, ca);
}
}
int asksum(int x, int y, int ans = 0) {
int pos = b[x];
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans += Seg::asksum(rt[pos], dfn[top[x]], dfn[x], 1, n);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
ans += Seg::asksum(rt[pos], dfn[x], dfn[y], 1, n);
return ans;
}
int askmax(int x, int y, int ans = 0) {
int pos = b[x];
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans = max(ans, Seg::askmax(rt[pos], dfn[top[x]], dfn[x], 1, n));
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
ans = max(ans, Seg::askmax(rt[pos], dfn[x], dfn[y], 1, n));
return ans;
}
}
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m); int x, y, c, w;
for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
for (int i = 2; i <= n; i++) {
scanf("%d%d", &x, &y);
Cut::add(x, y); Cut::add(y, x);
}
Cut::prepare(1); Cut::dfs(1, 1);
for (int i = 1; i <= n; i++) Seg::change(rt[b[pre[i]]], 1, n, i, a[pre[i]]);
while (m--) {
char opt[2]; scanf("%s", opt);
if (opt[1] == 'C') {
scanf("%d%d", &x, &c);
Seg::dele(rt[b[x]], 1, n, dfn[x]);
b[x] = c;
Seg::change(rt[b[x]], 1, n, dfn[x], a[x]);
}
if (opt[1] == 'W') {
scanf("%d%d", &x, &w);
Seg::dele(rt[b[x]], 1, n, dfn[x]);
a[x] = w;
Seg::change(rt[b[x]], 1, n, dfn[x], a[x]);
}
if (opt[1] == 'S') {
scanf("%d%d", &x, &y);
printf("%d\n", Cut::asksum(x, y));
}
if (opt[1] == 'M') {
scanf("%d%d", &x, &y);
printf("%d\n", Cut::askmax(x, y));
}
}
}