版本比较
模板
正文
增加 540 行删除 130 行
2 行未改动3456789101112131415161718192052 行未改动737475767778798081118 行未改动全部展开20020120220320420520620720822 行未改动2312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084092 行未改动## 最短路### dijkstra### SPFA## 生成树### Prim#### 原版#### 堆优化```cppstruct node {int v, w;bool operator>(const node &x)const {52 行未改动}}```# 数据结构## 并查集118 行未改动全部展开return aaa.end();}};```# 数学## 快速幂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++) {isprime[i * prime[j]] = 0;if (i % prime[j] == 0)break;}}}```# 其他## 快读快输```cpp#include <cstdio>#include <cctype>#include <string>struct FastIO {private:static const int BUFSIZE = 1 << 20;char ibuf[BUFSIZE], *ip1, *ip2;char obuf[BUFSIZE], *op;public:FastIO() : ip1(ibuf), ip2(ibuf), op(obuf) {}~FastIO() {flush();}// 读取整数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();}while (isdigit(ch)) {x = x * 10 + (ch - '0');ch = getchar();}if (sign) x = -x;return *this;}// 读取字符串(到空白字符停止)FastIO& operator>>(std::string& s) {s.clear();char ch = getchar();while (isspace(ch)) ch = getchar();while (!isspace(ch) && ch != EOF) {s += ch;ch = getchar();}return *this;}// 读取字符FastIO& operator>>(char& c) {c = getchar();while (isspace(c)) c = getchar();return *this;}// 输出整数template<typename T>FastIO& operator<<(T x) {if (x < 0) {putchar('-');x = -x;}static char stk[20];int top = 0;do {stk[top++] = x % 10 + '0';x /= 10;} while (x);while (top) {putchar(stk[--top]);}return *this;}// 输出字符FastIO& operator<<(char c) {putchar(c);return *this;}// 输出C风格字符串FastIO& operator<<(const char* s) {while (*s) {putchar(*s++);}return *this;}// 输出std::stringFastIO& operator<<(const std::string& s) {for (char c : s) {putchar(c);}return *this;}// 刷新缓冲区void flush() {if (op != obuf) {fwrite(obuf, 1, op - obuf, stdout);op = obuf;}fflush(stdout);}private:char getchar() {if (ip1 == ip2) {ip2 = ibuf + fread(ibuf, 1, BUFSIZE, stdin);ip1 = ibuf;if (ip1 == ip2) return EOF;}return *ip1++;}void putchar(char c) {if (op - obuf == BUFSIZE) {fwrite(obuf, 1, BUFSIZE, stdout);op = obuf;}*op++ = c;}};// 使用示例int main() {FastIO io;int a, b;std::string s;char c;// 读取整数io >> a >> b;// 读取字符串io >> s;// 读取字符io >> c;// 输出所有结果io << "a = " << a << ", b = " << b << "\n"<< "s = " << s << "\n"<< "c = " << c << "\n";// 析构函数会自动调用flush()return 0;}```2 行未改动34567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606152 行未改动114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166118 行未改动全部展开28528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332422 行未改动3473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188192 行未改动## 最短路### 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 (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)```cppint 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``` 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;}```## 矩阵```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;}```## 高斯消元```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];}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;}```# 其他## 高精度```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;}if (v == 0) digits.push_back(0);while (v > 0) {digits.push_back(v % 10);v /= 10;}}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();}void trim() {while (digits.size() > 1 && digits.back() == 0) {digits.pop_back();}if (digits.size() == 1 && digits[0] == 0) {is_neg = false;}}BigInt abs() const {BigInt res = *this;res.is_neg = false;return res;}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 false;}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 res;}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);}res.trim();return res;}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 res;} else {BigInt res = sub_unsigned(*this, b);res.is_neg = is_neg;res.trim();return res;}}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;}}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;}};```## 快读快输```cpp#include <iostream>#include <cstdio>#include <string>// ---------------- 核心 FastIO 模板 ----------------namespace FastIO {const int BUF_SIZE = 1 << 20; // 1MB 缓冲区// 输入类,模拟 std::cinclass FastIn {private:char buf[BUF_SIZE], *p1, *p2;inline char gc() {return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF_SIZE, stdin), p1 == p2) ? EOF : *p1++;}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::coutclass 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::stringinline 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 到屏幕}```