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
| #include <cstdio> #include <vector> #include <algorithm> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' && c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 5e4 + 10; struct Node { int l, r, tag, mn, mx, lv, rv; friend Node operator ~ (Node _) { Node res = _; swap(res.lv, res.rv); return res; } } tr[N << 2]; vector <int> g[N]; int n, m, w[N], d[N], p[N], sz[N]; int stmp, dfn[N], rnk[N], son[N], top[N]; void pushup (Node &x, Node l, Node r) { x.mn = min(l.mn, r.mn); x.mx = max(l.mx, r.mx); x.lv = max(max(l.lv, r.lv), r.mx - l.mn); x.rv = max(max(l.rv, r.rv), l.mx - r.mn); } void add (int x, int k) { tr[x].mn += k, tr[x].mx += k; tr[x].tag += k; } void pushdown (int x) { add(x << 1, tr[x].tag), add(x << 1 | 1, tr[x].tag); tr[x].tag = 0; } void build (int l = 1, int r = n, int x = 1) { tr[x].l = l, tr[x].r = r; if (l == r) return void(tr[x].mn = tr[x].mx = w[rnk[l]]); int mid = l + r >> 1; build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1); pushup(tr[x], tr[x << 1], tr[x << 1 | 1]); } void modify (int l, int r, int k, int x = 1) { if (l > tr[x].r || tr[x].l > r) return; if (tr[x].l >= l && tr[x].r <= r) return add(x, k); pushdown(x); modify(l, r, k, x << 1), modify(l, r, k, x << 1 | 1); pushup(tr[x], tr[x << 1], tr[x << 1 | 1]); } Node query (int l, int r, int x = 1) { if (tr[x].l >= l && tr[x].r <= r) return tr[x]; pushdown(x); int mid = tr[x].l + tr[x].r >> 1; if (r <= mid) return query(l, r, x << 1); if (l > mid) return query(l, r, x << 1 | 1); Node res; pushup(res, query(l, r, x << 1), query(l, r, x << 1 | 1)); return res; } void dfs1 (int x) { sz[x] = 1; son[x] = -1; for (int i : g[x]) if (!d[i]) { p[i] = x; d[i] = d[x] + 1; dfs1(i); sz[x] += sz[i]; (son[x] == -1 || sz[i] > sz[son[x]]) && (son[x] = i); } } void dfs2 (int x, int t) { rnk[dfn[x] = ++stmp] = x; top[x] = t; ~son[x] && (dfs2(son[x], t), 0); for (int i : g[x]) i ^ p[x] && i ^ son[x] && (dfs2(i, i), 0); } void ModifyPath (int x, int y, int k) { while (top[x] ^ top[y]) { d[top[x]] < d[top[y]] && (swap(x, y), 0); modify(dfn[top[x]], dfn[x], k); x = p[top[x]]; } d[x] > d[y] && (swap(x, y), 0); modify(dfn[x], dfn[y], k); } int QueryPath (int x, int y) { Node l, r; bool _l = false, _r = false; while (top[x] ^ top[y]) if (d[top[x]] > d[top[y]]) { Node t = ~query(dfn[top[x]], dfn[x]); _l ? pushup(l, l, t) : (l = t, void(_l = true)); x = p[top[x]]; } else { Node t = query(dfn[top[y]], dfn[y]); _r ? pushup(r, t, r) : (r = t, void(_r = true)); y = p[top[y]]; } if (d[x] < d[y]) _l ? pushup(l, l, query(dfn[x], dfn[y])) : void(l = query(dfn[x], dfn[y])); else _l ? pushup(l, l, ~query(dfn[y], dfn[x])) : void(l = ~query(dfn[y], dfn[x])); return _r ? (pushup(l, l, r), l.lv) : l.lv; } int main () { read(n); for (int i = 1; i <= n; i++) read(w[i]); for (int i = 1, u, v; i < n; i++) { read(u), read(v); g[u].push_back(v); g[v].push_back(u); } d[1] = 1, dfs1(1), dfs2(1, 1), build(); read(m); for (int a, b, v; m; m--) { read(a), read(b), read(v); write(QueryPath(a, b)), puts(""); ModifyPath(a, b, v); } return 0; }
|