LCT 求调
查看原帖
LCT 求调
749665
huta0楼主2024/7/9 16:37
#include <iostream>
#include <vector>
#define ai_jiang signed main()
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define drep(a,b,c) for(int a=b;a>=c;a--)
#define all(a) a.begin(),a.end()
#define ll long long
using namespace std;
using poly=vector<ll>;
constexpr int N = 1e6+5;
int n,m,a,b,w[N];
namespace ai {
    struct mat {
        int g[2][2];
        void init() {
            g[0][0]=g[0][1]=g[1][0]=g[1][1]=-2e7;
        }
        mat operator*(mat b) {
            mat c; c.init();
            rep(i,0,1) {
                rep(j,0,1) {
                    rep(k,0,1) {
                        c.g[i][j] = max(c.g[i][j], g[i][k] + b.g[k][j]);
                    } 
                }
            } return c;
        }
    } mt[N];
    struct node {
        int p,tag;
        int s[2];
        mat v;
    } tr[N];
    #define fa(x) tr[x].p
    #define lc(x) tr[x].s[0]
    #define rc(x) tr[x].s[1]
    #define ntrt(x) lc(fa(x))==x||rc(fa(x))==x
    void pushup(int x) {
        tr[x].v=mt[x];
        if(lc(x)) tr[x].v=tr[x].v*tr[lc(x)].v;
        if(rc(x)) tr[x].v=tr[x].v*tr[rc(x)].v;
    }
    void pushdown(int x) {
        if(tr[x].tag) {
            swap(lc(x),rc(x));
            tr[x].tag=0;
            tr[lc(x)].tag^=1;
            tr[rc(x)].tag^=1;
        }
    }
    void pushall(int x) {
        if(ntrt(x)) pushall(fa(x));
        pushdown(x);
    }
    void rotate(int x) {
        int y=fa(x), z=fa(y);
        int k=rc(y)==x;
        if(ntrt(y)) tr[z].s[rc(z)==y]=x;
        fa(x)=z;
        tr[y].s[k]=tr[x].s[k^1];
        fa(tr[x].s[k^1])=y;
        tr[x].s[k^1]=y;
        fa(y)=x;
        pushup(y); pushup(x);
    }
    void splay(int x) {
        pushall(x);
        while(ntrt(x)) {
            int y=fa(x), z=fa(y);
            if(ntrt(y)) rotate((rc(y)==x)^(rc(z)==y)?x:y);
            rotate(x);
        }
    }
    void access(int x) {
        int y=0;
        while(x) {
            splay(x);
            if(rc(x)) {
                tr[x].v.g[0][0]+=max(tr[rc(x)].v.g[0][0],tr[rc(x)].v.g[1][0]);
                tr[x].v.g[1][0]+=tr[rc(x)].v.g[0][0];
                tr[x].v.g[0][1]=tr[x].v.g[0][0];
            }
            if(y) {
                tr[x].v.g[0][0]-=max(tr[y].v.g[0][0],tr[y].v.g[1][0]);
                tr[x].v.g[1][0]-=tr[y].v.g[0][0];
                tr[x].v.g[0][1]=tr[x].v.g[0][0];
            }
            rc(x)=y;
            pushup(x);
            y=x; x=fa(x);
        }
    }
    void split(int x, int y) {
        access(x); splay(x); access(y); splay(y);
    }
    int fdrt(int x) {
        access(x); splay(x);
        while(lc(x)) pushdown(x),x=lc(x);
        splay(x);
        return x;
    }
    void link(int x,int y) {
        access(x); splay(x);
        if(fdrt(y)!=x) fa(x)=y;
    }
    void modify(int x,int ww) {
        access(x); splay(x); 
        mt[x].g[1][0]+=ww-w[x]; pushup(x); w[x]=ww;
    }
    vector<int> e[N];
    int f[N][2];
    void dfs(int x,int fa) {
        f[x][1]=w[x];
        for(auto v:e[x]) {
            if(v==fa) continue;
            fa(v)=x;
            dfs(v,x);
            f[x][0]+=max(f[v][0],f[v][1]);
            f[x][1]+=f[v][0];
        }
        mt[x].g[0][0]=mt[x].g[0][1]=f[x][0]; mt[x].g[1][0]=f[x][1];
    }
} 
using namespace ai;
ai_jiang {
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    rep(i,1,n) cin>>w[i],mt[i].init();
    rep(i,1,n-1) {
        cin>>a>>b;
        //link(a,b); 
        e[a].push_back(b); e[b].push_back(a);
    }
    dfs(1,0);
    rep(i,1,m) {
        cin>>a>>b;
        modify(a,b); splay(1);
        cout<<max(tr[1].v.g[0][0],tr[1].v.g[1][0])<<'\n';
    }
    return 0;
}
2024/7/9 16:37
加载中...