跳到正文

个人记录

军训期间总结

__JCY__约 34 分钟
原文
文章信息与版本

文章信息

发布
2026年8月29日
收录
2026年8月30日
更新
2026年11月21日
篇幅
约 34 分钟

版本

  1. v1当前

8.24

一些比较难想到的树形 DP

P16961 [SCCPC 2026] 永恒的奥古斯都

该点能不能修改取决于自己能不能修改或者祖先节点已经进行了修改。

fi,0/1f_{i,0/1} 表示 从在根到 ii 为根的子树的路径上没有 / 有修改的方案数。

令 ii 的子节点为 vv。

fi,0=∏fv,0+∏fv,1[ax=1]f_{i,0} = \prod f_{v,0} + \prod f_{v,1} [a_x = 1].

fi,1=∏fv,1f_{i,1} = \prod f_{v,1}.

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = 3e5 + 5, Mod = 998244353;

int f[kMaxN][2], a[kMaxN], n;
vector<int> G[kMaxN];

void dfs(int x, int fa) {
	f[x][0] = (a[x] == 1), f[x][1] = 1;
	int s0 = 1; 
	for (auto v : G[x]) if (v != fa) {
		dfs(v, x);
		s0 = s0 * f[v][0] % Mod;
		f[x][0] = f[x][0] * f[v][1] % Mod; 
		f[x][1] = f[x][1] * f[v][1] % Mod;
	}
	f[x][0] = (f[x][0] + s0) % Mod; 
	f[x][1] = f[x][1] * 2 % Mod;
}

signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i < n; i++) {
		int x, y; cin >> x >> y;
		G[x].push_back(y), G[y].push_back(x);
	}
	dfs(1, 0);
	cout << f[1][0] << '\n';
	return 0;
}

P15734 [JAG 2024 Summer Camp #2] Flip Path on Rooted Tree

与上一题刚好相反,有思维难度。

很容易想到,该点操不操作仅于子树的操作次数有关系,但由于需要保证满足 aia_i 的条件。即操作次数奇偶性与 aia_i 相同。所以 fx=fx∏fvf_x = f_x \prod f_v。每次更新仅更新 xx 的答案和 faxfa_x 的答案。更新即可。

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = 2e5 + 5, INF = 1e9;

int f[kMaxN], Fa[kMaxN], a[kMaxN], n, ans;
vector<int> G[kMaxN];

void dfs(int x, int fa) {
	f[x] = a[x];
	for (auto v : G[x]) if (v != fa) {
		dfs(v, x);
		f[x] ^= a[v];
	}
}

signed main() {
	ios ::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 2; i <= n; i++) {
		int x; cin >> x; G[x].push_back(i), Fa[i] = x;
	}
	for (int i = 1; i <= n; i++) cin >> a[i];
	dfs(1, 0);
	for (int i = 1; i <= n; i++) ans = ans + f[i];
	int T;
	for (cin >> T; T; T--) {
		int x; cin >> x;
		ans += -f[x] + (f[x] ^ 1);
		f[x] ^= 1;
		if (Fa[x]) {
			ans += -f[Fa[x]] + (f[Fa[x]] ^ 1);
			f[Fa[x]] ^= 1;
		}
		cout << ans << '\n';
	}
	return 0;
} 

CF1626E Black and White Tree

并不容易想到换根。

当一个点为好点有几种情况。

  1. 自己是黑点或邻节点是黑点

  2. 通往好点的方向上有至少两个黑点。即能保证自己进入好点。

考虑换根,需要计算原本的根的贡献。若源根为好点且黑点大于等于2即可换过去。

code
C++
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 3e5 + 5;

int f[2][kMaxN], a[kMaxN], sz[kMaxN], tot, n;
vector<int> G[kMaxN];

void dfs1(int x, int fa) {
    for (auto v : G[x]) if (v != fa) {
        dfs1(v, x), sz[x] += sz[v];
        f[0][x] |= (a[v] || (sz[v] >= 2 && f[0][v]));
    }
}

