正文
增加 209 行删除 6 行
66 行未改动676869707172737480 行未改动15515615715815916016116216316416516616716816917051 行未改动222223224225226227228229230231232288 行未改动全部展开521522523524525526527528529441 行未改动全部展开97197297397497597697797897947 行未改动66 行未改动cf[i] += cf[i - 1];a[i] += cf[i];}```::::::::info[离散化]离散化 $a$ 数组,先复制一遍 $a$ 数组到 $aa$。80 行未改动::::error[队列]::::#### 高级数据结构::::error[单调栈]::::::::error[单调队列]::::::::info[线段树]区间修改 + 区间查询```cpp#define lc x * 251 行未改动}```::::::::error[树状数组]::::::::error[并查集]::::::::error[ST 表 / 稀疏表]288 行未改动全部展开}```SpfaDijkstra```cppvoid dijkstra(){441 行未改动全部展开}```::::::::info[模意义下的乘法逆元]费马小定理求逆元(仅适用于质数模数)```cpp(a / b) % MOD = qpow(b, MOD - 2) * a % MOD;```47 行未改动66 行未改动6768697071727374757677787980818283848586878880 行未改动16917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935051 行未改动402403404405406407408409288 行未改动全部展开698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732441 行未改动全部展开11741175117611771178117911801181118247 行未改动66 行未改动cf[i] += cf[i - 1];a[i] += cf[i];}```二维前缀和```cppfor (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {sum[i][j] = sum[i][j - 1] + g[i][j];}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {sum[i][j] += sum[i - 1][j];}}```::::::::info[离散化]离散化 $a$ 数组,先复制一遍 $a$ 数组到 $aa$。80 行未改动::::error[队列]::::#### 高级算法思想#### 动态规划本部分如果没有详细说明,DP 数组初值即为全 0。::::warning[背包 DP]01 背包```cppfor (int i = 1; i <= m; i++) {for (int j = n; j >= w[i]; j--) {dp[j] = max(dp[j], dp[j - w[i]] + v[i]);}}ans = dp[n];```完全背包(疯狂的菜药)```cppfor (int i = 1; i <= m; i++) {for (int j = w[i]; j <= n; j++) {dp[j] = max(dp[j], dp[j - w[i]] + v[i]);}}ans = dp[n];```::::::::warning[线性 DP]最长上升子序列 [B3637](https://www.luogu.com.cn/problem/B3637)```cppfor (int i = 1; i <= n; i++) {dp[i] = 1;for (int j = i - 1; j >= 1; j--) {if (a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1);}}for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);```最长公共子序列 [B2175](https://www.luogu.com.cn/problem/B2175)```cppfor (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {if (a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);}}ans = dp[n][m];```::::::::error[区间 DP]::::::::error[树形 DP]::::::::info[数位 DP]使用记忆化搜索实现。```cpp// 位置 当前的数 是否达到上线 是否为前导0// 根据题意增加其他的需要记录的内容int dfs(int pos, int num, bool lim, bool zero){if (pos == 0) // 记录答案,返回if (dp[pos][num][lim][zero] != -1) return dp[pos][num][lim][zero];int res = 0, maxn = (lim ? max_num[pos] : 9);for (int i = 0; i <= maxn; i++) {if (i == 0 && zero) res += // 向下搜索else if (f[i]) res += // 向下搜索}return dp[pos][num][lim][zero] = res;}int solve(int x){memset(dp, -1, sizeof dp);int xx = x, len = 0;while (xx > 0) {max_num[++len] = xx % 10;xx /= 10;}return dfs(len, 0, true, true);}```计算 $[a,b]$ 的答案```cppsolve(b) - solve(a - 1);```::::::::error[状态压缩 DP]::::#### 高级数据结构::::info[单调栈]注意,出栈规则 & 遍历顺序需要根据题目而具体修改。以下模板皆为:找第一个后方大于当前元素的下标。找第一个\正序遍历时,后方;倒序遍历时,前方。\< 大于 <= 大于等于 > 小于 >= 小于等于。\当前元素的下标。数组实现```cpptop = 0;for (int i = 1; i <= n; i++) {while (top != 0 && a[st[top]] < a[i]) nxt[st[top--]] = i;st[++top] = i;}```STL实现```cppwhile (!st.empty()) st.pop();for (int i = 1; i <= n; i++) {while (!st.empty() && a[st.top()] < a[i]) nxt[st.top()] = i, st.pop();st.push(i);}```::::::::info[单调队列 / 滑动窗口]\>= 最小 <= 最大数组实现```cpp// 最小值head = 1, tail = 0;for (int i = 1; i <= n; i++) {while (head <= tail && a[q[tail]] >= a[i]) tail--;while (head <= tail && q[head] + k <= i) head++;q[++tail] = i;if (i >= k) minn[i] = q[head];}// 最大值head = 1, tail = 0;for (int i = 1; i <= n; i++) {while (head <= tail && a[q[tail]] <= a[i]) tail--;while (head <= tail && q[head] + k <= i) head++;q[++tail] = i;if (i >= k) maxn[i] = q[head];}```STL 实现(不建议,因为 deque 太石山了)```cpp// 最小q.clear();for (int i = 1; i <= n; i++) {while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();while (!q.empty() && q.front() + k <= i) q.pop_front();q.push_back(i);if (i >= k) minn[i] = q.front();}// 最大q.clear();for (int i = 1; i <= n; i++) {while (!q.empty() && a[q.back()] <= a[i]) q.pop_back();while (!q.empty() && q.front() + k <= i) q.pop_front();q.push_back(i);if (i >= k) maxn[i] = q.front();}```::::::::error[树状数组]::::::::info[线段树]区间修改 + 区间查询```cpp#define lc x * 251 行未改动}```::::::::error[并查集]::::::::error[ST 表 / 稀疏表]288 行未改动全部展开}```Spfa```cppvoid spfa(int s){queue<int> q;memset(dis, 0x3f, sizeof(dis));q.push(s);dis[s] = 0;vis[s] = 1;while (!q.empty()) {int u = q.front();vis[u] = 0;q.pop();for (auto it : g[u]) {if (dis[it.first] > dis[u] + it.second) {dis[it.first] = dis[u] + it.second;if (!vis[it.first]) {vis[it.first] = 1;q.push(it.first);}}}}}```Dijkstra```cppvoid dijkstra(){441 行未改动全部展开}```::::::::warning[模意义下的乘法逆元]费马小定理求逆元(仅适用于质数模数)```cpp(a / b) % MOD = qpow(b, MOD - 2) * a % MOD;```47 行未改动