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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
| template<class info, class mark> struct SMT { int L, R, p, pcnt; using pii = struct { info f, b; }; vector<pii> t; vector<mark> m; vector<int> ls, rs;
SMT() { init(0, 0); t.resize(N << 2); m.resize(N << 2); ls.resize(N << 2); rs.resize(N << 2); }
void init(int l, int r) { L = l, R = r; p = pcnt = 0; }
void init(int p) { t[p] = { info(),info() }; m[p] = mark(); ls[p] = rs[p] = 0; }
#define mid ((L+R)>>1) #define leaf (R-L<2) #define curr p,L,R #define lson ls[p],L,mid #define rson rs[p],mid,R #define self p,int L,int R
void push_up(int& p) { t[p].f = t[ls[p]].f + t[rs[p]].f; t[p].b = t[rs[p]].b + t[ls[p]].b; }
void push_down(int& self, const mark& d) { if (!p) p = ++pcnt, init(p); t[p].f.apply(d, L, R); t[p].b.apply(d, L, R); m[p].apply(d, L, R); } void push_down(int& self) { if (leaf) return; push_down(lson, m[p]); push_down(rson, m[p]); m[p] = mark(); }
void modify(int& self, int l, int r, const mark& d) { if (!p) p = ++pcnt, init(p); if (l <= L && R <= r) return push_down(curr, d); push_down(curr); if (l < mid) modify(lson, l, r, d); if (mid < r) modify(rson, l, r, d); return push_up(p); } void modify(int l, int r, const mark& d) { if (l >= r) return; return modify(curr, l, r, d); }
void modify(int& self, int x, const info& d) { if (!p) p = ++pcnt, init(p); if (leaf) return t[p].apply(d); push_down(curr); if (x < mid) modify(lson, x, d); else modify(rson, x, d); return push_up(p); } void modify(int x, const info& d) { return modify(curr, x, d); }
pii query(int& self, int l, int r) { if (l <= L && R <= r) return t[p]; push_down(curr); if (r <= mid) return query(lson, l, r); if (mid <= l) return query(rson, l, r); pii les = query(lson, l, r), res = query(rson, l, r); return { les.f + res.f, res.b + les.b }; } pii query(int l, int r) { if (l >= r) return { info(),info() }; return query(curr, l, r); } };
struct mark { Z a{ inf }, b{ inf }; void apply(const mark& self)& { if (p.a != inf && p.b != inf) { a = p.a; b = p.b; } } };
struct info { Z a{ 1 }, b{ 0 }; void apply(const mark& self)& { if (p.a != inf && p.b != inf) { a = p.a; b = p.b; } } friend info operator + (const info& l, const info& r) { return { l.a * r.a, r.a * l.b + r.b }; } };
struct TREE { int n, unix; vector<vector<int>> E; vector<int> p, dep, siz, top, dfn, dfnR, seq; SMT<info, mark> smt;
TREE() {} TREE(int n) { init(n); }
void init(int n) { this->n = n; E.assign(n, {}); p.assign(n, -1); dep.resize(n); siz.resize(n); top.resize(n); dfn.resize(n); dfnR.resize(n); seq.resize(n); unix = 0; smt.init(0, n); }
void add(int u, int v) { E[u].push_back(v); E[v].push_back(u); }
void work(int s = 0) { top[s] = s; dep[s] = 0; p[s] = -1; dfs1(s); dfs2(s); }
void dfs1(int u) { if (p[u] != -1) E[u].erase(find(E[u].begin(), E[u].end(), p[u])); siz[u] = 1; for (auto& v : E[u]) { p[v] = u; dep[v] = dep[u] + 1; dfs1(v); siz[u] += siz[v]; if (siz[v] > siz[E[u][0]]) swap(v, E[u][0]); } }
void dfs2(int u) { seq[dfn[u] = unix++] = u; for (auto& v : E[u]) { top[v] = v == E[u][0] ? top[u] : v; dfs2(v); } dfnR[u] = unix; }
int lca(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] > dep[top[v]]) u = p[top[u]]; else v = p[top[v]]; } return dep[u] > dep[v] ? v : u; }
int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
bool isAncester(int u, int v) { return dfn[u] <= dfn[v] && dfn[v] < dfnR[u]; }
int kthAncester(int u, int k) { if (dep[u] < k) return -1; int d = dep[u] - k; while (dep[top[u]] > d) { u = p[top[u]]; } return seq[dfn[u] - dep[u] + d]; }
int getson(int u, int v) { if (dep[u] > dep[v]) return -1; return kthAncester(v, dep[v] - dep[u] - 1); }
void modify_path(int u, int v, const mark& d, bool op) { while (top[u] != top[v]) { if (dep[top[u]] > dep[top[v]]) { smt.modify(dfn[top[u]], dfn[u] + 1, d); u = p[top[u]]; } else { smt.modify(dfn[top[v]], dfn[v] + 1, d); v = p[top[v]]; } } if (dep[u] > dep[v]) { smt.modify(dfn[v] + op, dfn[u] + 1, d); } else { smt.modify(dfn[u] + op, dfn[v] + 1, d); } }
info query_path(int u, int v, bool op) { info les, res; while (top[u] != top[v]) { if (dep[top[u]] > dep[top[v]]) { les = les + smt.query(dfn[top[u]], dfn[u] + 1).b; u = p[top[u]]; } else { res = smt.query(dfn[top[v]], dfn[v] + 1).f + res; v = p[top[v]]; } } if (dep[u] > dep[v]) { les = les + smt.query(dfn[v] + op, dfn[u] + 1).b; } else { res = smt.query(dfn[u] + op, dfn[v] + 1).f + res; } return les + res; }
void modify_node(int u, const mark& d) { smt.modify(dfn[u], dfn[u] + 1, d); } info query_node(int u) { return smt.query(dfn[u], dfn[u] + 1).f; } };
int main() { int n, q; cin >> n >> q;
TREE tree(n);
vector<mark> w(n); for (int i = 0; i < n; i++) { Z a, b; cin >> a >> b; w[i] = { a, b }; }
for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; tree.add(u, v); }
tree.work(0);
for (int i = 0; i < n; i++) { tree.modify_node(i, w[i]); }
while (q--) { int op; cin >> op; if (op == 0) { int p; Z a, b; cin >> p >> a >> b; tree.modify_node(p, { a, b }); } else { int u, v; Z x; cin >> u >> v >> x; auto res = tree.query_path(u, v, 0); cout << res.a * x + res.b << endl; } }
return 0; }
|