void dfs2(int x, int fa) {
    for (auto v : G[x]) if (v != fa) {
        f[1][v] |= (f[0][v] | (f[1][x] && tot - sz[v] >= 2) | a[x]);
        dfs2(v, x);
    }
}

signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i], sz[i] = f[0][i] = a[i], tot += a[i];
    }
    for (int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        G[x].push_back(y), G[y].push_back(x);
    }
    dfs1(1, 0), f[1][1] = f[0][1], dfs2(1, 0);
    for (int i = 1; i <= n; i++) cout << f[1][i] << ' ';
    return 0; 
}

8.25~8.26

最近在补一下图论地方的知识空缺。这两天在学习点分治。

CF1575E Eye-Pleasing City Park Tour

一个比较难写的题目,码量比较大。点分治的思路显而易见。

对于一个新边,需要考虑是否要换成,因此记录换成次数和上一次边的颜色,使用 4 个树状数组维护贡献(分别维护方案数和点权和)。

注意从 rt 开头的路径不要忘记添加。

code
C++
#include <bits/stdc++.h>
#define int long long
#define Pii pair<int, int>
#define Fr first
#define Sc second
#define lowbit(x) (x & (-x))

using namespace std;

const int kMaxN = 2e5 + 5, INF = 1e18, Mod = 1e9 + 7;

struct E {
	int w, t, s;
};

int sz[kMaxN], ms[kMaxN], a[kMaxN], ans, rt, tot, mxp, n, k;
vector<Pii> G[kMaxN];
bool vis[kMaxN];
Pii tmp[kMaxN];
vector<E> GG;

struct BIT {
	int a[kMaxN]; 
	void update(int x, int v) {
		for (++x; x <= n; x += lowbit(x)) {
			a[x] += v;
		}
	}
	int query(int x, int ret = 0) {
		for (++x; x; x -= lowbit(x)) {
			ret += a[x];
		} 
		return ret;
	} 
} cnt[2], sum[2];

void getsize(int x, int fa) {
	sz[x] = 1;
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getsize(v.Fr, x), sz[x] += sz[v.Fr]; 
	} 
}

void getrt(int x, int fa) {
	int Mx = 0;
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getrt(v.Fr, x), Mx = max(sz[v.Fr], Mx);
	}
	Mx = max(tot - Mx, Mx);
	if (Mx < mxp) mxp = Mx, rt = x;
}

void getdis(int x, int fa, int d, int w, int op) {
	if (d > k) return;
	tmp[++tot] = {d, w};
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getdis(v.Fr, x, d + (v.Sc != op), (w + a[v.Fr]) % Mod, v.Sc);
	}
}

void solve(int x) {
	getsize(x, 0);
	tot = sz[x];
	mxp = INF, getrt(x, 0);
	for (auto r : G[rt]) if (!vis[r.Fr]) {
		int v = r.Fr, w = r.Sc;
		tot = 0;
		getdis(v, rt, 0, (a[rt] + a[v]) % Mod, w);
		for (int i = 1; i <= tot; i++) {
			if (tmp[i].Fr <= k) (ans += tmp[i].Sc) %= Mod;
			int cntt = (cnt[w].query(k - tmp[i].Fr) + cnt[w ^ 1].query(k - tmp[i].Fr - 1)) % Mod; 
			int summ = (sum[w].query(k - tmp[i].Fr) + sum[w ^ 1].query(k - tmp[i].Fr - 1)) % Mod; 
			ans = ((ans + cntt * ((tmp[i].Sc - a[rt] + Mod) % Mod) % Mod) % Mod + summ) % Mod;
		} 
		for (int i = 1; i <= tot; i++) {
			cnt[w].update(tmp[i].Fr, 1);
			sum[w].update(tmp[i].Fr, tmp[i].Sc);
			GG.push_back({w, tmp[i].Fr, tmp[i].Sc});
		}
	}
	vis[rt] = 1;
	for (auto v : GG) {
		cnt[v.w].update(v.t, -1);
		sum[v.w].update(v.t, -v.s);
	} 
	GG.clear();
	int cur = rt;
	for (auto v : G[cur]) if (!vis[v.Fr])  solve(v.Fr); 
} 

