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
| #include <cstdio> #include <vector> using namespace std; 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'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <class Type> void chkmin (Type &x, Type k) { if (k < x) x = k; } template <class Type> void chkmax (Type &x, Type k) { if (k > x) x = k; } typedef long long LL; const int N = 1e5 + 10; const LL inf = 1e10; vector <int> g[N]; LL w[N], f[N][2]; int n, m, p[N], sz[N]; int stmp, dfn[N], rnk[N], son[N], top[N], ed[N]; struct Matrix { LL w[2][2]; Matrix () { w[0][0] = w[0][1] = w[1][0] = w[1][1] = inf; } friend Matrix operator * (Matrix a, Matrix b) { Matrix res; for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) for (int k = 0; k < 2; ++k) chkmin(res.w[i][j], a.w[i][k] + b.w[k][j]); return res; } } v[N]; struct Node { int l, r; Matrix w; } tr[N << 2]; void pushup (int x) { tr[x].w = tr[x << 1].w * tr[x << 1 | 1].w; } void build (int l = 1, int r = n, int x = 1) { tr[x].l = l, tr[x].r = r; if (l == r) return void(tr[x].w = v[rnk[l]]); int mid = l + r >> 1; build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1); pushup(x); } void modify (int t, int x = 1) { if (tr[x].l == tr[x].r) return void(tr[x].w = v[rnk[t]]); int mid = tr[x].l + tr[x].r >> 1; modify(t, t <= mid ? x << 1 : x << 1 | 1); pushup(x); } Matrix query (int l, int r, int x = 1) { if (tr[x].l >= l && tr[x].r <= r) return tr[x].w; int mid = tr[x].l + tr[x].r >> 1; if (r <= mid) return query(l, r, x << 1); if (l > mid) return query(l, r, x << 1 | 1); return query(l, r, x << 1) * query(l, r, x << 1 | 1); } void dfs1 (int x) { sz[x] = 1; for (int i : g[x]) if (i ^ p[x]) { p[i] = x; dfs1(i); sz[x] += sz[i]; if (sz[i] > sz[son[x]]) son[x] = i; } } void dfs2 (int x, int t) { rnk[dfn[x] = ++stmp] = x; top[x] = t; chkmax(ed[t], stmp); f[x][0] = 0, f[x][1] = w[x]; v[x].w[0][1] = 0, v[x].w[1][0] = v[x].w[1][1] = w[x]; if (!son[x]) return; dfs2(son[x], t); f[x][0] += f[son[x]][1], f[x][1] += min(f[son[x]][0], f[son[x]][1]); for (int i : g[x]) if (i ^ p[x] && i ^ son[x]) { dfs2(i, i); f[x][0] += f[i][1], f[x][1] += min(f[i][0], f[i][1]); v[x].w[0][1] += f[i][1]; v[x].w[1][0] += min(f[i][0], f[i][1]); v[x].w[1][1] = v[x].w[1][0]; } } void ModifyPath (int x, LL k) { v[x].w[1][0] += k - w[x], v[x].w[1][1] = v[x].w[1][0], w[x] = k; while (x) { Matrix s = query(dfn[top[x]], ed[top[x]]); modify(dfn[x]); Matrix t = query(dfn[top[x]], ed[top[x]]); x = p[top[x]]; v[x].w[0][1] += t.w[1][1] - s.w[1][1]; v[x].w[1][0] += min(t.w[0][1], t.w[1][1]) - min(s.w[0][1], s.w[1][1]); v[x].w[1][1] = v[x].w[1][0]; } } int main () { read(n, m); scanf("%*s"); for (int i = 1; i <= n; ++i) read(w[i]); for (int i = 1, a, b; i < n; ++i) { read(a, b); g[a].push_back(b), g[b].push_back(a); } dfs1(1), dfs2(1, 1), build(); for (int a, x, b, y; m; --m) { read(a, x, b, y); LL s = w[a], t = w[b], res = 0; x ? (res += s, ModifyPath(a, 0)) : ModifyPath(a, inf); y ? (res += t, ModifyPath(b, 0)) : ModifyPath(b, inf); Matrix k = query(dfn[1], ed[1]); res += min(k.w[0][1], k.w[1][1]); ModifyPath(a, s), ModifyPath(b, t); write(res >= inf ? -1 : res), puts(""); } return 0; }
|