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
| #include <cstdio> #include <vector> using namespace std; typedef long long LL; 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 << 3) + (x << 1) + 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(x % 10 + '0'); } const int N = 1e5 + 10; bool vis[N]; int n, m, w[N], p[N]; vector<int> g[N]; vector<LL> tr[N][2]; template <class Type> void chkmin(Type &x, Type k) { (x > k) && (x = k); } template <class Type> void chkmax(Type &x, Type k) { (x < k) && (x = k); } namespace LCA { const int K = 20; int d[N]; int stmp, id[N], st[N << 1][K], lg[N << 1]; void dfs(int x, int fa) { st[++stmp][0] = x; d[x] = d[fa] + 1; id[x] = stmp; for (int i : g[x]) if (i ^ fa) { dfs(i, x); st[++stmp][0] = x; } } int dmin(int x, int y) { return d[x] < d[y] ? x : y; } void init() { dfs(1, 0); for (int i = 2; i <= stmp; i++) lg[i] = lg[i >> 1] + 1; for (int k = 1; (1 << k) <= stmp; k++) for (int i = 1; i + (1 << k) - 1 <= stmp; i++) st[i][k] = dmin(st[i][k - 1], st[i + (1 << k - 1)][k - 1]); } int dis(int a, int b) { int x = id[a], y = id[b]; if (x > y) swap(x, y); int k = lg[y - x + 1], lca = dmin(st[x][k], st[y - (1 << k) + 1][k]); return d[a] + d[b] - 2 * d[lca]; } } int Size(int x, int fa) { if (vis[x]) return 0; int res = 1; for (int i : g[x]) i ^ fa && (res += Size(i, x)); return res; } int WeightCentre(int x, int fa, int tot, int &wc) { if (vis[x]) return 0; int sum = 1, mx = 0; for (int i : g[x]) if (i != fa) { int t = WeightCentre(i, x, tot, wc); chkmax(mx, t); sum += t; } chkmax(mx, tot - sum); mx <= tot / 2 && (wc = x); return sum; } void modify(int x, int k, vector<LL> &tr) { for (x++; x < tr.size(); x += x & -x) tr[x] += k; } LL query(int x, vector<LL> &tr) { LL res = 0; for (chkmin(++x, (int)tr.size() - 1); x; x -= x & -x) res += tr[x]; return res; } void calc(int x) { if (vis[x]) return; int sz = Size(x, 0); WeightCentre(x, -1, sz, x); vis[x] = true; tr[x][0].resize(sz + 2), tr[x][1].resize(sz + 2); for (int i : g[x]) if (!vis[i]) { int t; WeightCentre(i, 0, Size(i, -1), t); p[t] = x; } for (int i : g[x]) calc(i); } void change(int x, int k) { for (int i = x; i; i = p[i]) modify(LCA::dis(x, i), k, tr[i][0]); for (int i = x; p[i]; i = p[i]) modify(LCA::dis(x, p[i]), k, tr[i][1]); } int main() { read(n), read(m); for (int i = 1; i <= n; i++) read(w[i]); for (int i = 1, a, b; i < n; i++) { read(a), read(b); g[a].push_back(b); g[b].push_back(a); } LCA::init(); calc(1); for (int i = 1; i <= n; i++) change(i, w[i]); for (int op, last = 0; m; m--) { read(op); if (!op) { int x, d; read(x), read(d); x ^= last, d ^= last; LL res = 0; res += query(d, tr[x][0]); for (int i = x; p[i]; i = p[i]) { int k = LCA::dis(x, p[i]); if (d >= k) res += query(d - k, tr[p[i]][0]) - query(d - k, tr[i][1]); } write(last = res), puts(""); } else { int x, k; read(x), read(k); x ^= last, k ^= last; change(x, k - w[x]); w[x] = k; } } return 0; }
|