1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <bits/stdc++.h> using namespace std;
void read(int &ret) { ret = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { ret = (ret<<1) + (ret<<3) + (ch^48); ch = getchar(); } return ; }
const int MAXN = 1e5 + 10; const int MAX_SIZE = 2e7;
int n, Q, cnt, tot, idx; int w[MAXN], c[MAXN], wl[MAXN], wc[MAXN]; int dep[MAXN], siz[MAXN], fa[MAXN], son[MAXN]; int tp[MAXN], root[MAXN], id[MAXN]; char opt[3]; int head[MAXN<<1];
struct Edge { int to, nxt; } e[MAXN<<1]; void addEdge(int u, int v) { e[++cnt].to = v; e[cnt].nxt = head[u]; head[u] = cnt; return ; }
void dfs1(int cur, int f) { fa[cur] = f, siz[cur] = 1; dep[cur] = dep[f] + 1; int heavyson = -1; for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (to == f) continue; dfs1(to, cur), siz[cur] += siz[to]; if (siz[to] > heavyson) heavyson = siz[to], son[cur] = to; } return ; }
void dfs2(int cur, int Tp) { tp[cur] = Tp, id[cur] = ++idx; if (!son[cur]) return ; dfs2(son[cur], Tp); for (int i=head[cur]; i; i=e[i].nxt) { int to = e[i].to; if (to==fa[cur] || to==son[cur]) continue; dfs2(to, to); } return ; }
struct seg { int ls, rs, Max, val; } tree[MAX_SIZE]; void pushup(int rt) { tree[rt].val = tree[tree[rt].ls].val + tree[tree[rt].rs].val; tree[rt].Max = max(tree[tree[rt].ls].Max, tree[tree[rt].rs].Max); return ; }
void upd(int &rt, int l, int r, int pos, int W) { if (!rt) rt = ++tot; if (l==r) { tree[rt].val = tree[rt].Max = W; return ; } int mid = (l+r) >> 1; if (pos<=mid) upd(tree[rt].ls, l, mid, pos, W); else upd(tree[rt].rs, mid+1, r, pos, W); pushup(rt); return ; }
void remv(int rt, int l, int r, int pos) { if (l==r) { tree[rt].val = tree[rt].Max = 0; return ; } int mid = (l+r) >> 1; if (pos<=mid) remv(tree[rt].ls, l, mid, pos); else remv(tree[rt].rs, mid+1, r, pos); pushup(rt); return ; }
int querys(int rt, int l, int r, int L, int R) { if (L<=l && r<=R) return tree[rt].val; int mid = (l+r) >> 1, ans = 0; if (L<=mid) ans += querys(tree[rt].ls, l, mid, L, R); if (R>mid) ans += querys(tree[rt].rs, mid+1, r, L, R); return ans; }
int querym(int rt, int l, int r, int L, int R) { if (L<=l && r<=R) return tree[rt].Max; int mid = (l+r) >> 1, ans = 0; if (L<=mid) ans = max(ans, querym(tree[rt].ls, l, mid, L, R)); if (R>mid) ans = max(ans, querym(tree[rt].rs, mid+1, r, L, R)); return ans; }
int qus(int l, int r, int rel) { int ans = 0; while (tp[l] ^ tp[r]) { if (dep[tp[l]] < dep[tp[r]]) swap(l, r); ans += querys(root[rel], 1, n, id[tp[l]], id[l]); l = fa[tp[l]]; } if (dep[l] > dep[r]) swap(l, r); ans += querys(root[rel], 1, n, id[l], id[r]); return ans; }
int qum(int l, int r, int rel) { int ans = 0; while (tp[l] ^ tp[r]) { if (dep[tp[l]] < dep[tp[r]]) swap(l, r); ans = max(ans, querym(root[rel], 1, n, id[tp[l]], id[l])); l = fa[tp[l]]; } if (dep[l] > dep[r]) swap(l, r); ans = max(ans, querym(root[rel], 1, n, id[l], id[r])); return ans; }
int main() { read(n), read(Q); for (int i=1; i<=n; ++i) read(w[i]), read(c[i]); for (int i=1; i<n; ++i) { int u, v; read(u), read(v); addEdge(u, v); addEdge(v, u); } dfs1(1, 1), dfs2(1, 1); for (int i=1; i<=n; ++i) upd(root[c[i]], 1, n, id[i], w[i]); while (Q--) { cin >> opt+1; int u, v; read(u), read(v); if (opt[2] == 'C') { remv(root[c[u]], 1, n, id[u]); upd(root[v], 1, n, id[u], w[u]); c[u] = v; } else if (opt[2] == 'W') { remv(root[c[u]], 1, n, id[u]); upd(root[c[u]], 1, n, id[u], v); w[u] = v; } else if (opt[2] == 'S') printf("%d\n", qus(u, v, c[u])); else if (opt[2] == 'M') printf("%d\n", qum(u, v, c[u])); } return 0; }
|