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
| #include <bits/stdc++.h> using namespace std; #define debug cerr<<"The code runs successfully.\n"; #define endl '\n' #define TRACE 1 #define tcout TRACE && cout #define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define int long long #define lson(x) (x<<1) #define rson(x) (x<<1|1) const int P = 998244353; const int Base = 33331; const int INF = 0x3f3f3f3f3f3f3f3f; const int N = 2e5 + 10, M = 2e6 + 10;
struct segtree { int l, r, sum, max, min, lazy; } s[N << 2]; pair<int, int> edge[N]; vector<pair<int, int>> g[N]; int n, q, id[N], cnt, son[N], siz[N], fa[N], w[N], top[N], dep[N], wssb[N];
inline void calc(int p) { s[p].lazy ^= 1; s[p].sum = -s[p].sum; s[p].max = -s[p].max; s[p].min = -s[p].min; swap(s[p].max, s[p].min); } inline void pushup(int p) { s[p].sum = s[lson(p)].sum + s[rson(p)].sum; s[p].min = min(s[lson(p)].min, s[rson(p)].min); s[p].max = max(s[lson(p)].max, s[rson(p)].max); } inline void pushdown(int p) { if (!s[p].lazy) return; calc(lson(p)), calc(rson(p)); s[p].lazy ^= 1; } inline void build(int p, int l, int r) { s[p].l = l, s[p].r = r; if (l == r) { s[p].sum = s[p].min = s[p].max = w[l]; return; } int mid = l + r >> 1; build(lson(p), l, mid); build(rson(p), mid + 1, r); pushup(p); } inline void change1(int p, int x, int k) { if (s[p].l == s[p].r) { s[p].sum = s[p].max = s[p].min = k; return; } pushdown(p); int mid = s[p].l + s[p].r >> 1; if (mid >= x) change1(lson(p), x, k); else change1(rson(p), x, k); pushup(p); } inline void change2(int p, int l, int r) { if (s[p].r < l || r < s[p].l) return; if (s[p].l >= l && s[p].r <= r) { calc(p); return; } pushdown(p); int mid = s[p].l + s[p].r >> 1; change2(lson(p), l, r), change2(rson(p), l, r); pushup(p); } inline int asksum(int p, int l, int r) { if (s[p].r < l || r < s[p].l) return 0; if (s[p].l >= l && s[p].r <= r) return s[p].sum; pushdown(p); return asksum(lson(p), l, r) + asksum(rson(p), l, r); } inline int askmax(int p, int l, int r) { if (s[p].r < l || r < s[p].l) return -INF; if (s[p].l >= l && s[p].r <= r) return s[p].max; pushdown(p); return max(askmax(lson(p), l, r), askmax(rson(p), l, r)); } inline int askmin(int p, int l, int r) { if (s[p].r < l || r < s[p].l) return INF; if (s[p].l >= l && s[p].r <= r) return s[p].min; pushdown(p); return min(askmin(lson(p), l, r), askmin(rson(p), l, r)); }
inline void dfs1(int f, int u) { fa[u] = f, dep[u] = dep[f] + 1, siz[u] = 1; int tmp = -1; for (auto e : g[u]) { int v = e.first; if (v == f) continue; wssb[v] = e.second; dfs1(u, v); siz[u] += siz[v]; if (tmp < siz[v]) { son[u] = v, tmp = siz[v]; } } } inline void dfs2(int f, int u) { id[u] = ++cnt, w[cnt] = wssb[u], top[u] = f; if (son[u]) dfs2(f, son[u]); for (auto e : g[u]) { int v = e.first; if (fa[u] == v || v == son[u]) continue; dfs2(v, v); } } inline void update1(int x, int k) { int ccf; if (dep[edge[x].first] > dep[edge[x].second]) ccf = edge[x].first; else ccf = edge[x].second; change1(1, id[ccf], k); } inline void update2(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); change2(1, id[top[x]], id[x]); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (x != y) change2(1, id[x] + 1, id[y]); } inline int querysum(int x, int y) { int res = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); res += asksum(1, id[top[x]], id[x]); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (x != y) res += asksum(1, id[x] + 1, id[y]); return res; } inline int querymax(int x, int y) { int res = -INF; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); res = max(res, askmax(1, id[top[x]], id[x])); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (x != y) res = max(res, askmax(1, id[x] + 1, id[y])); return res; } inline int querymin(int x, int y) { int res = INF; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); res = min(res, askmin(1, id[top[x]], id[x])); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); if (x != y) res = min(res, askmin(1, id[x] + 1, id[y])); return res; }
signed main() { fst; cin >> n; for (int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; u++, v++; g[u].push_back({v, w}); g[v].push_back({u, w}); edge[i] = {u, v}; } dfs1(0, 1); dfs2(1, 1); build(1, 1, n); cin >> q; while (q--) { string op; int x, y; cin >> op >> x >> y; if (op == "C") { update1(x, y); } else if (op == "N") { x++, y++; update2(x, y); } else if (op == "SUM") { x++, y++; cout << querysum(x, y) << endl; } else if (op == "MAX") { x++, y++; cout << querymax(x, y) << endl; } else { x++, y++; cout << querymin(x, y) << endl; } } return 0; }
|