跳到正文

个人记录

近期好题记录

Monotonic约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年10月30日
收录
2026年10月31日
更新
2026年11月10日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

CF2277F

statement

You are given a tree with nn vertices, numbered from 11 to nn. Initially, vertex ii contains aia_i tokens.

Suppose the tree is rooted at some vertex rr. An operation on a vertex uu moves all tokens currently located in the subtree∗^{\text{∗}} of uu to vertex uu. The cost of this operation is the number of tokens at vertex uu after the move.

You are also given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of the integers from 11 to nn. The weight of pp is the total cost of performing the operations on vertices p1,p2,…,pnp_1, p_2, \ldots, p_n in this order.

For each rr from 11 to nn, find the weight of pp when the tree is rooted at vertex rr. For each rr, the operations are performed independently, starting from the initial placement of tokens. Since the answers might be large, output them modulo 998 244 353998\,244\,353.

∗^{\text{∗}}A subtree of vertex uu is the subgraph consisting of uu, all its descendants, and all the edges between them.

solution

显然每次操作一个节点时其产生的贡献只能为 00 或 sumx=∑y∈subtree(x)axsum_x=\sum_{y\in subtree(x)}a_x

考虑根为 xx 时什么情况下 yy 会给 xx 产生贡献,当且仅当 xx 比它所有祖先都早被操作,也就是说 xx 的所有祖先节点在 pp 中的位置都在 xx 之后。

考虑把序列 pp 从后往前加点并维护连通块,当加入点 xx 后,容易发现联通块中任意点为根 xx 均能对其造成贡献,于是我们只需要实现一个并查集联通块的全局加即可,可以根据并查集的构建过程构造一棵重构树,操作等价于子树加。

code

C++
const int N = 5e5+10;
const int mod = 998244353;
int n,p[N],fa[N],ft[N];
ll a[N],sum[N],val[N];
vector<int> G[N],E[N];
void dfs(int x,int fa){
    ft[x] = fa;
    sum[x] = a[x];
    for(int y : G[x]){
        if(y == fa) continue;
        dfs(y,x);
        sum[x] += sum[y];
    }
    sum[x] %= mod;
}
void dfs2(int x){
    for(int y : E[x]){
        (val[y] += val[x]) %= mod;
        dfs2(y);
    }
}
int get(int x){
    return fa[x] == x ? x : fa[x] = get(fa[x]);
}
void merge(int x,int y){
    fa[get(x)] = get(y);
}
void solve(){
    n = read();
    for(int i=1;i<=n;i++){
        G[i].clear();
        E[i].clear();
        val[i] = 0;
    }
    for(int i=1;i<n;i++){
        int u = read(),v = read();
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i=1;i<=n;i++) a[i] = read();
    for(int i=1;i<=n;i++) p[i] = read();
    dfs(1,0);
    for(int i=1;i<=n;i++) fa[i] = 0;
    for(int i=n;i;i--){
        int x = p[i];
        fa[x] = x;
        for(int y : G[x]){
            if(!fa[y]) continue;
            if(y == ft[x]){
                y = get(y);
                (val[y] += sum[x]) %= mod;
            }else{
                ll t = (sum[1]+mod-sum[y])%mod;
                y = get(y);
                (val[y] += t) %= mod;
            }
            merge(y,x);
            E[x].push_back(y);
        }
    }
    dfs2(p[1]);
    for(int i=1;i<=n;i++) cout << (val[i]+sum[1])%mod << " ";
    cout << "\n";
}

The 2026 ICPC Asia Xi’an Regional Contest G

设全集为 U=0,1,…,2n−1U={0,1,\ldots,2^n-1}。

一次操作选择 mm, kk,实际上会使集合

S(m,k)={k⊕0,k⊕1,…,k⊕m} S(m,k)=\{k\oplus 0,k\oplus 1,\ldots,k\oplus m\}

中的每个位置的数量增加 11。

考虑一棵满二叉字典树,深度为 nn,每个叶子对应一个编号。

对于一次操作:

S(m,k)={x∣x⊕k≤m} S(m,k)= \{x\mid x\oplus k\le m \}

考虑 kk 的最高位的 11。

设这一位为第 pp 位,那么 kk 的低 pp 位可以任意变化,而操作在对应的字典树中具有一个非常重要的性质:

如果一次操作同时涉及一个节点的左右子树,那么它必须完整覆盖其中一棵子树。

考虑字典树上的 dp\text{dp},定义 fx,tf_{x,t} 表示祖先对 xx 子树进行了 tt 次全体 +1+1 后要满足条件还需的最小操作次数,mxm_x 表示 xx 子树内 aa 的最小值,也就是祖先节点对整棵子树进行 +1+1 操作的最大次数。

记 LL, RR 表示 xx 的左右子树。

考虑转移,对于左子树,我们需要操作 fL,tf_{L,t} 次,其中有 fL,mLf_{L,m_L} 次操作不能通过左子树全局加的方式与右子树配对完成,右子树同理。

记 A=fL,t,B=fR,t,c=fL,mL,d=fR,mRA = f_{L,t},B = f_{R,t},c = f_{L,m_L},d = f_{R,m_R}。

我们贪心地将 cc 和 dd 先操作掉,并尽量与对方子树能全局配对的配对。

然后写出来就是一堆 max min 乱套,反正你就能得出

fx,t=max⁡(A,B,c+d) f_{x,t} = \max(A,B,c+d)

然后容易注意到 fx,t=max⁡(fx,0−t,fx,mx)f_{x,t} = \max(f_{x,0}-t,f_{x,m_x})

于是就只需要维护 fx,0f_{x,0} 和 fx,mxf_{x,m_x} 就行,直接 dp\text{dp} 即可。

C++
const int N = 4e6+10;
int n,a[N],mn[N];
ll g[N],f[N];
#define ls (x<<1)
#define rs (x<<1|1)
void dfs(int x,int l,int r){
    if(l == r){
        mn[x] = a[l];
        g[x] = 0;
        f[x] = a[l];
        return ;
    }
    int mid = (l+r) >> 1;
    dfs(ls,l,mid);
    dfs(rs,mid+1,r);
    mn[x] = min(mn[ls],mn[rs]);
    f[x] = max(f[ls],max(f[rs],g[ls]+g[rs]));
    g[x] = max(f[ls]-mn[x],max(f[rs]-mn[x],g[ls]+g[rs]));
}
void solve(){
    n = read();
    for(int i=0;i<(1<<n);i++) a[i] = read();
    dfs(1,0,(1<<n)-1);
    cout << f[1] << "\n";
}
原文