#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100010;
int n, m, fa[maxn], ch[maxn][2];
ll p[maxn], dp[maxn][2];
vector<int> G[maxn];
struct mat { ll a, b, c, d; mat() { a = b = c = d = 1e18; } } f[maxn], g[maxn];
bool get(int v) { return v == ch[fa[v]][1]; }
bool is_root(int v) { return ch[fa[v]][0] ^ v && ch[fa[v]][1] ^ v; }
mat operator * (mat &A, mat &B) {
mat C;
C.a = min(A.a + B.a, A.b + B.c), C.b = min(A.a + B.b, A.b + B.d);
C.c = min(A.c + B.a, A.d + B.c), C.d = min(A.c + B.b, A.d + B.d);
return C;
}
void dfs(int v) {
dp[v][1] = p[v];
for (int i = 0, u; i < G[v].size(); i++) {
if ((u = G[v][i]) ^ fa[v]) {
fa[u] = v, dfs(u);
dp[v][0] += dp[u][1], dp[v][1] += min(dp[u][0], dp[u][1]);
}
}
g[v].b = dp[v][0], g[v].c = dp[v][1];
}
void maintain(int v) {
g[v].d = g[v].c, f[v] = g[v];
if (ch[v][0]) f[v] = f[ch[v][0]] * f[v];
if (ch[v][1]) f[v] = f[v] * f[ch[v][1]];
}
void rotate(int v) {
int old = fa[v], oldf = fa[old], chk = get(v);
if (!is_root(old)) ch[oldf][get(old)] = v;
ch[old][chk] = ch[v][chk ^ 1], fa[ch[old][chk]] = old;
ch[v][chk ^ 1] = old, fa[old] = v, fa[v] = oldf;
maintain(old), maintain(v);
}
void splay(int v) {
for (int f = fa[v]; !is_root(v); rotate(v), f = fa[v]) {
if (!is_root(f)) rotate(get(v) ^ get(f) ? v : f);
}
}
void access(int v) {
for (int u = 0, t; v; v = fa[u = v]) {
splay(v);
if (t = ch[v][1]) g[v].b += f[t].d, g[v].c += min(f[t].b, f[t].d);
if (ch[v][1] = u) g[v].b -= f[u].d, g[v].c -= min(f[u].b, f[u].d);
maintain(v);
}
}
void upd(int v, ll w) {
access(v), splay(v), g[v].c += w - p[v];
p[v] = w, maintain(v);
}
int main() {
scanf("%d %d %*s", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lld", &p[i]);
}
for (int i = 1, u, v; i < n; i++) {
scanf("%d %d", &u, &v);
G[u].push_back(v), G[v].push_back(u);
}
dfs(1);
for (int i = 1, a, b, x, y; i <= m; i++) {
scanf("%d %d %d %d", &a, &x, &b, &y);
ll olda = p[a], oldb = p[b], ans = 0;
if (x) upd(a, 0), ans += olda; else upd(a, 2e15);
if (y) upd(b, 0), ans += oldb; else upd(b, 2e15);
splay(1), ans += min(f[1].b, f[1].d);
printf("%lld\n", ans > 1e15 ? -1 : ans);
upd(a, olda), upd(b, oldb);
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.097 ms | 8 MB + 460 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 1.082 ms | 8 MB + 460 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 1.087 ms | 8 MB + 460 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 1.074 ms | 8 MB + 460 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 1.243 ms | 8 MB + 476 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 1.229 ms | 8 MB + 476 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 1.242 ms | 8 MB + 468 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 5.53 ms | 8 MB + 764 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 5.493 ms | 8 MB + 764 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 5.248 ms | 8 MB + 676 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 5.259 ms | 8 MB + 676 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 218.752 ms | 23 MB + 608 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 218.587 ms | 23 MB + 608 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 300.257 ms | 23 MB + 412 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 300.201 ms | 23 MB + 416 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 300.459 ms | 23 MB + 416 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 352.531 ms | 23 MB + 608 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 112.757 ms | 16 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 112.723 ms | 16 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 197.154 ms | 19 MB + 452 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 197.361 ms | 19 MB + 452 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 236.404 ms | 19 MB + 256 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 321.531 ms | 19 MB + 448 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 321.694 ms | 19 MB + 452 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 322.007 ms | 19 MB + 464 KB | Accepted | Score: 4 | 显示更多 |