signed main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i], (ans += a[i]) %= Mod;
	for (int i = 1; i < n; i++) {
		int x, y, w; cin >> x >> y >> w;
		G[x].push_back({y, w}), G[y].push_back({x, w});
	}
	solve(1);
	cout << ans << '\n';
	return 0;
} 

CF1101D GCD Counting

这是一个想到了但是不会证明时间复杂度的题目。

对 gcd 有要求,可以转换为枚举因数。不同的因数个数最多只有 13,因此对于每个因数进行 getdis,若不含这个因数就直接退出。这样仅添加一个常数,时间复杂度依然为 O(nlog⁡n)O(n \log n)。

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 200005, K = 505, INF = 1e9;

int n, k, rt, Mx, tot, id, a[N], sz[N], ans;
vector<int> g[N];
bool vis[N];

vector<int> C(int x) {
    vector<int> ret;
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0) ret.push_back(i);
        for (; x % i == 0; x /= i);
    }
    if (x > 1) ret.push_back(x);
    return ret;
}

void getsize(int x, int fa) {
    sz[x] = 1;
    for (auto v : g[x]) if (v != fa && !vis[v]) {
        getsize(v, x);
        sz[x] += sz[v];
    }
}

void getrt(int x, int fa) {
    int mx = 0;
    for (auto v : g[x]) if (v != fa && !vis[v]) {
        getrt(v, x);
        mx = max(mx, sz[v]);
    }
    mx = max(mx, tot - sz[x]);
    if (mx < Mx) {
        Mx = mx, rt = x;
    }
}

int getdis(int x, int fa, int w) {
    int ret = 0;
    for (auto v : g[x]) if (v != fa && !vis[v] && a[v] % w == 0) {
        ret = max(ret, getdis(v, x, w));
    }
    return ret + 1;
}

int work(int x, int w) {
    int Mx = 0, Mxx = 0;
    for (auto v : g[x]) if (!vis[v] && (a[v] % w == 0)) {
        int r = getdis(v, x, w);
        if (r > Mx) Mxx = Mx, Mx = r;
        else if (r > Mxx) Mxx = r;
    }
    return Mx + Mxx;
}

void solve(int x) {
    getsize(x, 0);
    tot = sz[x], Mx = INF;
    getrt(x, 0);
    
    vector<int> ret = C(a[rt]);
    for (auto w : ret) {
        ans = max(ans, work(rt, w) + 1);
    }
    
    vis[rt] = 1;
    for (auto v : g[rt]) {
        if (!vis[v]) {
            solve(v);
        }
    }
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        int x, y; cin >> x >> y;
        g[x].push_back(y), g[y].push_back(x);
    }
    solve(1);
    cout << ans << '\n';
    return 0;
}

AT_cf17_final_j Tree MST

一个很有启发的题目。

显然,不能直接跑最小生成树,考虑减少边的数量。对于一个确定的根 rtrt,令子树内节点 xx 到 rtrt 距离为 pxp_x,对于 rtrt,找出最小的 qx=px+axq_x = p_x + a_x,设点为 uu。 则经过 rtrt 的路径 (x,y)(x, y)为 qx+qy−artq_x + q_y - a_{rt}.

假设同一子树内 x,yx, y 且 x≠u,y≠ux \ne u,y \ne u在最小生成树上有连边,则 (x,p)<(x,y)(x, p) < (x, y) 且 (y,p)<(x,y)(y, p) < (x, y)。在最小生成树的逻辑下,一定是链接 (x,p),(y,p)(x, p), (y, p) 而不是 (x,y)(x, y)。

因此对于每个子树找到 qxq_x 的最小的 xx 与子树内每个点连边再跑最短路。

code
C++
#include <bits/stdc++.h>
#define int long long
#define Pii pair<int, int>
#define Fr first
#define Sc second

using namespace std;

const int kMaxN = 2e5 + 5, INF = 1e18;

struct E {
	int x, y, w;
	friend bool operator<(const E A, const E B) {
		return A.w < B.w;
	}
};

