咕咕咕掉。。。。
什么玩意啊。。。
自闭ing
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
int n,m,h[MAXN],tot,a[MAXN];
int f[MAXN],dep[MAXN],dfn[MAXN],dex,sz[MAXN],ch[MAXN],tp[MAXN],id[MAXN];
int ycl[MAXN][2],ans[2];
struct node{
int from,to,next;
}e[MAXN << 1];
struct Ma{
int o[2][2];
}g[MAXN],sum[MAXN * 5];
Ma operator * (Ma x , Ma y){
Ma z;
z.o[0][0] = max(x.o[0][0] + y.o[0][0] , x.o[0][1] + y.o[1][0]);
z.o[0][1] = max(x.o[0][0] + y.o[0][1] , x.o[0][1] + y.o[1][1]);
z.o[1][0] = max(x.o[1][0] + y.o[0][0] , x.o[1][1] + y.o[1][0]);
z.o[1][1] = max(x.o[1][0] + y.o[0][1] , x.o[1][1] + y.o[1][1]);
return z;
}
void add(int x , int y){
tot++;
e[tot].from = x;
e[tot].to = y;
e[tot].next = h[x];
h[x] = tot;
}
void dfs(int now , int fa){
f[now] = fa;
sz[now] = 1;
dep[now] = dep[fa] + 1;
ycl[now][1] = a[now];
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(e[i].to == fa)continue;
dfs(e[i].to , now);
ycl[now][1] = ycl[now][1] + ycl[e[i].to][0];
ycl[now][0] = ycl[now][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
sz[now] = sz[now] + sz[e[i].to];
if(sz[ch[now]] < sz[e[i].to])ch[now] = e[i].to;
}
}
void dfs2(int now , int fa){
dfn[now] = ++dex;
id[dex] = now;
if(ch[now]){
tp[ch[now]] = tp[now];
dfs2(ch[now] , now);
}
g[dfn[now]].o[0][1] = a[now];
g[dfn[now]].o[1][1] = -99999999;
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(e[i].to == fa)continue;
if(e[i].to == ch[now])continue;
tp[e[i].to] = e[i].to;
g[dfn[now]].o[0][0] = g[dfn[now]].o[0][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
g[dfn[now]].o[1][0] = g[dfn[now]].o[1][0] + max(ycl[e[i].to][0] , ycl[e[i].to][1]);
g[dfn[now]].o[0][1] = g[dfn[now]].o[0][1] + ycl[e[i].to][0];
dfs2(e[i].to , now);
}
}
void build(int rt , int l , int r){
if(l == r){
sum[rt] = g[l];
return;
}
int mid = (l + r) >> 1;
build(rt << 1 , l , mid);
build((rt << 1) | 1 , mid + 1 , r);
sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
return;
}
void init(){
memset(ycl , 0 , sizeof(ycl));
memset(g , 0 , sizeof(g));
memset(h , -1 , sizeof(h));
tot = dex = 0;
cin>>n>>m;int x,y;
for(int i = 1 ; i <= n ; i++)cin>>a[i];
for(int i = 1 ; i < n ; i++){
cin>>x>>y;
add(x , y) , add(y , x);
}
tp[1] = 1;
dfs(1 , 1);
dfs2(1 , 1);
build(1 , 1 , n);
}
void update(int rt , int l , int r , int x , int y){
if(x < l || x > r)return;
if(l == r){
sum[rt].o[0][1] = sum[rt].o[0][1] - a[id[l]];
sum[rt].o[0][1] = sum[rt].o[0][1] + y;
a[id[l]] = y;
return;
}
int mid = (l + r) >> 1;
update(rt << 1 , l , mid , x , y);
update((rt << 1) | 1 , mid + 1 , r , x , y);
sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
return;
}
void update2(int rt , int l , int r , int x){
if(x < l || x > r)return;
if(l == r){
sum[rt].o[0][0] = ans[0];
sum[rt].o[1][0] = ans[0];
sum[rt].o[0][1] = ans[1] + a[id[l]];
return;
}
int mid = (l + r) >> 1;
update2(rt << 1 , l , mid , x);
update2((rt << 1) | 1 , mid + 1 , r , x);
sum[rt] = sum[(rt << 1) | 1] * sum[rt << 1];
return;
}
void que(int rt , int l , int r , int x , int y){
if(r < x || l > y)return;
if(x <= l && r <= y){
int zz1 = ans[0] , zz2 = ans[1];
ans[0] = max(sum[rt].o[0][0] + zz1 , sum[rt].o[0][1] + zz2);
ans[1] = max(sum[rt].o[1][0] + zz1 , sum[rt].o[1][1] + zz2);
return;
}
int mid = (l + r) >> 1;
que(rt << 1 , l , mid , x , y);
que((rt << 1) | 1 , mid + 1 , r , x , y);
return;
}
void sp_update(int x , int y){
int now = x;
update(1 , 1 , n , dfn[x] , y);
while(tp[now] != 1){
ans[0] = ans[1] = 0;
que(1 , 1 , n , dfn[tp[x]] , dfn[x]);
update2(1 , 1 , n , dfn[f[tp[x]]]);
now = f[tp[x]];
}
}
void solve(){
int x,y;
while(m--){
cin>>x>>y;
}
}
int main(){
init();
solve();
}