跳到正文

个人记录

剪切板

ZhuYF1029约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年2月10日
更新
2026年2月10日
篇幅
约 10 分钟

版本

  1. v1当前

https://www.luogu.com.cn/problem/P4719

没过样例

C++
//#pragma GCC optimize(2,3,"Ofast","inline")

#include <bits/stdc++.h>

#define int long long
//#define double long double
#define ll long long
#define db double
#define pii pair<int,int>
#define pdd pair<double,double>
#define fi first
#define se second
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define sz(s) ((int)s.size())
#define lowbit(x) (x&-x)
#define endl "\n"
#define y0 y114514
#define y1 y1919810
#define all(x) x.begin(),x.end()
#define pf(x) push_front(x)
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,l,r) for(int i=l;i>=r;i--)
#define gcd(x,y) __gcd(x,y)
#define lcm(x,y) ((x)/gcd(x,y)*(y))
#define sq(x) ((x)*(x))
#define rep_(i,l,r) for(int i=l;i<r;i++)
#define trav(a,x) for(auto& a : x)

using namespace std;

void inline ZakYF(int C);

signed main(){
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(time(0));

    //freopen("","r",stdin);
    //freopen("","w",stdout);

    int T=1,C=0;
    //cin >> T;
    //cin >> C;
    while(T--) ZakYF(C);

    return 0;
}

const int N=2e5+10,INF=123456789;

struct mat{
    int h,w,a[2][2];
    mat(){memset(a,0,sizeof(a));}
    mat operator * (const mat& oth)const{
        mat res;
        res.h=h,res.w=oth.w;
        rep_(i,0,h){
            rep_(j,0,w){
                rep_(k,0,oth.w){
                    res.a[i][k]=max(res.a[i][k],a[i][j]+oth.a[j][k]);
                    //cerr << res.a[i][k] << " " << a[i][j] << " " << oth.a[j][k] << endl;
                }
            }
        }
        return res;
    }
}tr[N<<2],b[N];

int n,m,a[N],fa[N],dfn[N],ind,top[N],dep[N],son[N],siz[N],len[N],dp[N][2],f[N][2];
vector<int> g[N];

void inline pushup(int u){
    tr[u]=tr[u*2]*tr[u*2+1];
}

void upd(int p,const mat& x,int l=1,int r=n,int u=1){
    if(l==r){
        tr[u]=x;
        return;
    }
    int mid=l+r>>1;
    if(p<=mid) upd(p,x,l,mid,u*2);
    else upd(p,x,mid+1,r,u*2+1);
    pushup(u);
}

mat qry(int fl,int fr,int l=1,int r=n,int u=1){
    if(fl<=l && r<=fr) return tr[u];
    int mid=l+r>>1;
    if(fr<=mid) return qry(fl,fr,l,mid,u*2);
    else if(fl>mid) return qry(fl,fr,mid+1,r,u*2+1);
    return  qry(fl,fr,l,mid,u*2)*qry(fl,fr,mid+1,r,u*2+1);
}

void dfs1(int u){
    siz[u]=1;
    dp[u][1]=a[u];
    for(int v:g[u]){
        if(v!=fa[u]){
            fa[v]=u;
            dep[v]=dep[u]+1;
            dfs1(v);
            dp[u][0]+=max(dp[v][0],dp[v][1]);
            dp[u][1]+=dp[v][0];
            f[u][1]+=max(dp[v][0],dp[v][1]);
            f[u][0]+=dp[v][0];
            siz[u]+=siz[v];
            if(!son[u] || siz[son[u]]<siz[v]) son[u]=v;
        }
    }
}

void dfs2(int u,int rt){
    top[u]=rt;
    dfn[u]=++ind;
    len[rt]++;
    if(son[u]){
        dfs2(son[u],rt);
        b[u].h=2,b[u].w=2,
        b[u].a[0][0]=b[u].a[0][1]=max(f[u][0],f[u][1]),
        b[u].a[1][0]=f[u][0]+a[u],b[u].a[1][1]=-INF;
    }else{
        b[u].h=2,b[u].w=1,
        b[u].a[0][0]=dp[u][0],b[u].a[1][0]=dp[u][1];
    }
    upd(dfn[u],b[u]);
    for(int v:g[u]){
        if(v!=son[u] && v!=fa[u]){
            dfs2(v,v);
        }
    }
    /*cerr << u << " : " << " " << b[u].h << " " << b[u].w << endl;
    rep_(i,0,b[u].h){
        rep_(j,0,b[u].w){
            cerr << b[u].a[i][j] << " ";
        }
        cerr << endl;
    }*/
}

void inline change(int u,int w){
    //cerr << "???" << " " << u << " " << top[u] << endl;
    //a[u]->w
    mat las=qry(dfn[top[u]],dfn[top[u]]+len[top[u]]-1);
    b[u].a[1][0]+=w-a[u];
    upd(dfn[u],b[u]);
    a[u]=w;
    mat ans=qry(dfn[1],dfn[1]+len[1]-1);
    cerr << max(ans.a[0][0],ans.a[1][0]) << endl;
    while(fa[top[u]]){
        int rt=top[u],pa=fa[rt];
        //cerr << u << " : " << " " << pa << endl;
        mat cur=qry(dfn[rt],dfn[rt]+len[rt]-1);
        //cerr << cur.a[0][0] << " " << cur.a[1][0] << endl;
        //cout << las.a[0][0] << " " << las.a[1][0] << " ??? " << cur.a[0][0]-las.a[0][0] << endl;
        //cout << pa << " " << f[pa][0] << " " << f[pa][1] << endl;
        f[pa][0]+=cur.a[0][0]-las.a[0][0];
        //cout << pa << " " << f[pa][0] << " " << f[pa][1] << " " << cur.a[0][0]-las.a[0][0] << endl;
        f[pa][1]+=cur.a[1][0]-las.a[1][0];
        /*rep(i,0,1){
            rep(j,0,1){
                cerr << b[pa].a[i][j] << " ";
            }
            cerr << endl;
        }
        cerr << endl;*/
        b[pa].a[0][0]=b[pa].a[0][1]=max(f[pa][0],f[pa][1]),
        b[pa].a[1][0]=f[pa][0];
        /*rep(i,0,1){
            rep(j,0,1){
                cerr << b[pa].a[i][j] << " ";
            }
            cerr << endl;
        }
        cerr << endl;*/
        upd(dfn[pa],b[pa]);
        u=pa;
        las=qry(dfn[top[pa]],dfn[top[pa]]+len[top[pa]]-1);
   }
}

void inline ZakYF(int C){
    cin >> n >> m;
    rep(i,1,n) cin >> a[i];
    rep(i,1,n-1){
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dep[1]=1;
    dfs1(1);
    dfs2(1,1);
    mat ans=qry(dfn[1],dfn[1]+len[1]-1);
    cout << ans.a[0][0] << " " << ans.a[1][0] << endl;
    rep(i,1,m){
        int x,y;
        cin >> x >> y;
        change(x,y);
        mat ans=qry(dfn[1],dfn[1]+len[1]-1);
        cout << max(ans.a[0][0],ans.a[1][0]) << endl;
    }
}
原文