正文
增加 127 行删除 236 行
1234567869 行未改动7879808182838485868788899091929394959660 行未改动1571581591601611621631647 行未改动1721731741751761771781791801811821831841856 行未改动19219319419519619719819920020120220320420520620720820921021135 行未改动24724824925025125225325425518 行未改动27427527627727827928028128228328428528628728828929012 行未改动3033043053063073083093103112 行未改动31431531631731831932032132248 行未改动3713723733743753763773783793 行未改动3833843853863873883893903915 行未改动3973983994004014024034044053 行未改动4094104114124134144154164174184194204214224234244254264274284294304314324334344353 行未改动4394404414424434444454464472 行未改动4504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844855 行未改动4914924934944954964974984996 行未改动50650750850951051151251351421 行未改动53653753853954054154254354425 行未改动57057157257357457557657757849 行未改动6286296306316326336346356367 行未改动64464564664764864965065165289 行未改动74274374474574674774874975084 行未改动83583683783883984084184284310 行未改动85485585685785885986086186210 行未改动87387487587687787887988088135 行未改动917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105711 行未改动10691070107110721073107410751076107752 行未改动11301131113211331134113511361137113819 行未改动11581159116011611162116311641165116611 行未改动1178117911801181118211831184118511863 行未改动11901191119211931194119511961197119832 行未改动12311232123312341235123612371238123974 行未改动13141315131613171318131913201321132228 行未改动13511352135313541355135613571358135911 行未改动1371137213731374137513761377137813797 行未改动13871388138913901391139213931394139517 行未改动1413141414151416141714181419142014217 行未改动1429143014311432143314341435143614371438143914401441144216 行未改动14591460146114621463146414651466146754 行未改动15221523152415251526152715281529153064 行未改动15951596159715981599160016011602160355 行未改动16591660166116621663166416651666166742 行未改动17101711171217131714171517161717171844 行未改动1763176417651766176717681769177017714 行未改动17761777177817791780178117821783178451 行未改动183618371838183918401841184218431844116 行未改动全部展开19611962196319641965196619671968196959 行未改动20292030203120322033203420352036203746 行未改动20842085208620872088208920902091209223 行未改动21162117211821192120212121222123212424 行未改动21492150215121522153215421552156215726 行未改动21842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220738 行未改动224622472248224922502251225222532254225522565 行未改动22622263226422652266226722682269227022 行未改动2293229422952296229722982299230023012302230323044 行未改动230923102311231223132314231523162317553 行未改动全部展开287128722873287428752876287728782879366 行未改动全部展开32463247324832493250325132523253325460 行未改动3315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333447 行未改动3352335333543355335633573358335933605 行未改动3366336733683369337033713372337333743 行未改动33783379338033813382338333843385338673 行未改动346034613462346334643465346634673468497 行未改动全部展开396639673968396939703971397239733974321 行未改动全部展开429642974298429943004301430243034304109 行未改动全部展开441444154416441744184419442044214422143 行未改动全部展开456645674568456945704571457245734574325 行未改动全部展开49004901490249034904490549064907490817 行未改动492649274928492949304931493249334934125 行未改动全部展开50605061506250635064506550665067506836 行未改动51055106510751085109511051115112511356 行未改动517051715172517351745175517651775178308 行未改动全部展开54875488548954905491549254935494549554965497549854996 行未改动5506550755085509551055115512551355146 行未改动55215522552355245525552655275528552914 行未改动5544554555465547554855495550555155526 行未改动555955605561556255635564556555665567260 行未改动全部展开**注意:文章内容量较大,网页内存占用可能达到数 MB。由于浏览器性能原因,渲染耗时可能较长。**[](https://www.luogu.com.cn/article/xjmzumm3/edit "编辑:此操作仅作者有效") [](https://cyx012113.top "作者主页") [](https://www.luogu.com.cn/user/1099138 "作者洛谷主页")# Tips> Tips:> - [ ] 你数组开小了吗?> - [ ] 你 MLE 了吗?69 行未改动——[$\bf\sf\color{#3498db}{sszcdjr}$](https://www.luogu.com.cn/user/820056 "月亮好闪,拜谢月亮!"):::# 预处理## 主函数模板```cpp#include <bits/stdc++.h>using namespace std;signed main() {return 0;}```## 主函数模板(加强版)```cpp#define _USE_MATH_DEFINES#include <bits/stdc++.h>#include <bits/extc++.h>//#define int long long60 行未改动- [ ] 指针型数据结构改为数组型?(例如线段树)- [ ] 关同步流 / 快读快写- [ ] 避免除法和取模- [ ] 分块的块大小是否合理?(例如带修莫队板子,块大小需要为 $n^{\frac{2}{3}}$ 才可以,$\sqrt{n}$ 过不了)- [ ] 运算能二进制的改成二进制(例如线段树 tr[pos << 1] tr[pos << 1 | 1])- [ ] 减少函数传参- [ ] 评测机波动?- [ ] endl 换成 `\n`7 行未改动- [ ] 厌氧?- [ ] 火车头?(例如:指令集优化,fread,编译优化,请勿滥用)- [ ] i++ 改成 ++i- [ ] 无符号整数更快?- [ ] 试试 C++ 14 (GCC 9) 和 C++ 23- [ ] 递归函数改成非递归- [ ] 循环展开?(费手,不推荐)## 快读板子```cppinline int read() { // 快读int x = 0, f = 1;char c = getchar();while (c < '0' || c > '9') {if (c == '-') f = -1;6 行未改动return x * f;}```## 快写板子```cppinline void print(int x) { // 快写if (x < 0) {putchar('-');x = -x;}if (x > 9) print(x / 10);putchar(x % 10 + '0');}```## 指令集优化```cpp#pragma GCC target("mmx,sse,sse2,sse3,ssse3") // 基础SSE#pragma GCC target("sse4.1,sse4.2,popcnt,lzcnt") // SSE4#pragma GCC target("avx,bmi,bmi2,f16c,fma,fma4,xop") // AVX#pragma GCC target("avx2,avx512f,avx512cd") // AVX2/AVX51235 行未改动#define SIMD_AVAILABLE 0#endif```## 编译设置(洛谷等 OJ 限制使用 `#pragma GCC optimize`)```cpp#pragma GCC optimize("Ofast,unroll-loops,inline,fast-math,O3")#pragma GCC optimize("unsafe-math-optimizations,finite-math-only")#pragma GCC optimize("no-signed-zeros,no-trapping-math,associative-math")#pragma GCC optimize("no-stack-protector,omit-frame-pointer")18 行未改动#pragma GCC diagnostic ignored "-Wunused-variable"#pragma GCC diagnostic ignored "-Wsign-compare"```## 关闭同步流```cpp#include <fstream>ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);```# 数学操作## 随机数```cppunsigned long long get_seed() {auto now = chrono::system_clock::now();auto timestamp = chrono::duration_cast <chrono::nanoseconds>(now.time_since_epoch());return static_cast <unsigned long long> (timestamp.count());12 行未改动return l + Rand(r - l);}```## 判断质数```cppbool Ip(int n) {if (n < 2) return false;if (n == 2) return true;for (int i = 2;i * i <= n;i++) {2 行未改动return true;}```## 判断质数(Miller Rabin)```cppconst int test_time = 10;unsigned long long get_seed() {auto now = chrono::system_clock::now();48 行未改动| 20 | $(4^{-20} \approx 9.09 \times 10^{-13}) (\approx 0.000000000091\%)$ | $\approx 100%$ |建议:不小于 $10$ 即可。## 输出因数```cppvoid of(int n) {if (n < 1) return;if (n == 1) {cout << "1" << endl;3 行未改动if (n % i == 0) cout << i << endl;}```## 分解质因数```cppvector <int> Gif(int n) {vector <int> result;for (int i = 2;i * i <= n;i++) {if (n % i == 0) {5 行未改动return result;}```## 倒转数```cppint rv(int n) {int t = 0;while (n > 0) {t *= 10;3 行未改动return t;}```## 阶乘```cppint fact(int n) {int ans = 1;for (int i = 2;i <= n;i++) ans *= i;return ans;}```## 排列组合```cppint C(int n, int m) {return fact(n) / fact(m) / fact(n - m);}int A(int n, int m) {return fact(n) / fact(n - m);}```## 快速幂```cppint qp(int a, int b, int mod) {int ans = 1;while (b) {if (b & 1) ans = (ans * a) % mod;3 行未改动return ans;}```## 龟速乘```cppint sm(int a, int b, int mod) {int ans = 1;while (b) {if (b & 1) ans = (ans + a) % mod;2 行未改动}}```## 快速乘```cppint qm(int a, int b, int mod) {int c = a * b - ((long double)a / mod * b + 0.5L) * mod;if (c < 0) c += mod;return c + mod;}```## 勾股定理```cppdouble pt(double a, double b) {return sqrt(pow(a, 2) + pow(b, 2));}```## 两点距离```cppdouble dist(double a1, double a2, double b1, double b2) {return sqrt(pow(fabs(a1 - b1), 2) + pow(fabs(a2 - b2), 2));}```## 海伦-秦九韶公式```cppdouble s(double a, double b, double c) {double p = (a + b + c) / 2;return sqrt(p * (p - a) * (p - b) * (p - c));}```## 线性逆元```cppint n, p, inv[N], factinv[N];signed main() {cin >> n >> p;5 行未改动return 0;}```## 扩展欧几里得(Exgcd)```cppint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;6 行未改动return r;}```## 中国剩余定理(CRT)```cppint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;21 行未改动cout << (s + ans) % ans << endl;```## 扩展中国剩余定理(ExCRT)```cppint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;25 行未改动}cout << (Y % X + X) % X << endl;```## 手写根号```cpp#define exp 0.000001// 二分法double binsearch_sqrt(int m) {49 行未改动return 1 / y;}```## 线性基```cppstruct LinearBase {int dat[N];void insert(int x) {for (int i = 63;~i;i--) {7 行未改动}} lb;```## 日期计算```cppstruct _time {int y, m, d, h, min, s;int mmod[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};89 行未改动}};```## Base 64 编码 / 解码```cpp#include <bits/stdc++.h>using namespace std;static const string base64_chars =84 行未改动return 0;}```## 欧拉(线性)质数筛```cppint pr[N];bool ip[N];int gp(int n) {10 行未改动return p;}```## 归并排序```cppint a[N], b[N], c[N], ans1, ans2, n, _;void msort(int l, int r, int &ans, auto &A, auto &B) {if (l == r) return;10 行未改动for (int k = l;k <= r;k++) A[k] = B[k];}```## 矩阵快速幂 / 矩阵乘法```cpp#include <bits/stdc++.h>using namespace std;const ll N = 1e2 + 2, mod = 1e9 + 7;35 行未改动}return 0;}```## 高精度```cppstruct bign {int d[N], len;void clean() {while (len > 1 && !d[len-1]) len--;}bign() {memset(d, 0, sizeof(d));len = 1;}bign(int num) {*this = num;}bign(char* num) {*this = num;}bign operator = (const char* num) {memset(d, 0, sizeof(d));len = strlen(num);for (int i = 0;i < len;i++) d[i] = num[len - 1 - i] - '0';clean();return *this;}bign operator = (int num) {char s[20];sprintf(s, "%d", num);*this = s;return *this;}bign operator + (const bign& b) {bign c = *this;int i;for (i = 0;i < b.len;i++) {c.d[i] += b.d[i];if (c.d[i] > 9) c.d[i] %= 10, c.d[i+1]++;}while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++;c.len = max(len, b.len);if (c.d[i] && c.len <= i) c.len = i + 1;return c;}bign operator - (const bign& b) {bign c = *this;int i;for (i = 0;i < b.len;i++) {c.d[i] -= b.d[i];if (c.d[i] < 0) c.d[i] += 10, c.d[i + 1]--;}while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--;c.clean();return c;}bign operator * (const bign& b) const {int i, j;bign c;c.len = len + b.len;for (j = 0;j < b.len;j++)for(i = 0;i < len;i++)c.d[i + j] += d[i] * b.d[j];for (i = 0;i < c.len - 1;i++)c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;c.clean();return c;}bign operator / (const bign& b) {int i, j;bign c = *this, a = 0;for (i = len - 1;i >= 0;i--) {a = a * 10 + d[i];for (j = 0;j < 10;j++) if (a < b * (j + 1)) break;c.d[i] = j;a = a - b * j;}c.clean();return c;}bign operator % (const bign& b) {int i, j;bign a = 0;for (i = len - 1;i >= 0;i--) {a = a * 10 + d[i];for (j = 0;j < 10;j++) if (a < b * (j + 1)) break;a = a - b * j;}return a;}bign operator += (const bign& b) {*this = *this + b;return *this;}bool operator < (const bign& b) const {if (len != b.len) return len < b.len;for (int i = len - 1;i >= 0;i--)if (d[i] != b.d[i]) return d[i] < b.d[i];return false;}bool operator > (const bign& b) const {return b < *this;}bool operator <= (const bign& b) const {return !(b < *this);}bool operator >= (const bign& b) const {return !(*this < b);}bool operator != (const bign& b) const {return b < *this || *this < b;}bool operator == (const bign& b) const {return !(b < *this) && !(b > *this);}string str() const {char s[maxn]= {};for (int i = 0;i < len;i++) s[len - 1 - i] = d[i] + '0';return s;}};istream& operator >> (istream& in, bign& x) {string s;in >> s;x = s.c_str();return in;}ostream& operator << (ostream& out, const bign& x) {out << x.str();return out;}```## 快速位运算```cppint getor(int l, int r) {if (l == r) return l;return l | ((1 << ( __lg(l ^ r) + 1)) - 1);}11 行未改动if (x % 4 == 3) return 0;}```## 快速傅里叶变换(FFT / IFFT)```cppstruct Complex {double x, y;Complex(double _x = 0.0, double _y = 0.0) {52 行未改动if (on == -1) for (int i = 0;i < len;i++) y[i].x /= len, y[i].y /= len;}```## 快速数论变换(NTT)```cppvoid ntt(int *x, int lim, int opt) {int k, gn, g, tmp;for (int i = 0; i < lim; ++i)19 行未改动}```# 字符串操作## 树的遍历```cppvoid pre(int x) {cout << x << " ";if (a[x][1]) pre(a[x][1]);if (a[x][2]) pre(a[x][2]);11 行未改动cout << x << " ";}```## 判断回文字符串```cppbool is_hw(string s) {int left = 0, right = s.size() - 1;while (left < right) {if (s[left] != s[right]) return false;3 行未改动return true;}```## trie 字典树```cppint trie[N][MN];int tot, sum[N];void insert(string s) { //插入一个字符串s32 行未改动sum[root] -= fa;}```## AC 自动机```cppnamespace AC {struct S {int son[27], ans, fail, du, id;void init() {74 行未改动return 0;}```## 回文自动机(PAM)```cppstruct Palindrome_Automaton {int ch[N][LN], fail[N], len[N], sum[N], cnt, last;Palindrome_Automaton() {cnt = 1;28 行未改动return 0;}```## Manacher 最长回文串```cppstring s = "~|";char c;int p[N], ans, r = 0, mid = 0;11 行未改动return 0;}```## 手写字符串 hash```cppconst ull M = 1610612741, P = 131;ull h[2000005];7 行未改动hash <string> _hash;```## 双模数 hash```cppconst ull mod1 = 1610612741, mod2 = 402653189, p = 131;struct s {ull x, y;17 行未改动return ans;}```## 字符串任意子段 hash```cppull h[N], p[N];void init_Hash(string s) {int n = s.size();7 行未改动return h[pos + len] - h[pos] * p[len];}```## hash 质数表```cppull M[] = {0, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};```## 字符串 KMP```cppvector <int> prefix(string s) {int n = s.size();vector <int> pi(n);for (int i = 1;i < n;i++) {16 行未改动}```# 图论算法## SPFA 最短路```cppint dis[N];int h[N], to[N], val[N], nxt[N], cnt, n, m, s;int cnt_in_queue[N];bool vis[N];54 行未改动return 0;}```## SPFA 优化版(SLF + LLL + 随机化)```cppint dis[N];int h[N], to[N], val[N], nxt[N], tot[N], cnt, n, m, s, t, x, y, z;bool vis[N];64 行未改动return false;}```## SPFA Hack 数据生成器```cppunsigned long long get_seed() {auto now = chrono::system_clock::now();auto timestamp = chrono::duration_cast <chrono::nanoseconds>(now.time_since_epoch());return static_cast <unsigned long long> (timestamp.count());55 行未改动return 0;}```## Bellman-Ford 最短路```cppstruct Edge {int u, v, w;};42 行未改动return 0;}```## Dijkstra 最短路```cppint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z, s, t;bool vis[N];struct node {44 行未改动return 0;}```## Floyd 最短路```cppint n, m, x, y, z, dp[N][N];void Floyd() {for (int k = 1;k <= n;k++)4 行未改动memset(dp, 0x3f, sizeof(dp));```## Prim 最小生成树```cppint dis[N], h[N], to[N], val[N], nxt[N], ans, cnt, n, m;bool vis[N];struct node {51 行未改动return 0;}```## Kruskal 最小生成树```cppstruct Edge {int u, v, w;bool operator < (const Edge& other) const {return w < other.w;116 行未改动全部展开return 0;}```## Dinic 网络最大流```cppint h[N], to[N], val[N], nxt[N], dis[N], now[N], cnt, n, m, x, y, z, s, t, maxflow, flow;void add(int a, int b, int c) {to[++cnt] = b;59 行未改动return 0;}```## Dinic 二分图匹配```cppint h[N], to[N], val[N], nxt[N], dis[N], now[N], cnt = 1, n, m, x, y, z, s, t, maxflow, flow;void add(int a, int b, int c) {to[++cnt] = b;46 行未改动return flow - rest;}```## tarjan 割边 / 割点```cppint dfn[N], low[N], dfncnt, tot;int dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;map <int, bool> cut_v;map <pair <int, int>, bool> cut_e;23 行未改动if (flag >= 2 && u == root) cut_v[u] = 1;}```## tarjan 强连通分量```cppint dfn[N], low[N], tot, dfncnt;int dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;bool vis[N];stack <int> S;24 行未改动}}```## 拓扑排序```cppint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;int in[N];void add(int a, int b, int c) {26 行未改动return topoOrder;}```## 最近公共祖先(LCA)```cppint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, s, x, y, z, ans;int f[N][32], cost[N][32], dep[N];bool is_find;map <int, bool> vis;struct node {int v, w;friend bool operator < (node a, node b) {return a.w > b.w;}friend bool operator > (node a, node b) {return a.w < b.w;}} tmp;void add(int a, int b, int c) {to[++cnt] = b;val[cnt] = c;nxt[cnt] = h[a];38 行未改动x = f[x][j], y = f[y][j];return f[x][0];}```# 小型数据结构## 并查集```cppint f[N];int find(int x) {if (f[x] == x) return x;5 行未改动if (fx != fy) f[fx] = fy;}```## ST 表```cpp#include <iostream>using namespace std;const int N = 2e5 + 2, Logn = 22;22 行未改动}```# 树形数据结构## 树状数组```cppint s[N];inline int lowbit(int x) {return x & (-x);}void modify(int p, int v) {for(;p <= n;p += lowbit(p)) s[p] += v;}4 行未改动return res;}```## 线段树(指针版)```cppint n, m, mod = inf;int a[N];struct Segment_Tree {553 行未改动全部展开return 0;}```## 线段树(数组版)```cppint n, m, mod = inf;int a[N];struct Segment_Tree {366 行未改动全部展开return 0;}```## 线段树(加法版)```cppint n, m, mod;int a[N];struct Segment_Tree {60 行未改动return val;}```## 线段树(最小值版)```cppstruct Segment_Tree {int add;int min_val;int l, r;} s[N << 2];inline void pushup(int pos) {s[pos].min_val = min(s[pos << 1].min_val, s[pos << 1 | 1].min_val);}inline void pushdown(int pos) {s[pos << 1].min_val += s[pos].add;s[pos << 1].add += s[pos].add;s[pos << 1 | 1].min_val += s[pos].add;s[pos << 1 | 1].add += s[pos].add;s[pos].add = 0;}inline void build(int pos, int l, int r) {s[pos].l = l;s[pos].r = r;s[pos].add = 0;7 行未改动build(pos << 1 | 1, mid + 1, r);pushup(pos);}inline void add(int pos, int x, int y, int k) {if (x <= s[pos].l && s[pos].r <= y) {s[pos].min_val += k;s[pos].add += k;return;5 行未改动if (y > mid) add(pos << 1 | 1, x, y, k);pushup(pos);}inline int query_min(int pos, int x, int y) {if (x <= s[pos].l && s[pos].r <= y) return s[pos].min_val;pushdown(pos);int mid = (s[pos].l + s[pos].r) >> 1;3 行未改动return res;}```## gcd 线段树```cppint n, b[N];struct Segment_Tree {int sum;73 行未改动return __gcd(a_l, tail_g);}```## 可持久化线段树(主席树)- 指针版```cppint n, m, mod = inf;int a[N];vector <int> sorted; // 用于离散化的排序数组497 行未改动全部展开return 0;}```## 可持久化线段树(主席树)- 数组版```cppint n, m, mod = inf;int a[N];vector <int> sorted; // 离散化数组321 行未改动全部展开return 0;}```## Treap```cppint na;int ch[N][2];int val[N], dat[N];int sz[N], cnt[N];109 行未改动全部展开}```## Splay 伸展树from oi-wiki```cppint n, id, rt;int fa[N], val[N], cnt[N], sz[N], ch[N][2];bool dir(int x) {143 行未改动全部展开return 0;}```## 树套树(线段树套 Splay)```cpp// Splay Tree Node Structurestruct SplayNode {int val, cnt, sz;SplayNode *ch[2], *fa;325 行未改动全部展开return 0;}```## 笛卡尔树```cppint p[N], l[N], r[N], n;void build(int n) {stack <int> stk;17 行未改动return 0;}```## 树链剖分```cpp#define lc o << 1#define rc o << 1 | 1int n, a, b, w[N], q, u, v;125 行未改动全部展开——[$\bf\sf\color{#fe4c61}{Wxb2010}$](https://www.luogu.com.cn/user/1415227 "博客园:https://www.cnblogs.com/HBoier")[$\,\bf\sf\small\colorbox{#fe4c61}{\color{white}{rp++}}$](https://help.luogu.com.cn/manual/luogu/account/user-tag):::# 离线算法## 普通莫队```cppint n, m, k, bl, l = 1, r = 0, ans, a[N], cnt[N], res[N];struct S {int l, r, i;36 行未改动return 0;}```## 带修莫队```cppint n, m, bl, l = 1, r = 0, t, cntq, cntm, ans, a[N], cnt[N], res[N];char op;struct S {56 行未改动return 0;}```## 回滚莫队(不删除莫队)```cppint n, m, bl, l = 1, r = 0, num, ans, lans, a[N], cnt[N], res[N];int val[N], t[N];vector <int> vis, upd;308 行未改动全部展开#endif /* _LIBICONV_H */```## __gnu__pbds 哈希报错```cpp#include <functional> // 需要导入的头文件```加在你的代码里(以下四选一)```cppnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {6 行未改动};}}``````cppnamespace std {namespace tr1 {template <>struct hash <std::pair<int, int> > {6 行未改动};}}``````cppnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {14 行未改动};}}``````cppnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {6 行未改动}}```## ANSI 转义 - 控制台颜色调整```cpp// ANSI 颜色代码class Color {public:// 前景色260 行未改动全部展开12345669 行未改动7677787980818283848586878889909192939460 行未改动1551561571581591601611621637 行未改动1711721731741751761771781791801811821836 行未改动19019119219319419519619719819920020120220320420520620720820935 行未改动24524624724824925025125225318 行未改动27227327427527627727827928028128228328428528628728812 行未改动3013023033043053063073083092 行未改动31231331431531631731831932048 行未改动3693703713723733743753763773 行未改动3813823833843853863873883895 行未改动3953963973983994004014024033 行未改动4074084094104114124134144154164174184194204214224234244254264274284294304314324333 行未改动4374384394404414424434444452 行未改动4484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824835 行未改动4894904914924934944954964976 行未改动50450550650750850951051151221 行未改动53453553653753853954054154225 行未改动56856957057157257357457557649 行未改动6266276286296306316326336347 行未改动64264364464564664764864965089 行未改动74074174274374474574674774884 行未改动83383483583683783883984084110 行未改动85285385485585685785885986010 行未改动87187287387487587687787887935 行未改动91591691791891992092192292392411 行未改动93693793893994094194294394452 行未改动99799899910001001100210031004100519 行未改动10251026102710281029103010311032103311 行未改动1045104610471048104910501051105210533 行未改动10571058105910601061106210631064106532 行未改动10981099110011011102110311041105110674 行未改动11811182118311841185118611871188118928 行未改动12181219122012211222122312241225122611 行未改动12381239124012411242124312441245124612477 行未改动12551256125712581259126012611262126317 行未改动1281128212831284128512861287128812897 行未改动1297129812991300130113021303130413051306130713081309131016 行未改动13271328132913301331133213331334133554 行未改动13901391139213931394139513961397139864 行未改动14631464146514661467146814691470147155 行未改动15271528152915301531153215331534153542 行未改动15781579158015811582158315841585158644 行未改动1631163216331634163516361637163816394 行未改动16441645164616471648164916501651165251 行未改动170417051706170717081709171017111712116 行未改动全部展开18291830183118321833183418351836183759 行未改动18971898189919001901190219031904190546 行未改动19521953195419551956195719581959196023 行未改动19841985198619871988198919901991199224 行未改动20172018201920202021202220232024202526 行未改动20522053205420552056205720582059206020612062206320642065206638 行未改动21052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621475 行未改动21532154215521562157215821592160216122 行未改动2184218521862187218821892190219121922193219421954 行未改动220022012202220322042205220622072208553 行未改动全部展开276227632764276527662767276827692770366 行未改动全部展开31373138313931403141314231433144314560 行未改动3206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432357 行未改动3243324432453246324732483249325032515 行未改动3257325832593260326132623263326432653 行未改动32693270327132723273327432753276327773 行未改动335133523353335433553356335733583359497 行未改动全部展开385738583859386038613862386338643865321 行未改动全部展开418741884189419041914192419341944195109 行未改动全部展开430543064307430843094310431143124313143 行未改动全部展开445744584459446044614462446344644465325 行未改动全部展开47914792479347944795479647974798479917 行未改动481748184819482048214822482348244825125 行未改动全部展开49514952495349544955495649574958495936 行未改动49964997499849995000500150025003500456 行未改动506150625063506450655066506750685069308 行未改动全部展开53785379538053815382538353845385538653875388538953906 行未改动5397539853995400540154025403540454056 行未改动54125413541454155416541754185419542014 行未改动5435543654375438543954405441544254436 行未改动545054515452545354545455545654575458260 行未改动全部展开**注意:文章内容量较大,网页内存占用可能达到数 MB。由于浏览器性能原因,渲染耗时可能较长。**# Tips> Tips:> - [ ] 你数组开小了吗?> - [ ] 你 MLE 了吗?69 行未改动——[$\bf\sf\color{#3498db}{sszcdjr}$](https://www.luogu.com.cn/user/820056 "月亮好闪,拜谢月亮!"):::# 预处理## 主函数模板```cpp line-numbers#include <bits/stdc++.h>using namespace std;signed main() {return 0;}```## 主函数模板(加强版)```cpp line-numbers#define _USE_MATH_DEFINES#include <bits/stdc++.h>#include <bits/extc++.h>//#define int long long60 行未改动- [ ] 指针型数据结构改为数组型?(例如线段树)- [ ] 关同步流 / 快读快写- [ ] 避免除法和取模- [ ] 分块的块大小是否合理?(例如带修莫队板子,块大小需要为 $n^{\frac{2}{3}}$ 才可以,$\sqrt{n}$ 过不了)- [ ] 试试 C++ 14 (GCC 9),很多卡常的题换上就过了,据说“常数 $\times 0.7$”。- [ ] 运算能二进制的改成二进制(例如线段树 tr[pos << 1] tr[pos << 1 | 1])- [ ] 减少函数传参- [ ] 评测机波动?- [ ] endl 换成 `\n`7 行未改动- [ ] 厌氧?- [ ] 火车头?(例如:指令集优化,fread,编译优化,请勿滥用)- [ ] i++ 改成 ++i- [ ] 无符号整数更快?- [ ] 递归函数改成非递归- [ ] 循环展开?(费手,不推荐)## 快读板子```cpp line-numbersint read() { // 快读int x = 0, f = 1;char c = getchar();while (c < '0' || c > '9') {if (c == '-') f = -1;6 行未改动return x * f;}```## 快写板子```cpp line-numbersvoid print(int x) { // 快写if (x < 0) {putchar('-');x = -x;}if (x > 9) print(x / 10);putchar(x % 10 + '0');}```## 指令集优化```cpp line-numbers#pragma GCC target("mmx,sse,sse2,sse3,ssse3") // 基础SSE#pragma GCC target("sse4.1,sse4.2,popcnt,lzcnt") // SSE4#pragma GCC target("avx,bmi,bmi2,f16c,fma,fma4,xop") // AVX#pragma GCC target("avx2,avx512f,avx512cd") // AVX2/AVX51235 行未改动#define SIMD_AVAILABLE 0#endif```## 编译设置(洛谷等 OJ 限制使用 `#pragma GCC optimize`)```cpp line-numbers#pragma GCC optimize("Ofast,unroll-loops,inline,fast-math,O3")#pragma GCC optimize("unsafe-math-optimizations,finite-math-only")#pragma GCC optimize("no-signed-zeros,no-trapping-math,associative-math")#pragma GCC optimize("no-stack-protector,omit-frame-pointer")18 行未改动#pragma GCC diagnostic ignored "-Wunused-variable"#pragma GCC diagnostic ignored "-Wsign-compare"```## 关闭同步流```cpp line-numbers#include <fstream>ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);```# 数学操作## 随机数```cpp line-numbersunsigned long long get_seed() {auto now = chrono::system_clock::now();auto timestamp = chrono::duration_cast <chrono::nanoseconds>(now.time_since_epoch());return static_cast <unsigned long long> (timestamp.count());12 行未改动return l + Rand(r - l);}```## 判断质数```cpp line-numbersbool Ip(int n) {if (n < 2) return false;if (n == 2) return true;for (int i = 2;i * i <= n;i++) {2 行未改动return true;}```## 判断质数(Miller Rabin)```cpp line-numbersconst int test_time = 10;unsigned long long get_seed() {auto now = chrono::system_clock::now();48 行未改动| 20 | $(4^{-20} \approx 9.09 \times 10^{-13}) (\approx 0.000000000091\%)$ | $\approx 100%$ |建议:不小于 $10$ 即可。## 输出因数```cpp line-numbersvoid of(int n) {if (n < 1) return;if (n == 1) {cout << "1" << endl;3 行未改动if (n % i == 0) cout << i << endl;}```## 分解质因数```cpp line-numbersvector <int> Gif(int n) {vector <int> result;for (int i = 2;i * i <= n;i++) {if (n % i == 0) {5 行未改动return result;}```## 倒转数```cpp line-numbersint rv(int n) {int t = 0;while (n > 0) {t *= 10;3 行未改动return t;}```## 阶乘```cpp line-numbersint fact(int n) {int ans = 1;for (int i = 2;i <= n;i++) ans *= i;return ans;}```## 排列组合```cpp line-numbersint C(int n, int m) {return fact(n) / fact(m) / fact(n - m);}int A(int n, int m) {return fact(n) / fact(n - m);}```## 快速幂```cpp line-numbersint qp(int a, int b, int mod) {int ans = 1;while (b) {if (b & 1) ans = (ans * a) % mod;3 行未改动return ans;}```## 龟速乘```cpp line-numbersint sm(int a, int b, int mod) {int ans = 1;while (b) {if (b & 1) ans = (ans + a) % mod;2 行未改动}}```## 快速乘```cpp line-numbersint qm(int a, int b, int mod) {int c = a * b - ((long double)a / mod * b + 0.5L) * mod;if (c < 0) c += mod;return c + mod;}```## 勾股定理```cpp line-numbersdouble pt(double a, double b) {return sqrt(pow(a, 2) + pow(b, 2));}```## 两点距离```cpp line-numbersdouble dist(double a1, double a2, double b1, double b2) {return sqrt(pow(fabs(a1 - b1), 2) + pow(fabs(a2 - b2), 2));}```## 海伦-秦九韶公式```cpp line-numbersdouble s(double a, double b, double c) {double p = (a + b + c) / 2;return sqrt(p * (p - a) * (p - b) * (p - c));}```## 线性逆元```cpp line-numbersint n, p, inv[N], factinv[N];signed main() {cin >> n >> p;5 行未改动return 0;}```## 扩展欧几里得(Exgcd)```cpp line-numbersint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;6 行未改动return r;}```## 中国剩余定理(CRT)```cpp line-numbersint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;21 行未改动cout << (s + ans) % ans << endl;```## 扩展中国剩余定理(ExCRT)```cpp line-numbersint exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1;y = 0;25 行未改动}cout << (Y % X + X) % X << endl;```## 手写根号```cpp line-numbers#define exp 0.000001// 二分法double binsearch_sqrt(int m) {49 行未改动return 1 / y;}```## 线性基```cpp line-numbersstruct LinearBase {int dat[N];void insert(int x) {for (int i = 63;~i;i--) {7 行未改动}} lb;```## 日期计算```cpp line-numbersstruct _time {int y, m, d, h, min, s;int mmod[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};89 行未改动}};```## Base 64 编码 / 解码```cpp line-numbers#include <bits/stdc++.h>using namespace std;static const string base64_chars =84 行未改动return 0;}```## 欧拉(线性)质数筛```cpp line-numbersint pr[N];bool ip[N];int gp(int n) {10 行未改动return p;}```## 归并排序```cpp line-numbersint a[N], b[N], c[N], ans1, ans2, n, _;void msort(int l, int r, int &ans, auto &A, auto &B) {if (l == r) return;10 行未改动for (int k = l;k <= r;k++) A[k] = B[k];}```## 矩阵快速幂 / 矩阵乘法```cpp line-numbers#include <bits/stdc++.h>using namespace std;const ll N = 1e2 + 2, mod = 1e9 + 7;35 行未改动}return 0;}```## 快速位运算```cpp line-numbersint getor(int l, int r) {if (l == r) return l;return l | ((1 << ( __lg(l ^ r) + 1)) - 1);}11 行未改动if (x % 4 == 3) return 0;}```## 快速傅里叶变换(FFT / IFFT)```cpp line-numbersstruct Complex {double x, y;Complex(double _x = 0.0, double _y = 0.0) {52 行未改动if (on == -1) for (int i = 0;i < len;i++) y[i].x /= len, y[i].y /= len;}```## 快速数论变换(NTT)```cpp line-numbersvoid ntt(int *x, int lim, int opt) {int k, gn, g, tmp;for (int i = 0; i < lim; ++i)19 行未改动}```# 字符串操作## 树的遍历```cpp line-numbersvoid pre(int x) {cout << x << " ";if (a[x][1]) pre(a[x][1]);if (a[x][2]) pre(a[x][2]);11 行未改动cout << x << " ";}```## 判断回文字符串```cpp line-numbersbool is_hw(string s) {int left = 0, right = s.size() - 1;while (left < right) {if (s[left] != s[right]) return false;3 行未改动return true;}```## trie 字典树```cpp line-numbersint trie[N][MN];int tot, sum[N];void insert(string s) { //插入一个字符串s32 行未改动sum[root] -= fa;}```## AC 自动机```cpp line-numbersnamespace AC {struct S {int son[27], ans, fail, du, id;void init() {74 行未改动return 0;}```## 回文自动机(PAM)```cpp line-numbersstruct Palindrome_Automaton {int ch[N][LN], fail[N], len[N], sum[N], cnt, last;Palindrome_Automaton() {cnt = 1;28 行未改动return 0;}```## Manacher 最长回文串```cpp line-numbersstring s = "~|";char c;int p[N], ans, r = 0, mid = 0;11 行未改动return 0;}```## 手写字符串 hash**热知识**:一定要用自己的模数,常用模数和自然溢出被卡遍了。```cpp line-numbersconst ull M = 1610612741, P = 131;ull h[2000005];7 行未改动hash <string> _hash;```## 双模数 hash```cpp line-numbersconst ull mod1 = 1610612741, mod2 = 402653189, p = 131;struct s {ull x, y;17 行未改动return ans;}```## 字符串任意子段 hash```cpp line-numbersull h[N], p[N];void init_Hash(string s) {int n = s.size();7 行未改动return h[pos + len] - h[pos] * p[len];}```## hash 质数表```cpp line-numbersull M[] = {0, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741};```## 字符串 KMP```cpp line-numbersvector <int> prefix(string s) {int n = s.size();vector <int> pi(n);for (int i = 1;i < n;i++) {16 行未改动}```# 图论算法## SPFA 最短路```cpp line-numbersint dis[N];int h[N], to[N], val[N], nxt[N], cnt, n, m, s;int cnt_in_queue[N];bool vis[N];54 行未改动return 0;}```## SPFA 优化版(SLF + LLL + 随机化)```cpp line-numbersint dis[N];int h[N], to[N], val[N], nxt[N], tot[N], cnt, n, m, s, t, x, y, z;bool vis[N];64 行未改动return false;}```## SPFA Hack 数据生成器```cpp line-numbersunsigned long long get_seed() {auto now = chrono::system_clock::now();auto timestamp = chrono::duration_cast <chrono::nanoseconds>(now.time_since_epoch());return static_cast <unsigned long long> (timestamp.count());55 行未改动return 0;}```## Bellman-Ford 最短路```cpp line-numbersstruct Edge {int u, v, w;};42 行未改动return 0;}```## Dijkstra 最短路```cpp line-numbersint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z, s, t;bool vis[N];struct node {44 行未改动return 0;}```## Floyd 最短路```cpp line-numbersint n, m, x, y, z, dp[N][N];void Floyd() {for (int k = 1;k <= n;k++)4 行未改动memset(dp, 0x3f, sizeof(dp));```## Prim 最小生成树```cpp line-numbersint dis[N], h[N], to[N], val[N], nxt[N], ans, cnt, n, m;bool vis[N];struct node {51 行未改动return 0;}```## Kruskal 最小生成树```cpp line-numbersstruct Edge {int u, v, w;bool operator < (const Edge& other) const {return w < other.w;116 行未改动全部展开return 0;}```## Dinic 网络最大流```cpp line-numbersint h[N], to[N], val[N], nxt[N], dis[N], now[N], cnt, n, m, x, y, z, s, t, maxflow, flow;void add(int a, int b, int c) {to[++cnt] = b;59 行未改动return 0;}```## Dinic 二分图匹配```cpp line-numbersint h[N], to[N], val[N], nxt[N], dis[N], now[N], cnt = 1, n, m, x, y, z, s, t, maxflow, flow;void add(int a, int b, int c) {to[++cnt] = b;46 行未改动return flow - rest;}```## tarjan 割边 / 割点```cpp line-numbersint dfn[N], low[N], dfncnt, tot;int dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;map <int, bool> cut_v;map <pair <int, int>, bool> cut_e;23 行未改动if (flag >= 2 && u == root) cut_v[u] = 1;}```## tarjan 强连通分量```cpp line-numbersint dfn[N], low[N], tot, dfncnt;int dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;bool vis[N];stack <int> S;24 行未改动}}```## 拓扑排序```cpp line-numbersint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, x, y, z;int in[N];void add(int a, int b, int c) {26 行未改动return topoOrder;}```## 最近公共祖先(LCA)### 倍增法```cpp line-numbersint dis[N], h[N], to[N], val[N], nxt[N], cnt, n, m, s, x, y, z, ans;int f[N][32], cost[N][32], dep[N];bool is_find;map <int, bool> vis;void add(int a, int b, int c) {to[++cnt] = b;val[cnt] = c;nxt[cnt] = h[a];38 行未改动x = f[x][j], y = f[y][j];return f[x][0];}```### DFS 法```cpp line-numbersint get(int x, int y) {return dfn[x] < dfn[y] ? x : y;}void dfs(int u, int fa) {dfn[u] = ++tot;st[0][dfn[u]] = fa;dep[u] = dep[fa] + 1;for (int i = h[u];i;i = nxt[i]) {int v = to[i];if (v == fa) continue;dfs(v, u);}}void init() {dfs(s, 0);for (int i = 1;i <= 22;i++)for (int j = 1;j + ((1 << i) - 1) <= tot;j++)st[i][j] = get(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);}int lca(int u, int v) {if (u == v) return u;int x = dfn[u], y = dfn[v];if (x > y) swap(x, y);int d = __lg(y - x);return get(st[d][x + 1], st[d][y - (1 << d) + 1]);}```# 小型数据结构## 并查集```cpp line-numbersint f[N];int find(int x) {if (f[x] == x) return x;5 行未改动if (fx != fy) f[fx] = fy;}```## ST 表```cpp line-numbers#include <iostream>using namespace std;const int N = 2e5 + 2, Logn = 22;22 行未改动}```# 树形数据结构## 树状数组```cpp line-numbersint s[N];int lowbit(int x) {return x & (-x);}void modify(int p, int v) {for(;p <= n;p += lowbit(p)) s[p] += v;}4 行未改动return res;}```## 线段树(指针版)```cpp line-numbersint n, m, mod = inf;int a[N];struct Segment_Tree {553 行未改动全部展开return 0;}```## 线段树(数组版)```cpp line-numbersint n, m, mod = inf;int a[N];struct Segment_Tree {366 行未改动全部展开return 0;}```## 线段树(加法版)```cpp line-numbersint n, m, mod;int a[N];struct Segment_Tree {60 行未改动return val;}```## 线段树(最小值版)```cpp line-numbersstruct Segment_Tree {int add;int min_val;int l, r;} s[N << 2];void pushup(int pos) {s[pos].min_val = min(s[pos << 1].min_val, s[pos << 1 | 1].min_val);}void pushdown(int pos) {s[pos << 1].min_val += s[pos].add;s[pos << 1].add += s[pos].add;s[pos << 1 | 1].min_val += s[pos].add;s[pos << 1 | 1].add += s[pos].add;s[pos].add = 0;}void build(int pos, int l, int r) {s[pos].l = l;s[pos].r = r;s[pos].add = 0;7 行未改动build(pos << 1 | 1, mid + 1, r);pushup(pos);}void add(int pos, int x, int y, int k) {if (x <= s[pos].l && s[pos].r <= y) {s[pos].min_val += k;s[pos].add += k;return;5 行未改动if (y > mid) add(pos << 1 | 1, x, y, k);pushup(pos);}int query_min(int pos, int x, int y) {if (x <= s[pos].l && s[pos].r <= y) return s[pos].min_val;pushdown(pos);int mid = (s[pos].l + s[pos].r) >> 1;3 行未改动return res;}```## gcd 线段树```cpp line-numbersint n, b[N];struct Segment_Tree {int sum;73 行未改动return __gcd(a_l, tail_g);}```## 可持久化线段树(主席树)- 指针版```cpp line-numbersint n, m, mod = inf;int a[N];vector <int> sorted; // 用于离散化的排序数组497 行未改动全部展开return 0;}```## 可持久化线段树(主席树)- 数组版```cpp line-numbersint n, m, mod = inf;int a[N];vector <int> sorted; // 离散化数组321 行未改动全部展开return 0;}```## Treap```cpp line-numbersint na;int ch[N][2];int val[N], dat[N];int sz[N], cnt[N];109 行未改动全部展开}```## Splay 伸展树from oi-wiki```cpp line-numbersint n, id, rt;int fa[N], val[N], cnt[N], sz[N], ch[N][2];bool dir(int x) {143 行未改动全部展开return 0;}```## 树套树(线段树套 Splay)```cpp line-numbers// Splay Tree Node Structurestruct SplayNode {int val, cnt, sz;SplayNode *ch[2], *fa;325 行未改动全部展开return 0;}```## 笛卡尔树```cpp line-numbersint p[N], l[N], r[N], n;void build(int n) {stack <int> stk;17 行未改动return 0;}```## 树链剖分```cpp line-numbers#define lc o << 1#define rc o << 1 | 1int n, a, b, w[N], q, u, v;125 行未改动全部展开——[$\bf\sf\color{#fe4c61}{Wxb2010}$](https://www.luogu.com.cn/user/1415227 "博客园:https://www.cnblogs.com/HBoier")[$\,\bf\sf\small\colorbox{#fe4c61}{\color{white}{rp++}}$](https://help.luogu.com.cn/manual/luogu/account/user-tag):::# 离线算法## 普通莫队```cpp line-numbersint n, m, k, bl, l = 1, r = 0, ans, a[N], cnt[N], res[N];struct S {int l, r, i;36 行未改动return 0;}```## 带修莫队```cpp line-numbersint n, m, bl, l = 1, r = 0, t, cntq, cntm, ans, a[N], cnt[N], res[N];char op;struct S {56 行未改动return 0;}```## 回滚莫队(不删除莫队)```cpp line-numbersint n, m, bl, l = 1, r = 0, num, ans, lans, a[N], cnt[N], res[N];int val[N], t[N];vector <int> vis, upd;308 行未改动全部展开#endif /* _LIBICONV_H */```## __gnu__pbds 哈希报错```cpp line-numbers#include <functional> // 需要导入的头文件```加在你的代码里(以下四选一)```cpp line-numbersnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {6 行未改动};}}``````cpp line-numbersnamespace std {namespace tr1 {template <>struct hash <std::pair<int, int> > {6 行未改动};}}``````cpp line-numbersnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {14 行未改动};}}``````cpp line-numbersnamespace std {namespace tr1 {template <>struct hash <std::pair <int, int> > {6 行未改动}}```## ANSI 转义 - 控制台颜色调整```cpp line-numbers// ANSI 颜色代码class Color {public:// 前景色260 行未改动全部展开