P3313 solution

· · 题解

upd:补了一点说明。

走了一圈题解区怎么都是动态开点。

显然暴力可以对每个 C 开一个线段树来维护。但是这样空间会爆炸。我们尝试在同一棵线段树上完成所有操作。

单独考虑 C_u=x 的 u 组成的树。考虑一个 CC 操作实际上意味着什么。只有当切换 C_u 为 x 时点 u 才会被计入树 x 贡献的计算。

把一次 CC 操作拆成前后两部分:将 C_u 改为 0,将 C_u 改为 y。也就是说,我们在 C_u 树上去除了点 u 对应的 w_u 权值(可以看作设为 0),然后在 y 树上设置 w_u 权值。

两棵树之间是没有关系的,所以先把题目里面的更改操作跑一遍,记录什么时候某棵树上进行了一次询问或将某个点的权值改为 y,按时间顺序把操作记录到每棵树的操作列表,依次处理每棵树。由于 1 次 CC 操作只能拆出 2 个赋值操作,再加上起始时加入树和结束时退出树的赋值,最终的操作数量是 \mathcal O(n+q) 的。一次跳树链求答案复杂度是 \mathcal O(\log^2 n),赋值操作的复杂度是 \mathcal O(\log n),最终复杂度是 \mathcal O(n\log n+q\log^2 n)。

:::info[P3313]

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ir(i,a,b) for(int i=b;i>=a;i--)
#define db double
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define re return
#define len(str) (str.length())
#define inr(L,R,l,r) (l<=L and R<=r)
#define ofr(L,R,l,r) (L>r or l>R)
#define lowbit(x) (x&(-x))
#define tn2 tuple<node*,node*>
#define tn3 tuple<node*,node*,node*>
#define mt make_tuple
#define np nullptr
#define ioo cin.tie(0)->sync_with_stdio(0);cout.tie(0)->sync_with_stdio(0);
#define popc __builtin_popcount
using namespace std;
const int maxn=1e5+114;
struct Node
{
    int w,mx;
    Node()
    {
        w=mx=0;
    }
} t[maxn<<2];
struct Opt
{
    int typ,x,y,qid;
    Opt(int tt,int xx,int yy,int qq): typ(tt),x(xx),y(yy),qid(qq){}
};
vector<Opt> opt[maxn];
int w[maxn],c[maxn],n,q;
int head[maxn],nxt[maxn*2],to[maxn*2],tot;
bool isq[maxn];int ans[maxn];
int dfn[maxn],dfl[maxn],wc[maxn],siz[maxn],dep[maxn],top[maxn],fu[maxn],ts;
bool vis[maxn];
void add(int u,int v)
{
    nxt[++tot]=head[u];head[u]=tot;
    to[tot]=v;
}
inline void dfs(int u,int fa)
{
    siz[u]=1;dep[u]=dep[fa]+1;fu[u]=fa;
    for(int i=head[u],v;i;i=nxt[i])
    {
        v=to[i];
        if(v==fa) continue;
        dfs(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[wc[u]]) wc[u]=v;
    }
}
inline void chain(int u,int Top)
{
    top[u]=Top;dfn[u]=++ts;dfl[ts]=u;
    if(wc[u]) chain(wc[u],Top);
    for(int i=head[u],v;i;i=nxt[i])
    {
        v=to[i];
        if(v==fu[u] or v==wc[u]) continue;
        chain(v,v);
    }
}
Node pushup(Node a,Node b)
{
    Node c;
    c.w=a.w+b.w;
    c.mx=max(a.mx,b.mx);
    re c;
}
inline void update(int u,int L,int R,int k,int v)
{
    if(L==R)
    {
        t[u].w=t[u].mx=v;
    }else{
        int M=L+R>>1;
        if(k<=M) update(2*u,L,M,k,v);
        else update(2*u+1,M+1,R,k,v);
        t[u]=pushup(t[2*u],t[2*u+1]);
    }
}
inline int qmax(int u,int L,int R,int l,int r)
{
    if(inr(L,R,l,r)) re t[u].mx;
    else 
    {
        int M=L+R>>1,ans=0;
        if(l<=M) ans=max(ans,qmax(2*u,L,M,l,r));
        if(r>M) ans=max(ans,qmax(2*u+1,M+1,R,l,r));
        re ans;
    }
}
inline int qsum(int u,int L,int R,int l,int r)
{
    if(inr(L,R,l,r)) re t[u].w;
    else{
        int M=L+R>>1,ans=0;
        if(l<=M) ans=ans+qsum(2*u,L,M,l,r);
        if(r>M) ans=ans+qsum(2*u+1,M+1,R,l,r);
        re ans;
    }
}
inline int chainsum(int u,int v)
{
    int ans=0;
    while(top[u]!=top[v])
    {
        if(dep[top[u]]>dep[top[v]]) ans=ans+qsum(1,1,n,dfn[top[u]],dfn[u]),u=fu[top[u]];
        else ans=ans+qsum(1,1,n,dfn[top[v]],dfn[v]),v=fu[top[v]];
    }
    ans=ans+qsum(1,1,n,min(dfn[u],dfn[v]),max(dfn[u],dfn[v]));
    re ans;
}
inline int chainmax(int u,int v)
{
    int ans=0;
    while(top[u]!=top[v])
    {
        if(dep[top[u]]>dep[top[v]]) ans=max(ans,qmax(1,1,n,dfn[top[u]],dfn[u])),u=fu[top[u]];
        else ans=max(ans,qmax(1,1,n,dfn[top[v]],dfn[v])),v=fu[top[v]];
    }
    ans=max(ans,qmax(1,1,n,min(dfn[u],dfn[v]),max(dfn[u],dfn[v])));
    re ans;
}
int main()
{
    ioo;
    cin>>n>>q;
    rep(i,1,n) 
    {
        cin>>w[i]>>c[i];
        opt[c[i]].push_back(Opt(1,i,w[i],0));
    }
    rep(i,1,n-1)
    {
        int u,v;
        cin>>u>>v;
        add(u,v);add(v,u);
    }
    rep(i,1,q)
    {
        char p,q;int x,y;
        cin>>p>>q>>x>>y;
        if(q=='C')
        {
            opt[c[x]].push_back(Opt(1,x,0,i));
            c[x]=y;
            opt[y].push_back(Opt(1,x,w[x],i));
        }else if(q=='W')
        {
            opt[c[x]].push_back(Opt(1,x,y,i));
            w[x]=y;
        }else if(q=='S')
        {
            isq[i]=1;
            opt[c[x]].push_back(Opt(2,x,y,i));
        }else
        {
            isq[i]=1;
            opt[c[x]].push_back(Opt(3,x,y,i));
        }
    }
    rep(i,1,n) opt[c[i]].push_back(Opt(1,i,0,n+1));
    dfs(1,0);
    chain(1,1);
    rep(i,1,100000)
    {
        if(opt[i].empty()) continue;
        for(auto o:opt[i])
        {
            int typ,x,y,qid;
            typ=o.typ;x=o.x;y=o.y;qid=o.qid;
            if(typ==1)
            {
                update(1,1,n,dfn[x],y);
            }else if(typ==2)
            {
                ans[qid]=chainsum(x,y);
            }else{
                ans[qid]=chainmax(x,y);
            }
        }
    }
    rep(i,1,q) if(isq[i]) cout<<ans[i]<<'\n';
}

:::