版本比较
模板
正文
增加 540 行删除 130 行
2 行未改动3456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657131415165859606152 行未改动114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166118 行未改动全部展开28528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332422 行未改动3473483493502353513523533543553563573583592443603612463623633643653663673683693703713723733743753762492502513773783793803813823833843853863873883893903913922532542552562573933943953963973983994004014024034044054064074084094104114124134144152592604164172622634184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494502662672682692702714514522732742752762774534544554564574584594604614624634644654664674684694704714724732792802812822832842852864744754764774784794804814824834844854864872892902912924884892944904912962972984924934944954964974984995005015025033003015045055065075085093033043053063073085105115125135145155165175185195205215225235245255265273103113123133143153165285295305313183193203213223233243253263273285325335343303315353333343353363375365375385395403393403413423435415425435445455465475485495505515525535545555565575583455595603473483493503515615625635645655665675685695705715725735745755765775785795803535815823553563573583593605835845855865875885895905915925935945955963625975985993643653663673683693706006016026036046056066076086096103726116126136146156166176183743753763773786196206216226236246256266276286296306316326336346353806366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886893843853866903883893906916923923936936946956966973953966986997007013983997027037047057064014024034047077087097104064077117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188192 行未改动## 最短路
### dijkstra
```cppvoid dijkstra() { memset(dis, 0x7f, sizeof dis); dis[s] = 0; q.push({s, 0}); while (q.size()) { int u = q.top().v; q.pop(); if (vis[u]) continue; vis[u] = 1; for (auto [v, w] : g[u]) { if (dis[u] + w < dis[v]) { dis[v] = dis[u] + w; q.push({v, dis[v]}); } } }}```
### SPFA
```cppvoid spfa() { for (int i = 1; i <= n; i++) dis[i] = INT_MAX; dis[s] = 0; vis[s] = 1; q.push(s); while (q.size()) { int u = q.front(); q.pop(); vis[u] = 0; for (auto [v, w] : g[u]) { if (dis[u] + w < dis[v]) { dis[v] = dis[u] + w; if (!vis[v]) vis[v] = 1, q.push(v); } } }}```
## 生成树
### Prim
#### 原版
#### 堆优化
```cppstruct node { int v, w; bool operator>(const node &x)const {52 行未改动 }}```
# 字符串
## KMP
```cppvoid kmp() { for (int i = 2, j = 0; i <= m; i++) { while (j && s2[i] != s2[j + 1]) j = nxt[j]; if (s2[i] == s2[j + 1]) j++; nxt[i] = j; } for (int i = 1, j = 0; i <= n; i++) { while (j && s1[i] != s2[j + 1]) j = nxt[j]; if (s1[i] == s2[j + 1]) j++; if (j == m) j = nxt[j]; }}```
## Manacher
```cppvoid manacher() { t = "@ "; for (char c : s) t += c, t += " "; t += "~"; int n = t.size(); for (int i = 1; i < n - 1; i++) { p[i] = i < r ? min(p[2 * mid - i], r - i) : 1; while (t[i - p[i]] == t[i + p[i]]) p[i]++; if (i + p[i] - 1) { mid = i; r = i + p[i] - 1; } }}```
# 数据结构
## 并查集
118 行未改动全部展开 return aaa.end(); }};```## 二维树状数组
``` cppvoid add(int x, int y, int v) { for (int i = x; i <= n; i += lowbit(i)) for (int j = y; j <= m; j += lowbit(j)) { tree[i][j][0] += v; tree[i][j][1] += v * x; tree[i][j][2] += v * y; tree[i][j][3] += v * x * y; }}void range_add(int x1, int y1, int x2, int y2, int v) { add(x1, y1, v); add(x2 + 1, y2 + 1, v); add(x1, y2 + 1, -v); add(x2 + 1, y1, -v);}int caory(int x, int y) { int re = 0; for (int i = x; i; i -= lowbit(i)) for (int j = y; j; j -= lowbit(j)) re += (x + 1) * (y + 1) * tree[i][j][0] - (y + 1) * tree[i][j][1] - (x + 1) * tree[i][j][2] + tree[i][j][3]; return re;}int range_caory(int x1, int y1, int x2, int y2) { return caory(x2, y2) - caory(x2, y1 - 1) - caory(x1 - 1, y2) + caory(x1 - 1, y1 - 1);}```
# 数学
## 快速幂22 行未改动 isprime[1] = 0; for (int i = 2; i <= n; i++) { if (isprime[i]) prime.push_back(i); for (int j = 1; j <= prime.size() && i * prime[j] <= n; j++) { for (size_t j = 1; j < prime.size() && i * prime[j] <= n; j++) { isprime[i * prime[j]] = 0; if (i % prime[j] == 0) break; } }}```
# 其他## exgcd
## 快读快输``` cppint exgcd(int a, int b, int &x, int &y) { if (!b) { x = 1, y = 0; return a; } int re = exgcd(b, a % b, y, x); y -= a / b * x; return re;}```
## 中国剩余定理(CRT)
```cpp#include <cstdio>#include <cctype>#include <string>int crt() { int M = 1, ans = 0; for (int i = 1; i <= n; i++) M *= m[i]; for (int i = 1; i <= n; i++) { int mi = M / m[i]; int x, y; exgcd(mi, m[i], x, y); x = (x % m[i] + m[i]) % m[i]; ans = (ans + (__int128)a[i] * mi % M * x % M) % M; } return ans;}```## EXCRT
struct FastIO {private: static const int BUFSIZE = 1 << 20; char ibuf[BUFSIZE], *ip1, *ip2; char obuf[BUFSIZE], *op;``` cppfc unite(fc a1, fc a2) { int p, q; int d = exgcd(a1.m, a2.m, p, q); if ((a2.a - a1.a) % d) return {-1, -1}; int step = a2.m / d; p = ((__int128)p * (a2.a - a1.a) / d % step + step) % step; int next_m = lcm(a1.m, a2.m); int next_a = ((__int128)a1.m * p + a1.a) % next_m; return {next_m, next_a};}int excrt() { fc ans = a[1]; for (int i = 2; i <= n; i++) { ans = unite(ans, a[i]); if (ans.m == -1) return -1; } return ans.a;}```
public: FastIO() : ip1(ibuf), ip2(ibuf), op(obuf) {}## 矩阵
~FastIO() { flush();```cppstruct matrix { int mat[105][105]; int r, c; matrix () { memset(mat, 0, sizeof mat); } int *operator[](int i) { return mat[i]; } matrix operator *(matrix &b) const { matrix re; re.r = r, re.c = b.c; for (int i = 1; i <= r; i++) for (int k = 1; k <= c; k++) for (int j = 1; j <= b.c; j++) re[i][j] = (re[i][j] + mat[i][k] * b[k][j]) % mod; return re; }};matrix fpow(matrix a, long long b) { matrix re; re[0][0] = re[1][1] = 1; while (b) { if (b & 1) re = re * a; a = a * a; b >>= 1; } return re;}```
// 读取整数 template<typename T> FastIO& operator>>(T& x) { x = 0; bool sign = false; char ch = getchar();## 高斯消元
while (isspace(ch)) ch = getchar();
if (ch == '-') { sign = true; ch = getchar();```cppint Gauss() { int r = 1; for (int c = 1; c <= n; c++) { int cur = r; for (int i = r; i <= n; i++) if (fabs(a[i][c]) > fabs(a[cur][c])) cur = i; if (fabs(a[cur][c]) < eps) continue; swap(a[r], a[cur]); double div = a[r][c]; for (int i = c; i <= n + 1; i++) a[r][i] /= div; for (int i = 1; i <= n; i++) { if (i == r) continue; double mul = a[i][c]; for (int j = c; j <= n + 1; j++) a[i][j] -= mul * a[r][j]; }
while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); }
if (sign) x = -x; return *this; r++; } if (r <= n) { for (int i = r; i <= n; i++) if (fabs(a[i][n + 1]) > eps) return -1; return 0; } for (int i = 1; i <= n; i++) ans[i] = a[i][n + 1]; return 1;}```
// 读取字符串(到空白字符停止) FastIO& operator>>(std::string& s) { s.clear(); char ch = getchar();# 其他
while (isspace(ch)) ch = getchar();## 高精度
while (!isspace(ch) && ch != EOF) { s += ch; ch = getchar();```cppstruct BigInt { vector<int> digits; bool is_neg = false; BigInt() : digits(1, 0), is_neg(false) {} BigInt(long long v) { if (v < 0) { is_neg = true; v = -v; } else { is_neg = false; }
return *this; if (v == 0) digits.push_back(0); while (v > 0) { digits.push_back(v % 10); v /= 10; } }
// 读取字符 FastIO& operator>>(char& c) { c = getchar(); while (isspace(c)) c = getchar(); return *this; BigInt(const string& s) { is_neg = false; if (s.empty()) { digits.push_back(0); return; } int start = 0; if (s[0] == '-') { is_neg = true; start = 1; } else if (s[0] == '+') { start = 1; } for (int i = s.length() - 1; i >= start; i--) { digits.push_back(s[i] - '0'); } trim(); }
// 输出整数 template<typename T> FastIO& operator<<(T x) { if (x < 0) { putchar('-'); x = -x; void trim() { while (digits.size() > 1 && digits.back() == 0) { digits.pop_back(); }
static char stk[20]; int top = 0;
do { stk[top++] = x % 10 + '0'; x /= 10; } while (x);
while (top) { putchar(stk[--top]); if (digits.size() == 1 && digits[0] == 0) { is_neg = false; }
return *this; }
// 输出字符 FastIO& operator<<(char c) { putchar(c); return *this; BigInt abs() const { BigInt res = *this; res.is_neg = false; return res; }
// 输出C风格字符串 FastIO& operator<<(const char* s) { while (*s) { putchar(*s++); friend BigInt abs(const BigInt& a) { return a.abs(); } BigInt operator-() const { if (digits.size() == 1 && digits[0] == 0) return *this; BigInt res = *this; res.is_neg = !res.is_neg; return res; } static bool abs_less(const BigInt& a, const BigInt& b) { if (a.digits.size() != b.digits.size()) { return a.digits.size() < b.digits.size(); } for (int i = (int)a.digits.size() - 1; i >= 0; i--) { if (a.digits[i] != b.digits[i]) { return a.digits[i] < b.digits[i]; } } return *this; return false; }
// 输出std::string FastIO& operator<<(const std::string& s) { for (char c : s) { putchar(c); bool operator<(const BigInt& b) const { if (is_neg != b.is_neg) return is_neg; if (is_neg) return abs_less(b, *this); return abs_less(*this, b); } bool operator>(const BigInt& b) const { return b < *this; } bool operator<=(const BigInt& b) const { return !(*this > b); } bool operator>=(const BigInt& b) const { return !(*this < b); } bool operator==(const BigInt& b) const { return is_neg == b.is_neg && digits == b.digits; } bool operator!=(const BigInt& b) const { return !(*this == b); } static BigInt add_unsigned(const BigInt& a, const BigInt& b) { BigInt res; res.digits.clear(); int carry = 0; for (size_t i = 0; i < max(a.digits.size(), b.digits.size()) || carry; ++i) { if (i < a.digits.size()) carry += a.digits[i]; if (i < b.digits.size()) carry += b.digits[i]; res.digits.push_back(carry % 10); carry /= 10; } return *this; return res; }
// 刷新缓冲区 void flush() { if (op != obuf) { fwrite(obuf, 1, op - obuf, stdout); op = obuf; static BigInt sub_unsigned(const BigInt& a, const BigInt& b) { BigInt res; res.digits.clear(); int borrow = 0; for (size_t i = 0; i < a.digits.size(); ++i) { int diff = a.digits[i] - borrow - (i < b.digits.size() ? b.digits[i] : 0); if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } res.digits.push_back(diff); } fflush(stdout); res.trim(); return res; }
private: char getchar() { if (ip1 == ip2) { ip2 = ibuf + fread(ibuf, 1, BUFSIZE, stdin); ip1 = ibuf; if (ip1 == ip2) return EOF; BigInt operator+(const BigInt& b) const { if (is_neg == b.is_neg) { BigInt res = add_unsigned(*this, b); res.is_neg = is_neg; res.trim(); return res; } if (abs_less(*this, b)) { BigInt res = sub_unsigned(b, *this); res.is_neg = b.is_neg; res.trim(); return *ip1++; return res; } else { BigInt res = sub_unsigned(*this, b); res.is_neg = is_neg; res.trim(); return res; } }
void putchar(char c) { if (op - obuf == BUFSIZE) { fwrite(obuf, 1, BUFSIZE, stdout); op = obuf; BigInt operator-(const BigInt& b) const { return *this + (-b); } BigInt operator*(const BigInt& b) const { BigInt res; res.digits.assign(digits.size() + b.digits.size(), 0); for (size_t i = 0; i < digits.size(); ++i) { long long carry = 0; for (size_t j = 0; j < b.digits.size(); ++j) { long long cur = res.digits[i + j] + (long long)digits[i] * b.digits[j] + carry; res.digits[i + j] = cur % 10; carry = cur / 10; } if (carry > 0) { res.digits[i + b.digits.size()] += carry; } } *op++ = c; res.is_neg = is_neg ^ b.is_neg; res.trim(); return res; } BigInt operator/(const int& b) const { BigInt res; res.digits.clear(); long long current = 0; int abs_b = std::abs(b); for (int i = digits.size() - 1; i >= 0; i--) { current = current * 10 + digits[i]; res.digits.push_back(current / abs_b); current %= abs_b; } reverse(res.digits.begin(), res.digits.end()); res.is_neg = is_neg ^ (b < 0); res.trim(); return res; } long long operator%(const int& b) const { long long current = 0; int abs_b = std::abs(b); for (int i = digits.size() - 1; i >= 0; i--) { current = (current * 10 + digits[i]) % abs_b; } return is_neg ? -current : current; } BigInt pow(long long n) const { BigInt res(1); BigInt base = *this; while (n > 0) { if (n % 2 == 1) res = res * base; base = base * base; n /= 2; } return res; } BigInt operator^(const long long& n) const { return this->pow(n); } friend istream& operator>>(istream& in, BigInt& a) { string s; in >> s; a = BigInt(s); return in; } friend ostream& operator<<(ostream& out, const BigInt& a) { if (a.is_neg) out << '-'; for (int i = a.digits.size() - 1; i >= 0; i--) { out << a.digits[i]; } return out; }};```
// 使用示例int main() { FastIO io;
int a, b; std::string s; char c;## 快读快输
// 读取整数 io >> a >> b;```cpp#include <iostream>#include <cstdio>#include <string>
// 读取字符串 io >> s;// ---------------- 核心 FastIO 模板 ----------------namespace FastIO { const int BUF_SIZE = 1 << 20; // 1MB 缓冲区
// 读取字符 io >> c; // 输入类,模拟 std::cin class FastIn { private: char buf[BUF_SIZE], *p1, *p2;
// 输出所有结果 io << "a = " << a << ", b = " << b << "\n" << "s = " << s << "\n" << "c = " << c << "\n"; inline char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF_SIZE, stdin), p1 == p2) ? EOF : *p1++; }
// 析构函数会自动调用flush() return 0; public: FastIn() : p1(buf), p2(buf) {}
// 支持所有标准整型 (int, long long, __int128 等) template <typename T> inline FastIn& operator>>(T& x) { x = 0; int f = 1; char ch = gc(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = gc(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = gc(); } x *= f; return *this; }
// 支持读取单个字符 (自动跳过空白字符) inline FastIn& operator>>(char& c) { c = gc(); while (c == ' ' || c == '\n' || c == '\r' || c == '\t') c = gc(); return *this; }
// 支持读取 std::string (遇到空白字符停止) inline FastIn& operator>>(std::string& s) { s.clear(); char c = gc(); while (c == ' ' || c == '\n' || c == '\r' || c == '\t') c = gc(); while (c != EOF && c != ' ' && c != '\n' && c != '\r' && c != '\t') { s += c; c = gc(); } return *this; } } fin; // 实例化为 fin,代替 cin
// 输出类,模拟 std::cout class FastOut { private: char buf[BUF_SIZE], *pout;
inline void pc(char c) { if (pout - buf == BUF_SIZE) flush(); *pout++ = c; }
public: FastOut() : pout(buf) {} // 析构时自动清空缓冲区 ~FastOut() { flush(); }
inline void flush() { fwrite(buf, 1, pout - buf, stdout); pout = buf; }
// 支持所有标准整型 template <typename T> inline FastOut& operator<<(T x) { if (x < 0) { pc('-'); x = -x; } if (!x) { pc('0'); return *this; } static char stk[40]; int top = 0; while (x) { stk[++top] = (x % 10) ^ 48; x /= 10; } while (top) pc(stk[top--]); return *this; }
// 支持输出单个字符 inline FastOut& operator<<(char c) { pc(c); return *this; }
// 支持输出 C 风格字符串 inline FastOut& operator<<(const char* s) { while (*s) pc(*s++); return *this; }
// 支持输出 std::string inline FastOut& operator<<(const std::string& s) { for (char c : s) pc(c); return *this; } } fout; // 实例化为 fout,代替 cout}using FastIO::fin;using FastIO::fout;// ---------------- 模板结束 ----------------
// 测试与使用示例int main() { int a; long long b; std::string s;
// 完美复刻 cin 的链式调用 fin >> a >> b >> s;
// 完美复刻 cout 的链式调用 fout << "The sum is: " << a + b << '\n'; fout << "The string is: " << s << '\n';
// 甚至可以写循环 // int n; fin >> n; // for(int i=0; i<n; ++i) { ... }
return 0; // 程序结束时自动触发 fout 的析构函数,保证最后的数据被 flush 到屏幕}```