版本比较
《信息学奥赛 trick 杂录》
正文
增加 32 行删除 4 行
123456789101112131415161710181920212210 行未改动3334353637383940414278 行未改动12112212312411512512612712811912913013113213313413513613713813914014114214314414514614714826 行未改动17517617717817918018118218318418518618718818919019119219319419519644 行未改动24124224324421724524624724824967 行未改动持续更新ing...
欢迎各位在评论区分享经验,如若收录本文,我将注明贡献。
近七日更新:
无。
# 目录
## Part 1. 动态规划
- 连续段 DP
## Part 2. 分治与离线
- 树链分块
## Part 3. 图论
- 树的中心
10 行未改动 - 最大值与最小值之间的容斥转化
- 异或哈希
- 必要条件缩小枚举范围
## Part 5. 字符串算法
- 进制哈希的运用
78 行未改动### 冷门性质一
$n$ 个节点的树,以 **任意** 节点为根的 **任意** dfs 序下排行 $\lceil \dfrac{n}{2} \rceil$ 的点的祖先链中 **一定包含** 重心。
等价表达:重心的子树内一定包含 dfs 序排行 $\lceil \dfrac{n}{2} \rceil$ 的点。 等价表达:重心的子树内一定包含 dfs 序排行 $\lceil \dfrac{n}{2} \rceil$ 的点。
例题:
[P5666](https://www.luogu.com.cn/problem/P5666)- [P5666](https://www.luogu.com.cn/problem/P5666)
## 树上点到链的距离公式
设 $d$ 表示节点 $u$ 到链 $(x, y)$ 的距离:
- $d = dis[u] - dis[\operatorname{LCA}(x, u)] - dis[\operatorname{LCA}(y, u)] + dis[\operatorname{LCA}(x, y)]$.
其中,$dis[u]$ 表示 $u$ 到根节点的距离。
例题:
- [HDU-5296](https://vjudge.net/problem/HDU-5296)
- [P1099](https://www.luogu.com.cn/problem/P1099)
## 并查集的运用
详见 [OI-Wiki](https://oi-wiki.org/topic/dsu-app/)。
26 行未改动例题:
- [P8819 ~~不可以!总司令~~](https://www.luogu.com.cn/problem/P8819)
- [P10833](https://www.luogu.com.cn/problem/P10833)
## 必要条件缩小枚举范围
有的时候,目标答案由于限制过多难以统计,但是答案本身的数量不多,我们可以通过削弱限制来枚举所有可能的答案,及枚举满足必要条件的答案再逐个 check。
设满足必要条件的可能答案的个数为 $n$,对于每个可能答案的 check 是 $\operatorname{O}(m)$ 的,则总体时间复杂度为 $\operatorname{O}(nm)$,通常情况下要么 $n$ 非常小,要么 check 的时间复杂度可以做到 $\operatorname{O}(1)$ 或者 $\operatorname{O}(\log n)$。
例题:
- [P10833](https://www.luogu.com.cn/problem/P10833)
- [CF1175F](https://codeforces.com/problemset/problem/1175/F)
# 字符串算法
## 进制哈希的运用
44 行未改动 if (son[p][1]) w[p] ^= w[son[p][1]], val[p] ^= val[son[p][1]] << 1 | w[son[p][1]]; } void insert (int &p, int x, int dep) { if (!p) p = ++idx; if (dep > K) return w[p] ^= 1, void(); if (dep > K) return w[p] ^= 1, void(); insert(son[p][x & 1], x >> 1, dep + 1), pushup(p); }```
67 行未改动