int Fa[kMaxN];
int sz[kMaxN], ans[kMaxN], CNT[kMaxN], a[kMaxN], tot, cnt, mxp, mxx, mxi, rt, n, Ans;
bool vis[kMaxN];
vector<Pii> G[kMaxN];
vector<E> W;

int F(int x) {
	return Fa[x] = (Fa[x] == x ? x : F(Fa[x])); 
}

void getsize(int x, int fa) {
	sz[x] = 1;
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getsize(v.Fr, x), sz[x] += sz[v.Fr]; 
	}
} 

void getrt(int x, int fa) {
	int Mx = 0;
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getrt(v.Fr, x), Mx = max(Mx, sz[v.Fr]); 
	}
	Mx = max(Mx, tot - sz[x]);
	if (Mx < mxp) rt = x, mxp = Mx;
} 

void getdis(int x, int fa, int w) {
	if (w + a[x] < mxx) mxi = x, mxx = w + a[x];
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		getdis(v.Fr, x, w + v.Sc);
	}
}

void dfs(int x, int fa, int w) {
	if (x != mxi) W.push_back({x, mxi, w + a[x] + a[mxi]});
	for (auto v : G[x]) if (v.Fr != fa && !vis[v.Fr]) {
		dfs(v.Fr, x, w + v.Sc);
	}
} 

void solve(int x) {
	getsize(x, 0), tot = sz[x];
	mxp = INF, getrt(x, 0);
	mxi = rt, mxx = a[rt];
	for (auto r : G[rt]) if (!vis[r.Fr]) {
		int v = r.Fr, w = r.Sc;
		getdis(v, rt, w);
	}
	dfs(mxi, 0, 0);
	int cur = rt;
	vis[rt] = 1;
	for (auto v : G[cur]) if (!vis[v.Fr]) solve(v.Fr);
}

signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i], Fa[i] = i;
	}
	for (int i = 1; i < n; i++) {
		int x, y, w; cin >> x >> y >> w;
		G[x].push_back({y, w}), G[y].push_back({x, w});
		W.push_back({x, y, w + a[x] + a[y]});
	}
	solve(1);
	sort(W.begin(), W.end());
	for (auto v : W) {
		int x = v.x, y = v.y, w = v.w;
		if (F(x) != F(y)) {
			Fa[F(x)] = F(y);
			Ans += w;
		}
	}
	cout << Ans << '\n'; 
	return 0;
} 

8.27

AT_arc100_c [ARC100E] Or Plus Max

来自来追梦。并不是很会 SOS DP

转化为处理 (i or j)=K(i \ \text{or}\ j) = K 的问题,因此 i,ji,j 在二进制下为 KK 的子集。使用 SOS DP,fif_i 维护 ii 的子集中最大和次大的 aa。

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = (1 << 18) + 5, INF = 1e18;

struct E {
	int x, y;
	friend E operator+(const E &A, const E &B) {
		vector<int> s(4);
		s[0] = A.x, s[1] = A.y, s[2] = B.x, s[3] = B.y;
		sort(s.begin(), s.end());
		return E{s[2], s[3]};
	}
} f[kMaxN]; 

int a[kMaxN], n; 

signed main() {
	cin >> n;
	for (int i = 0; i < (1 << n); i++) {
		cin >> a[i], f[i] = {a[i], -INF};
	} 
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < (1 << n); j++) {
			if (j & (1 << i)) f[j] = f[j] + f[j ^ (1 << i)];
		}
	}
	int ans = 0;
	for (int i = 1; i < (1 << n); i++) {
		ans = max(ans, f[i].x + f[i].y);
		cout << ans << '\n';
	}
	return 0;
}

CF449D Jzzhu and Numbers

还是 SOS DP.

可以容斥,GiG_i 表示至少有 ii 位为 11 的方案数,答案为 G0−G1+G2−G3+...G_0 - G_1 + G_2 - G_3 + ....考虑计算 GiG_i,令状压状态 fif_i 为包含序列 AA 中的元素个数,有 if((1 << j) & i) f[i] += f[i ^ (1 << j)]. 令 ii 中 11 的个数为 cntcnt 个,对 GcntG_{cnt} 的贡献为 2fcnt−12^{f_{cnt}} - 1.

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = 1e6 + 5, kMaxM = (1 << 20) + 5, Mod = 1e9 + 7;

