正文
增加 334 行删除 1 行
21 行未改动22232425262728296 行未改动363738394041424315 行未改动59606162636465666768697051 行未改动12212312412512612712812972 行未改动20220320420520620720820913 行未改动22322422522622722822923025 行未改动256257258259260261262263130 行未改动全部展开394395396397398399400401402441 行未改动全部展开84484584684784884985085185285385485585685785838 行未改动21 行未改动reverse(c.begin(), c.end());```::::::::info[离散化]离散化 $a$ 数组,先复制一遍 $a$ 数组到 $aa$。$alen$ 表示 $a$ 的长度。6 行未改动```::::::::info[快速读写]```cppint read(){int x = 0, f = 1;15 行未改动}```::::#### 基础数据结构#### 高级数据结构::::info[线段树]区间修改 + 区间查询```cpp#define lc x * 251 行未改动}```::::::::info[字典树 Trie]```cppint getid(char ch) {if (ch >= 'a' && ch <= 'z') return ch - 'a';72 行未改动}```::::#### 树论::::info[树的直径]```cpp13 行未改动dfs(node, 0 );```::::::::info[最近公共祖先 LCA]```cppvoid dfs(int x, int fa){25 行未改动}```::::::::info[点分治][P3806](https://www.luogu.com.cn/problem/P3806)```cpp#include <iostream>130 行未改动全部展开}```SpfaDijkstra```cppvoid dijkstra(){441 行未改动全部展开}```::::::::info[模意义下的乘法逆元]费马小定理求逆元(仅适用于质数模数)```cpp(a / b) % MOD = qpow(b, MOD - 2) * a % MOD;```::::::::info[数论分块]```cppint res = 0, l = 1, r;while (l <= n) {38 行未改动21 行未改动2223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889906 行未改动97989910010110210310410510615 行未改动12212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935051 行未改动40240340440540640740840941041141241341441541641741872 行未改动49149249349449549649749849950050113 行未改动51551651751851952052152252352452552652752825 行未改动554555556557558559560561562563564565566567130 行未改动全部展开698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732441 行未改动全部展开11741175117611771178117911801181118211831184118511861187118811891190119138 行未改动21 行未改动reverse(c.begin(), c.end());```::::::::info[排序]最佳解法:sort冒泡排序```cppfor (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++){if (a[j] > a[j + 1]) {swap(a[j], a[j + 1]);}}}```归并排序的合并函数,可能在逆序对上有一点用。\分治函数自己写去。```cppvoid merge(){int i = 0, j = 0, k = 0;while (i < alen && j < blen) {if (b[j] < a[i]) c[k] = b[j], j++, k++;else c[k] = a[i], i++, k++;}for (; i < alen; i++, k++) c[k] = a[i];for (; j < blen; j++, k++) c[k] = b[j];}```::::::::info[前缀和与差分]前缀和```cppfor (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];```差分```// 修改:把 i 到 j 都加上 xcf[i] += x;cf[j + 1] -= x;// 统计答案for (int i = 1; i <= n; i++) {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$。$alen$ 表示 $a$ 的长度。6 行未改动```::::::::info[快速读写]如果普通快速读写还是会被卡常,可以把 getchar 和 putchar 替换为 getchar_unlocked (Linux) / _getchar_nolock (Windows) 和 putchar_unlocked (Linux) / _putchar_nolock (Windows)```cppint read(){int x = 0, f = 1;15 行未改动}```::::::::error[深度优先搜索 / 深度优先遍历 DFS]::::::::error[广度优先搜索 / 广度优先遍历 BFS]::::#### 基础数据结构::::error[链表]::::::::info[栈]手动实现```cpp// push xst[++top] = x;// pop topif (top != 0) top--;// query topif (top == 0) return -1;else return st[top];// sizereturn top;// clear uptop = 0;```STL模板```cpp// push xst.push(x);// pop topif (!st.empty()) st.pop();// query topif (!st.empty()) return st.top();// sizereturn st.size();// clear upwhile (!st.empty()) st.pop();// is emptyreturn st.empty();```::::::::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 表 / 稀疏表]::::::::error[哈希表 / 散列表]::::::::info[字典树 Trie]```cppint getid(char ch) {if (ch >= 'a' && ch <= 'z') return ch - 'a';72 行未改动}```::::::::error[AC 自动机]::::#### 树论::::info[树的直径]```cpp13 行未改动dfs(node, 0 );```::::::::error[树的重心]::::::::error[树的中心]::::::::info[最近公共祖先 LCA]```cppvoid dfs(int x, int fa){25 行未改动}```::::::::error[树链刨分]::::::::error[树上启发式合并]::::::::info[点分治][P3806](https://www.luogu.com.cn/problem/P3806)```cpp#include <iostream>130 行未改动全部展开}```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;```::::::::error[矩阵乘法与矩阵快速幂]::::::::info[数论分块]```cppint res = 0, l = 1, r;while (l <= n) {38 行未改动