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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| #include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define LD long double #define reg register #define _rep(i, x, y) for(int i = x; i <= y; i++) #define _per(i, x, y) for(int i = x; i >= y; i--) template <typename T> inline void read(T &x) { x = 0; T f = (T) 1; char c = getchar(); for(; !isdigit(c); c = getchar()) {if(c == '-') f = -f;} for(; isdigit(c); c = getchar()) x = x * 10 + c - 48; x *= f; } template <typename T, typename... Args> inline void read(T& x, Args&... args) { read(x); read(args...); } template <typename T> inline void write(T x) { if(x < 0) {x = -x; putchar('-');} if(x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> inline void writesp(T x, char sp = ' ') {write(x); putchar(sp);} const int maxN = 2e5 + 10, maxM = 2e5 + 10, inf = -2e9 - 9, maxinf = 2e9 + 9; int n, m, ll[maxM], rr[maxM], leaf[maxN]; char opt[5]; struct Edge {int from, to, nxt, val;} edge[maxM << 1]; int head[maxN], w[maxN], sz = 0; void add_edge(int u, int v, int w) {edge[++sz].from = u; edge[sz].to = v; edge[sz].val = w; edge[sz].nxt = head[u]; head[u] = sz;} int siz[maxN], fa[maxN], hson[maxN], dep[maxN]; void dfs1(int u, int f, int d, int E) { fa[u] = f; dep[u] = d; siz[u] = 1; hson[u] = 0; w[u] = edge[E].val; int max_size = -1; for(int i = head[u]; i; i = edge[i].nxt) { if(edge[i].to == f) continue; dfs1(edge[i].to, u, d + 1, i); siz[u] += siz[edge[i].to]; if(max_size < siz[edge[i].to]) { hson[u] = edge[i].to; max_size = siz[edge[i].to]; } } } int timer = 0, dfn[maxN], tp[maxN], rank[maxN]; void dfs2(int u, int top) { dfn[u] = ++timer; rank[timer] = u; tp[u] = top; if(!hson[u]) return; dfs2(hson[u], top); for(int i = head[u]; i; i = edge[i].nxt) { if(edge[i].to == hson[u] || edge[i].to == fa[u]) continue; dfs2(edge[i].to, edge[i].to); } } struct node { int min, max, sum, tag, neg; }; struct SegmentTree { node tree[maxN << 2]; void push_up(int o) { tree[o].sum = tree[o << 1].sum + tree[o << 1 | 1].sum; tree[o].min = std::min(tree[o << 1].min, tree[o << 1 | 1].min); tree[o].max = std::max(tree[o << 1].max, tree[o << 1 | 1].max); } void push_down(int o, int l, int r) { if(tree[o].tag) { int mid = (l + r) >> 1; tree[o << 1].sum = tree[o << 1].sum + 1ll * (mid - l + 1) * tree[o].tag; tree[o << 1 | 1].sum = tree[o << 1 | 1].sum + 1ll * (r - mid) * tree[o].tag; tree[o << 1].min = tree[o << 1].min + tree[o].tag; tree[o << 1 | 1].min = tree[o << 1 | 1].min + tree[o].tag; tree[o << 1].max = tree[o << 1].max + tree[o].tag; tree[o << 1 | 1].max = tree[o << 1 | 1].max + tree[o].tag; tree[o << 1].tag = tree[o << 1].tag + tree[o].tag; tree[o << 1 | 1].tag = tree[o << 1 | 1].tag + tree[o].tag; tree[o].tag = 0; } } void push_downneg(int o, int l, int r) { if(tree[o].neg == 1) { int mid = (l + r) >> 1; tree[o << 1].neg ^= 1; tree[o << 1 | 1].neg ^= 1; tree[o << 1].sum *= -1; tree[o << 1 | 1].sum *= -1; int lmax = tree[o << 1].max, lmin = tree[o << 1].min, rmax = tree[o << 1 | 1].max, rmin = tree[o << 1 | 1].min; tree[o << 1].max = -lmin, tree[o << 1].min = -lmax, tree[o << 1 | 1].max = -rmin, tree[o << 1 | 1].min = -rmax; tree[o].neg = 0; } } void build(int o, int l, int r) { if(l == r) {tree[o].min = tree[o].max = tree[o].sum = w[rank[l]]; tree[o].neg = 0; leaf[l] = o; return;} int mid = (l + r) >> 1; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); push_up(o); } void modify(int o, int l, int r, int x, int k) { if(r < x || x < l) return; if(l == x && r == x) { tree[o].sum = k; tree[o].min = k; tree[o].max = k; return; } push_downneg(o, l, r); int mid = (l + r) >> 1; modify(o << 1, l, mid, x, k); modify(o << 1 | 1, mid + 1, r, x, k); push_up(o); } void negative(int o, int l, int r, int xl, int xr) { if(r < xl || xr < l) return; if(xl <= l && r <= xr) { int minval = tree[o].min, maxval = tree[o].max; tree[o].sum = -tree[o].sum; tree[o].max = -minval; tree[o].min = -maxval; tree[o].neg ^= 1; return; } push_downneg(o, l, r); int mid = (l + r) >> 1; negative(o << 1, l, mid, xl, xr); negative(o << 1 | 1, mid + 1, r, xl, xr); push_up(o); } int querysum(int o, int l, int r, int xl, int xr) { if(r < xl || xr < l) return 0; if(xl <= l && r <= xr) return tree[o].sum; push_downneg(o, l, r); int mid = (l + r) >> 1; return querysum(o << 1, l, mid, xl, xr) + querysum(o << 1 | 1, mid + 1, r, xl, xr); } int querymax(int o, int l, int r, int xl, int xr) { if(r < xl || xr < l) return inf; if(xl <= l && r <= xr) return tree[o].max; push_downneg(o, l, r); int mid = (l + r) >> 1; return std::max(querymax(o << 1, l, mid, xl, xr), querymax(o << 1 | 1, mid + 1, r, xl, xr)); } int querymin(int o, int l, int r, int xl, int xr) { if(r < xl || xr < l) return maxinf; if(xl <= l && r <= xr) return tree[o].min; push_downneg(o, l, r); int mid = (l + r) >> 1; return std::min(querymin(o << 1, l, mid, xl, xr), querymin(o << 1 | 1, mid + 1, r, xl, xr)); } void debug(int o, int l, int r) { printf("Node #%d, Range: [%d, %d], Status: [Sum = %d, Min = %d, Max = %d, Tag = %d, Neg = %d]\n", o, l, r, tree[o].sum, tree[o].min, tree[o].max, tree[o].tag, tree[o].neg); if(l == r) return; debug(o << 1, l, (l + r) >> 1); debug(o << 1 | 1, ((l + r) >> 1) + 1, r); } } sgt; struct heavylight { void range_negative(int u, int v) { while(tp[u] != tp[v]) { if(dep[tp[u]] < dep[tp[v]]) std::swap(u, v); sgt.negative(1, 1, n, dfn[tp[u]], dfn[u]); u = fa[tp[u]]; } if(u > v) std::swap(u, v); if(u != v) sgt.negative(1, 1, n, dfn[u] + 1, dfn[v]); } int path_sum(int u, int v) { int ans = 0; while(tp[u] != tp[v]) { if(dep[tp[u]] < dep[tp[v]]) std::swap(u, v); ans += sgt.querysum(1, 1, n, dfn[tp[u]], dfn[u]); u = fa[tp[u]]; }
if(u > v) std::swap(u, v); if(u != v) ans += sgt.querysum(1, 1, n, dfn[u] + 1, dfn[v]); return ans; } int path_max(int u, int v) { int ans = inf; while(tp[u] != tp[v]) { if(dep[tp[u]] < dep[tp[v]]) std::swap(u, v); ans = std::max(ans, sgt.querymax(1, 1, n, dfn[tp[u]], dfn[u])); u = fa[tp[u]]; } if(u > v) std::swap(u, v); if(u != v) ans = std::max(ans, sgt.querymax(1, 1, n, dfn[u] + 1, dfn[v])); return ans; } int path_min(int u, int v) { int ans = maxinf; while(tp[u] != tp[v]) { if(dep[tp[u]] < dep[tp[v]]) std::swap(u, v); ans = std::min(ans, sgt.querymin(1, 1, n, dfn[tp[u]], dfn[u])); u = fa[tp[u]]; } if(u > v) std::swap(u, v); if(u != v) ans = std::min(ans, sgt.querymin(1, 1, n, dfn[u] + 1, dfn[v])); return ans; } } hld; int main() { read(n); _rep(i, 1, n - 1) { int u, v, w; read(u, v, w); u++; v++; ll[i] = u; rr[i] = v; add_edge(u, v, w); add_edge(v, u, w); } dfs1(1, 0, 0, 0); dfs2(1, 1);
sgt.build(1, 1, n); read(m); _rep(i, 1, m) { scanf("%s", opt); int x, y; read(x, y);
if(opt[0] == 'C') { int q = (dep[ll[x]] > dep[rr[x]] ? ll[x] : rr[x]);
sgt.modify(1, 1, n, dfn[q], y); } else if(opt[0] == 'N') { x++; y++; hld.range_negative(x, y); } else if(opt[1] == 'U') { x++; y++; writesp(hld.path_sum(x, y), '\n'); } else if(opt[1] == 'I') { x++; y++; writesp(hld.path_min(x, y), '\n'); } else { x++; y++; writesp(hld.path_max(x, y), '\n'); } } return 0; }
|