int a[kMaxN], f[kMaxM], p[kMaxM], g[kMaxM], n, ans;

signed main() {
    cin >> n;
    p[0] = 1;
    for (int i = 1; i < kMaxM; i++) p[i] = (p[i - 1] * 2) % Mod;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        f[a[i]]++;
    }
    for (int j = 0; j <= 20; j++) {
        for (int i = 1; i < (1 << 20); i++) {
            if (i & (1 << j)) {
                f[i ^ (1 << j)] += f[i];
            }
        }
    }
    for (int i = 0; i < (1 << 20); i++) {
        int cnt = 0;
        for (int j = 0; j <= 20; j++) {
            if (i & (1 << j)) cnt++;
        }
        g[cnt] = (g[cnt] + p[f[i]] - 1 + Mod) % Mod;
    }
    for (int i = 0; i <= 20; i++) {
        if (i % 2) ans = (ans - g[i] + Mod) % Mod;
        else ans = (ans + g[i]) % Mod;
    }
    cout << ans << '\n';
    return 0;
}

P3469 [POI 2008] BLO-Blockade

做知识梳理题单看到的题。一直没有想清楚如何计数。

其他思路:计算点双连通,构建圆方树。

很容易想到,若不为割点,答案仅有 2(n−1)2(n - 1)。

若为割点,令割去之后形成的连通块大小为 sizesize,则答案为 ∑i=1k∑j=1(j≠i)ksizei⋅sizej\sum_{i=1}^{k} \sum_{j=1(j\ne i)}^{k} size_i \cdot size_j,显然可以化简为 ∑i=1k(n−sizei−1)⋅sizei\sum_{i=1}^{k} (n - size_i - 1) \cdot size_i。

转化为如何求 sizesize,若子节点 vv 存在返祖边,那么割去 xx 后依然连通,可以与 xx 分在一组最后计算贡献。

令 sumsum 为之前割去 xx 之后产生的 sizesize 总和,若没有反祖边,则可以与 sumsum 计算贡献并添加在 sumsum 中。

在统计 sizesize 时,即使有反祖边也要统计,但是并不会重复,因为在搜索 vv 点的时候并不会将反祖边连的连通块的答案记录进去。

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = 5e5 + 5;

int low[kMaxN], dfn[kMaxN], sz[kMaxN], ans[kMaxN], cnt, n, m;
vector<int> G[kMaxN];
bool vis[kMaxN];

void dfs(int x, int fa, int son = 0) {
    low[x] = dfn[x] = ++cnt, sz[x] = 1;
    int sum = 0;
    for (auto v : G[x]) if (v != fa) {
        if (!dfn[v]) {
            dfs(v, x), son++, low[x] = min(low[x], low[v]);
            sz[x] += sz[v];
            if (low[v] >= dfn[x]) {
                if (fa != 0) vis[x] = 1;
                ans[x] += sum * sz[v];
                sum += sz[v];
            }
        } else low[x] = min(low[x], dfn[v]);
    }
    ans[x] += sum * (n - sum - 1);
    if (fa == 0 && son >= 0) vis[x] = 1;
}

signed main() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y; cin >> x >> y;
        G[x].push_back(y), G[y].push_back(x);
    }
    dfs(1, 0);
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) cout << (n - 1) * 2 << '\n';
        else cout << (n - 1) * 2 + ans[i] * 2 <<'\n';
    }
    return 0;
}

8.28

CF1234F Yet Another Substring Reverse

可以想到,如果两个字段字母没有重复,可以通过一次换位换到一起。因此转化为求最大的两个没有交集的字段长度总和。可以直接使用 DP 结局。

code
C++
#include <bits/stdc++.h> 

using namespace std;

const int kMaxN = 1e6 + 5, kMaxK = 21, kMaxM = (1 << 20) + 5;

int a[kMaxN], b[kMaxN], f[kMaxM], d[kMaxN], n, ans; 
bool vis[kMaxK];

