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
|
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int dep[maxn], fa[maxn], siz[maxn], son[maxn]; int id[maxn], top[maxn], rk[maxn]; int n, m, r, mod, rt, cnt; int head[maxn], v[maxn]; struct edge{ int next, to; }e[maxn * 2]; struct node{ int l, r, sum, lazy; }t[maxn << 2]; void add(int x, int y){ e[++cnt].next = head[x]; e[cnt].to = y; head[x] = cnt; } inline void dfs1(int u, int f, int deep){ dep[u] = deep; fa[u] = f; siz[u] = 1; int maxson = -1; for (int i = head[u]; i; i = e[i].next){ int v = e[i].to; if (v == f) continue; dfs1(v, u, deep + 1); siz[u] += siz[v]; if (siz[v] > maxson) son[u] = v, maxson = siz[v]; } } inline void dfs2(int u, int topf){ id[u] = ++cnt; rk[cnt] = u; top[u] = topf; if (!son[u]) return; dfs2(son[u], topf); for (int i = head[u]; i; i = e[i].next){ int v = e[i].to; if (v == fa[u] || v == son[u]) continue; dfs2(v, v); } } inline void pushup(int x){ t[x].sum = (t[x<<1].sum + t[x<<1|1].sum); } void build(int l, int r, int rt){ t[rt].l = l, t[rt].r = r, t[rt].sum = t[rt].lazy = 0; if (l == r){ t[rt].sum = v[rk[l]]; return; } int mid = l + r >> 1; build(l, mid, rt<<1), build(mid + 1, r, rt<<1|1); pushup(rt); } inline int len(int x){ return t[x].r - t[x].l + 1; } inline void pushdown(int x){ if (t[x].lazy){ int ls = (x<<1), rs = (x<<1|1), lz = t[x].lazy; t[ls].lazy += lz; t[rs].lazy += lz; t[ls].sum += lz*(len(ls)); t[rs].sum += lz*(len(rs)); t[x].lazy = 0; } } void update(int l, int r, int c, int x){ if (t[x].l >= l&&t[x].r <= r){ t[x].lazy += c; t[x].sum += len(x)*c; return; } pushdown(x); int mid = (t[x].l + t[x].r) >> 1; if (mid >= l) update(l, r, c, x<<1); if (r > mid) update(l, r, c, x<<1|1); pushup(x); } int query(int l, int r, int rt){ if (t[rt].l >= l&&t[rt].r <= r) return t[rt].sum; pushdown(rt); int mid = (t[rt].l + t[rt].r) >> 1, tot = 0; if (mid >= l) tot += query(l, r, rt<<1); if (r > mid) tot += query(l, r, rt<<1|1); return tot; } inline int sum(int x, int y){ int ans = 0; while (top[x] != top[y]){ if (dep[top[x]] < dep[top[y]]) swap(x, y); ans += query(id[top[x]], id[x], 1); x = fa[top[x]]; }
if (id[x]>id[y]) swap(x, y); ans += query(id[x], id[y], 1); return ans; } inline void updates(int x, int y, int c){ while (top[x] != top[y]){ if (dep[top[x]] < dep[top[y]]) swap(x, y); update(id[top[x]], id[x], c, 1); x = fa[top[x]]; } if (id[x]>id[y]) swap(x, y); update(id[x], id[y], c, 1); } int main(){ scanf("%lld%lld%lld", &n, &m, &r); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]); for (int x, y, i = 1; i < n; i++){ scanf("%d%d", &x, &y); add(x, y), add(y, x); } cnt = 0; dfs1(r, 0, 1), dfs2(r, r); build(1, n, 1); for (int op, x, y, k, i = 1; i <= m; i++){ scanf("%d", &op); if (op == 1){ scanf("%d%d%d", &x, &y, &k); updates(x, y, k); }else if (op == 2){ scanf("%d%d", &x, &y); printf("%d\n", sum(x, y)); }else if (op == 3){ scanf("%d%d", &x, &y); update(id[x], id[x] + siz[x] - 1, y, 1); }else{ scanf("%d", &x); printf("%d\n", query(id[x], id[x] + siz[x] - 1, 1)); } } }
|