题意
Sol
树链剖分板子 + 动态开节点线段树板子
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int ch(int x, int l, int r) {
return x >= l && x <= r;
}
int N, Q, C[MAXN], W[MAXN], dep[MAXN], fa[MAXN], son[MAXN], siz[MAXN], top[MAXN], id[MAXN], cnt;
vector<int> v[MAXN];
void dfs1(int x, int _fa) {
dep[x] = dep[_fa] + 1; siz[x] = 1; fa[x] = _fa;
for(auto &to : v[x]) {
if(to == _fa) continue;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf; id[x] = ++cnt;
if(!son[x]) return ;
dfs2(son[x], topf);
for(auto &to : v[x]) {
if(top[to]) continue;
dfs2(to, to);
}
}
const int SS = (3e6 + 10);
int root[MAXN], ls[SS], rs[SS], mx[SS], sum[SS], tot;
void update(int k) {
mx[k] = max(mx[ls[k]], mx[rs[k]]);
sum[k] = sum[ls[k]] + sum[rs[k]];
}
void Modify(int &k, int l, int r, int p, int v) {
if(!k) k = ++tot;
if(l == r) {sum[k] = v, mx[k] = v; return ;}
int mid = l + r >> 1;
if(p <= mid) Modify(ls[k], l, mid, p, v);
if(p > mid) Modify(rs[k], mid + 1, r, p, v);
update(k);
}
int Query(int k, int l, int r, int ql, int qr, int opt) {
if(ql <= l && r <= qr) return opt == 0 ? mx[k] : sum[k];
int mid = l + r >> 1;
if(ql > mid) return Query(rs[k], mid + 1, r, ql, qr, opt);
else if(qr <= mid) return Query(ls[k], l, mid, ql, qr, opt);
else return opt == 0 ? max(Query(ls[k], l, mid, ql, qr, opt), Query(rs[k], mid + 1, r, ql, qr, opt))
: Query(ls[k], l, mid, ql, qr, opt) + Query(rs[k], mid + 1, r, ql, qr, opt);
}
int TreeMax(int x, int y) {
int ans = -INF, p = x;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
chmax(ans, Query(root[C[p]], 1, N, id[top[x]], id[x], 0));
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
chmax(ans, Query(root[C[p]], 1, N, id[y], id[x], 0));
return ans;
}
int TreeSum(int x, int y) {
int ans = 0, p = x;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
ans += Query(root[C[p]], 1, N, id[top[x]], id[x], 1);
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
ans += Query(root[C[p]], 1, N, id[y], id[x], 1);
return ans;
}
signed main() {
N = read(); Q = read();
for(int i = 1; i <= N; i++) W[i] = read(), C[i] = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 1);
for(int i = 1; i <= N; i++)
Modify(root[C[i]], 1, N, id[i], W[i]);
while(Q--) {
char s[4];
scanf("%s", s);
int x = read(), c = read();
if(s[0] == 'C' && s[1] == 'C') {
Modify(root[C[x]], 1, N, id[x], 0);
Modify(root[c], 1, N, id[x], W[x]);
C[x] = c;
}
if(s[0] == 'C' && s[1] == 'W') {
Modify(root[C[x]], 1, N, id[x], c);
W[x] = c;
}
if(s[0] == 'Q' && s[1] == 'M') {
printf("%d\n", TreeMax(x, c));
}
if(s[0] == 'Q' && s[1] == 'S') {
printf("%d\n", TreeSum(x, c));
}
}
return 0;
}
/*
5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
QS 1 5
CC 3 1
QS 1 5
CW 3 3
QS 1 5
QM 2 4
*/