signed main() {
	string s; cin >> s, n = s.size(), s = " " + s; 
	for (int i = 1; i <= n; i++) {
		a[i] = s[i] - 'a';
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= 19; j++) vis[j] = 0; 
		int ret = 0, cnt = 0;
		for (int j = i; j >= 1; j--) {
			if (vis[a[j]]) break;
			cnt++, vis[a[j]] = 1, ret += (1 << a[j]);
			f[ret] = max(f[ret], cnt);
		} 
		b[i] = ret, d[i] = cnt;
	}
	for (int i = 0; i <= 19; i++) {
		for (int j = 0; j < (1 << 20); j++) {
			if (j & (1 << i)) f[j] = max(f[j], f[j ^ (1 << i)]);
		}
	}
	int X = (1 << 20) - 1;
	for (int i = 1; i <= n; i++) {
		ans = max(ans, d[i] + f[X ^ b[i]]);
	}
	cout << ans << '\n';
	return 0;
}

AT_abc349_f [ABC349F] Subsequence LCM

首先考虑到如果 aia_i 不是 mm 的因数,则一定不会被选入,可以直接跳过。

对于 mm,分解质因数。令 m=p1e1p2e2...pkekm = p_1^{e_1}p_2^{e_2}...p_k^{e_k},对于每一个质因数,一个合法的字段一定存在 pieip_i^{e_i}. 先进行一次 SOSDP,求出在属于 ii 的个数,使用 2fj−12^{f_j} - 1 求出所有非空子集。反向 SOSDP 求出集合恰好为 jj 的方案数。

code
C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kMaxN = 2e5 + 5, kMaxK = 20, kMaxM = (1 << 20) + 5, Mod = 998244353;

int a[kMaxN], sum[kMaxM], cnt[kMaxK], f[kMaxK], n, N, m;

vector<int> C(int x) {
    vector<int> ret;
    for (int i = 2; i * i <= x; i++) if (x % i == 0) {
        int cnt = 1;
        for (; x % i == 0; cnt *= i, x /= i);
        ret.push_back(cnt);
    }
    if (x != 1) ret.push_back(x);
    ret.push_back(1);
    return ret;
}

int P(int a, int b, int ret = 1) {
    for (; b ; b >>= 1, a = a * a % Mod) {
        if (b & 1) ret = ret * a % Mod;
    }
    return ret;
}

signed main() {
    cin >> N >> m;
    for (int i = 1; i <= N; i++) {
        int x; cin >> x;
        if (m % x == 0) a[++n] = x;
    }
    vector<int> p = C(m);
    int tot = p.size();
    for (int i = 1; i <= n; i++) {
        int ret = 0;
        for (int j = 0; j < tot; j++) {
            if (a[i] % p[j] == 0) ret += (1 << j); 
        }
        sum[ret]++;
    }
    for (int i = 0; i < tot; i++) {
        for (int j = 0; j < (1 << tot); j++) {
            if (j & (1 << i)) sum[j] = (sum[j] + sum[j ^ (1 << i)]) % Mod;
        }
    }
    for (int i = 0; i < (1 << tot); i++) {
        f[i] = (P(2, sum[i]) - 1 + Mod) % Mod;
    }
    for (int i = 0; i < tot; i++) {
        for (int j = 0; j < (1 << tot); j++) {
            if (j & (1 << i)) f[j] = (f[j] - f[j ^ (1 << i)] + Mod) % Mod;
        }
    }
    cout << f[(1 << tot) - 1] << '\n';
}

P12491 [集训队互测 2024] 串联

一个不太常规的题目。在点分治中需要先进行排序。

对于 min⁡\min 操作不方便点分治,因此先找出所有 disdis,并按照 AA 从大到小排序,可以定下 min⁡a\min a。接下来要拼接经过 rtrt 的边,拼接的边应满足Bx≥⌈VA⌉−B−brtB_x \ge \left \lceil \frac{V}{A} \right \rceil - B - b_{rt}。可以用值域线段树维护最小的 BB,但是不能在同一子树,所以可以用距离根最近的节点当做 opop,用线段树维护 opop 不同的次小值。

code
C++
原文