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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <algorithm> #include <iostream> #include <fstream> #include <cstring> #include <cstdlib> #include <cstdio> #define mmst(a, b) memset(a, b, sizeof(a)) #define lson (root<<1) #define rson ((root<<1)|1) using namespace std;
const int MAXN = 30010; const int MAXE = MAXN<<1;
void ri(int &x);
int n, q;
int to[MAXE], nex[MAXE], Mindex[MAXN], cur=0; int w[MAXN], v[MAXN], top[MAXN], fa[MAXN], son[MAXN], siz[MAXN], depth[MAXN], z=0;
int SUM[MAXN<<2], MAX[MAXN<<2];
void addEdge(const int a, const int b); void dfs(const int x) { son[x] = 0; siz[x] = 1; for (int i=Mindex[x]; i!=-1; i=nex[i]) { if (to[i] != fa[x]) { fa[to[i]] = x; depth[to[i]] = depth[x] + 1; dfs(to[i]); if (siz[to[i]] > siz[son[x]]) son[x] = to[i]; siz[x] += siz[to[i]]; } } } void set_tree(const int x, const int tp) { top[x] = tp; w[x] = ++z; if (son[x]!=0) set_tree(son[x], tp); for (int i=Mindex[x]; i!=-1; i=nex[i]) { if (to[i]!=fa[x] && to[i]!=son[x]) { set_tree(to[i], to[i]); } } }
void update(int l, int r, int p, int val, int root) { if (l==r) { SUM[root] = MAX[root] = val; return ; } const int mid = (l+r) >> 1; if (p<=mid) update(l, mid, p, val, lson); else update(mid+1, r, p, val, rson); SUM[root] = SUM[lson] + SUM[rson]; MAX[root] = max(MAX[lson], MAX[rson]); }
int qMax(int l, int r, int L, int R, int root) { if (L<=l && r<=R) return MAX[root]; const int mid = (l+r) >> 1; int ref = -MAXN; if (L<=mid) ref = qMax(l, mid, L, R, lson); if (mid<R) ref = max(ref, qMax(mid+1, r, L, R, rson)); return ref; }
int qSum(int l, int r, int L, int R, int root) { if (L<=l && r<=R) return SUM[root]; const int mid = (l+r) >> 1; int ref = 0; if (L<=mid) ref = qSum(l, mid, L, R, lson); if (mid<R) ref += qSum(mid+1, r, L, R, rson); return ref; }
int findMax(int a, int b) { int ref = -MAXN; int f1 = top[a], f2 = top[b]; while (f1 != f2) { if (depth[f1] < depth[f2]) { swap(a, b); swap(f1, f2); } ref = max(ref, qMax(1, z, w[f1], w[a], 1)); a = fa[f1]; f1 = top[a]; } if (a == b) return max(ref, v[a]); if (depth[a] > depth[b]) swap(a, b); return max(ref, qMax(1, z, w[a], w[b], 1)); }
int findSum(int a, int b) { int ref = 0; int f1 = top[a], f2 = top[b]; while (f1 != f2) { if (depth[f1] < depth[f2]) { swap(a, b); swap(f1, f2); } ref += qSum(1, z, w[f1], w[a], 1); a = fa[f1]; f1 = top[a]; } if (a == b) return ref + v[a]; if (depth[a] > depth[b]) swap(a, b); return ref + qSum(1, z, w[a], w[b], 1); }
int main() {
mmst(Mindex, -1); ri(n); for (int i=1, a, b; i<n; ++i) { ri(a); ri(b); addEdge(a, b); } for (int i=1; i<=n; ++i) ri(v[i]); siz[0] = 0; depth[1] = 1; fa[1] = 0; dfs(1); set_tree(1, 1); for (int i=1; i<=n; ++i) update(1, z, w[i], v[i], 1); ri(q); char que[7]; int q1, q2; while (q--) { scanf("%s", que); ri(q1); ri(q2); if (que[1] == 'M') printf("%d\n", findMax(q1, q2)); else if (que[1] == 'S') printf("%d\n", findSum(q1, q2)); else update(1, z, w[q1], v[q1]=q2, 1); } return 0; }
inline void addEdge(const int a, const int b) { to[cur] = b; nex[cur] = Mindex[a]; Mindex[a] = cur++; to[cur] = a; nex[cur] = Mindex[b]; Mindex[b] = cur++; }
inline void ri(int &x) { char c; bool minus = false; while ((c=getchar())<'0' || '9'<c) if (c=='-') minus=true; x = c-'0'; while ('0'<=(c=getchar()) && c<='9') x = 10*x+c-'0'; if (minus) x = -x; }
|