CF2277F
statement
You are given a tree with vertices, numbered from to . Initially, vertex contains tokens.
Suppose the tree is rooted at some vertex . An operation on a vertex moves all tokens currently located in the subtree of to vertex . The cost of this operation is the number of tokens at vertex after the move.
You are also given a permutation of the integers from to . The weight of is the total cost of performing the operations on vertices in this order.
For each from to , find the weight of when the tree is rooted at vertex . For each , the operations are performed independently, starting from the initial placement of tokens. Since the answers might be large, output them modulo .
A subtree of vertex is the subgraph consisting of , all its descendants, and all the edges between them.
solution
显然每次操作一个节点时其产生的贡献只能为 或
考虑根为 时什么情况下 会给 产生贡献,当且仅当 比它所有祖先都早被操作,也就是说 的所有祖先节点在 中的位置都在 之后。
考虑把序列 从后往前加点并维护连通块,当加入点 后,容易发现联通块中任意点为根 均能对其造成贡献,于是我们只需要实现一个并查集联通块的全局加即可,可以根据并查集的构建过程构造一棵重构树,操作等价于子树加。
code
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
设全集为 。
一次操作选择 , ,实际上会使集合
中的每个位置的数量增加 。
考虑一棵满二叉字典树,深度为 ,每个叶子对应一个编号。
对于一次操作:
考虑 的最高位的 。
设这一位为第 位,那么 的低 位可以任意变化,而操作在对应的字典树中具有一个非常重要的性质:
如果一次操作同时涉及一个节点的左右子树,那么它必须完整覆盖其中一棵子树。
考虑字典树上的 ,定义 表示祖先对 子树进行了 次全体 后要满足条件还需的最小操作次数, 表示 子树内 的最小值,也就是祖先节点对整棵子树进行 操作的最大次数。
记 , 表示 的左右子树。
考虑转移,对于左子树,我们需要操作 次,其中有 次操作不能通过左子树全局加的方式与右子树配对完成,右子树同理。
记 。
我们贪心地将 和 先操作掉,并尽量与对方子树能全局配对的配对。
然后写出来就是一堆 max min 乱套,反正你就能得出
然后容易注意到
于是就只需要维护 和 就行,直接 即可。
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";
}