// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
typedef long long int_t;
const int_t maxn = 100010;
const int_t inf = 2147483647ll * 192608;
#define lson son[rt][0]
#define rson son[rt][1]
struct matrix{
int_t mat[3][3];
matrix(){mat[1][1] = mat[1][2] = mat[2][1] = mat[2][2] = -inf;}
int_t MAX(){return max(mat[1][1],mat[2][1]);}
matrix operator *(matrix b){
matrix c;
for(int_t i=1;i<=2;i++)
for(int_t j=1;j<=2;j++)
for(int_t k=1;k<=2;k++)
c.mat[i][j] = max(c.mat[i][j],mat[i][k] + b.mat[k][j]);
return c;
}
};
matrix mat[maxn];
int_t f[maxn][2],fa[maxn],val[maxn],son[maxn][2];
vector<int_t> G[maxn];
void pushup(int_t rt){
mat[rt] = matrix();
mat[rt].mat[1][1] = mat[rt].mat[1][2] = f[rt][0];
mat[rt].mat[2][1] = f[rt][1];
if(lson) mat[rt] = mat[lson] * mat[rt];
if(rson) mat[rt] = mat[rt] * mat[rson];
}
bool isroot(int_t rt) { return son[fa[rt]][0] != rt && son[fa[rt]][1] != rt; }
void rotate(int_t rt){
int_t fat = fa[rt], gfa = fa[fat], rs = son[fat][1] == rt, mus = son[rt][!rs];
if(!isroot(fat)) son[gfa][son[gfa][1] == fat] = rt; son[rt][!rs] = fat; son[fat][rs] = mus;
if(mus) fa[mus] = fat; fa[rt] = gfa; fa[fat] = rt;
pushup(fat); pushup(rt);
}
void splay(int_t rt){
while(!isroot(rt)){
int_t fat = fa[rt],gfa = fa[fat];
if(!isroot(fat)) rotate(((son[fat][1] == rt) ^ (son[gfa][1] == fat)) ? rt : fat);
rotate(rt);
}
}
void access(int_t rt){
for(int_t y = 0;rt;y = rt,rt = fa[rt]){
splay(rt);
if(rson)
f[rt][0] += mat[rson].MAX(),
f[rt][1] += mat[rson].mat[1][1];
if(y)
f[rt][0] -= mat[y].MAX(),
f[rt][1] -= mat[y].mat[1][1];
rson = y;
pushup(rt);
}
}
void init(int_t rt){
f[rt][1] = val[rt];
for(int_t to : G[rt]){
if(to == fa[rt]) continue;
fa[to] = rt;
init(to);
f[rt][0] += max(f[to][0],f[to][1]);
f[rt][1] += f[to][0];
}
mat[rt].mat[1][1] = mat[rt].mat[1][2] = f[rt][0];
mat[rt].mat[2][1] = f[rt][1];
}
int_t read(){
int_t x = 0,w = 1;char ch = 0;
while(!isdigit(ch)) {ch = getchar();if(ch == '-') w = -1;}
while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * w;
}
void modify(int_t x,int_t y){
access(x); splay(x);
f[x][1] += y; val[x] += y;
pushup(x);
}
int main(){
int_t n = read(),m = read(),res = 0; string str;cin>>str;
for(int_t i=1;i<=n;i++) val[i] = read(),res += val[i];
for(int_t i=1;i<n;i++){
int_t u = read(),v = read();
G[u].push_back(v);
G[v].push_back(u);
}
init(1);
while(m--){
int_t a = read(),x = read(),b = read(),y = read();
modify( a,x ? -inf : inf );
modify( b,y ? -inf : inf );
splay(1);
if(!x) res += inf; if(!y) res += inf;
int_t ans = res - mat[1].MAX();
printf("%lld\n",ans > inf ? -1 : ans);
if(!x) res -= inf; if(!y) res -= inf;
modify( a,x ? inf : -inf );
modify( b,y ? inf : -inf );
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.263 ms | 9 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 1.264 ms | 9 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 1.278 ms | 9 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 1.261 ms | 9 MB + 216 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 1.426 ms | 9 MB + 232 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 1.444 ms | 9 MB + 232 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 1.436 ms | 9 MB + 224 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 7.297 ms | 9 MB + 544 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 7.278 ms | 9 MB + 544 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 6.903 ms | 9 MB + 464 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 6.907 ms | 9 MB + 464 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 303.397 ms | 25 MB + 512 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 303.052 ms | 25 MB + 512 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 435.328 ms | 25 MB + 316 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 435.373 ms | 25 MB + 320 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 435.68 ms | 25 MB + 320 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 534.739 ms | 25 MB + 512 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 128.502 ms | 18 MB + 520 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 128.177 ms | 18 MB + 516 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 268.785 ms | 21 MB + 760 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 269.105 ms | 21 MB + 756 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 337.045 ms | 21 MB + 564 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 480.347 ms | 21 MB + 756 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 479.159 ms | 21 MB + 760 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 480.939 ms | 21 MB + 772 KB | Accepted | Score: 4 | 显示更多 |