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
| #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 1; const int inf = 2e9; int to[N << 1], nxt[N << 1], x, y; int n, m, root, tot, head[N], dep[N], siz[N], son[N], top[N], f[N], dfn[N], rnk[N], cnt; char op[15]; class Tree { private: int ls(int p) { return p << 1; } int rs(int p) { return p << 1 | 1; }
public: int sum[N << 2], tag[N << 2], maxx[N << 2], a[N]; void push_up(int p) { sum[p] = sum[ls(p)] + sum[rs(p)], maxx[p] = max(maxx[ls(p)], maxx[rs(p)]); } void build(int p, int l, int r) { if (l == r) { maxx[p] = sum[p] = a[rnk[l]]; return; } int mid(l + r >> 1); build(ls(p), l, mid); build(rs(p), mid + 1, r); push_up(p); } int query1(int p, int l, int r, int ql, int qr) { if (l > qr || r < ql) return -inf; if (ql <= l && r <= qr) return maxx[p]; int mid(l + r >> 1); return max(query1(ls(p), l, mid, ql, qr), query1(rs(p), mid + 1, r, ql, qr)); } int query2(int p, int l, int r, int ql, int qr) { if (l > qr || r < ql) return 0; if (ql <= l && r <= qr) return sum[p]; int mid(l + r >> 1); return query2(ls(p), l, mid, ql, qr) + query2(rs(p), mid + 1, r, ql, qr); } void update(int p, int l, int r, int x, int t) { if (l == r) { maxx[p] = sum[p] = t; return; } int mid(l + r >> 1); if (x <= mid) update(ls(p), l, mid, x, t); else update(rs(p), mid + 1, r, x, t); push_up(p); } } T; void add(int u, int v) { to[++tot] = v; nxt[tot] = head[u]; head[u] = tot; } void dfs1(int u) { son[u] = -1, siz[u] = 1; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (!dep[v]) { dep[v] = dep[u] + 1; f[v] = u; dfs1(v); siz[u] += siz[v]; if (son[v] == -1 || siz[v] > siz[son[u]]) son[u] = v; } } } void dfs2(int x, int tv) { top[x] = tv; dfn[x] = ++cnt; rnk[cnt] = x; if (son[x] == -1) return; dfs2(son[x], tv); for (int i = head[x]; i; i = nxt[i]) { int v = to[i]; if (v != f[x] && v != son[x]) dfs2(v, v); } } int querymax(int x, int y) { int ret = -inf, fx = top[x], fy = top[y]; while (fx != fy) { if (dep[fx] >= dep[fy]) ret = max(ret, T.query1(1, 1, n, dfn[fx], dfn[x])), x = f[fx]; else ret = max(ret, T.query1(1, 1, n, dfn[fy], dfn[y])), y = f[fy]; fx = top[x]; fy = top[y]; } if (dfn[x] < dfn[y]) ret = max(ret, T.query1(1, 1, n, dfn[x], dfn[y])); else ret = max(ret, T.query1(1, 1, n, dfn[y], dfn[x])); return ret; } int querysum(int x, int y) { int ret = 0, fx = top[x], fy = top[y]; while (fx != fy) { if (dep[fx] >= dep[fy]) ret += T.query2(1, 1, n, dfn[fx], dfn[x]), x = f[fx]; else ret += T.query2(1, 1, n, dfn[fy], dfn[y]), y = f[fy]; fx = top[x]; fy = top[y]; } if (dfn[x] < dfn[y]) ret += T.query2(1, 1, n, dfn[x], dfn[y]); else ret += T.query2(1, 1, n, dfn[y], dfn[x]); return ret; }
int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } for (int i = 1; i <= n; i++) scanf("%d", &T.a[i]); dep[1] = 1; dfs1(1); dfs2(1, 1); T.build(1, 1, n); scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%s%d%d", op, &x, &y); if (!strcmp(op, "CHANGE")) T.update(1, 1, n, dfn[x], y); if (!strcmp(op, "QMAX")) printf("%d\n", querymax(x, y)); if (!strcmp(op, "QSUM")) printf("%d\n", querysum(x, y)); } return 0; }
|