合集-模板

摘要:洛谷模板测试七倍经验: https://www.luogu.com.cn/record/238785118 https://www.luogu.com.cn/record/238783283 https://www.luogu.com.cn/record/238788990 https://www. 阅读全文
posted @ 2025-10-05 17:42 Ke_scholar 阅读(7) 评论(0) 推荐(0)
摘要:template<class Info, class Tag> struct LazySegmentTree { int n; std::vector<Info> info; std::vector<Tag> tag; LazySegmentTree() : n(0) {} LazySegmentT 阅读全文
posted @ 2025-10-01 20:52 Ke_scholar 阅读(7) 评论(1) 推荐(0)
摘要:mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); 阅读全文
posted @ 2025-09-28 22:22 Ke_scholar 阅读(10) 评论(0) 推荐(0)
摘要://无向图 int idx = 0; bool found = false; vector<int> loop,dfn(n + 1, 0), fa(n + 1, 0); function<void(int)> dfs = [&](int u) { dfn[u] = ++idx; for (int v 阅读全文
posted @ 2025-09-11 13:56 Ke_scholar 阅读(15) 评论(1) 推荐(0)
摘要:constexpr int N = 3001; vector<bitset<N>> a(N); //n个方程,m个未知数,p组常数项 vector<vector<bool>> guass(int n, int m, int p) { int rank = 0; vector<int> pivot_c 阅读全文
posted @ 2025-09-04 16:32 Ke_scholar 阅读(12) 评论(0) 推荐(0)
摘要:【牛客一道模板】 struct MidNum { multiset<int> p, q; //p 从大到小 q从小到大 void work() { while (p.size() > q.size() + 1) { q.insert(*p.rbegin()); p.erase(--p.end()); 阅读全文
posted @ 2025-08-13 19:03 Ke_scholar 阅读(20) 评论(0) 推荐(0)
摘要:洛谷模板题已过【P2617 Dynamic Rankings】 /* 动态持久化线段树(树状数组套线段树)模板类 * 用于解决带修改的区间第k小和区间排名查询问题 * 核心功能: * - 单点更新 O(log n log V) * - 区间第k小查询 O(log n log V) * - 区间排名查 阅读全文
posted @ 2025-08-12 20:39 Ke_scholar 阅读(21) 评论(1) 推荐(0)
摘要:需要C++20及以上,从Jiangly那偷得(bushi using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; using i128 = 阅读全文
posted @ 2025-08-02 16:53 Ke_scholar 阅读(14) 评论(0) 推荐(0)
摘要:auto getMin = [](vector<int> &v)->vector<int> { int n = v.size(); int k = 0, i = 0, j = 1; while (k < n && i < n && j < n) { if (v[(i + k) % n] == v[( 阅读全文
posted @ 2025-06-05 16:39 Ke_scholar 阅读(13) 评论(0) 推荐(0)
摘要:洛谷模板题AC通过 #include <bits/stdc++.h> using namespace std; using i64 = long long; int P; template<class Node> struct SegmentTree { #define lc u<<1 #defin 阅读全文
posted @ 2025-05-25 15:49 Ke_scholar 阅读(27) 评论(0) 推荐(0)
摘要:将佬[1]$的值域分块封装了一下,洛谷过了 template <typename T> struct Block { int B, N, tot; vector<T> cnt, Cnt, L, R, belong; Block(int n): N(n - 1), B(sqrt(n - 1)), cn 阅读全文
posted @ 2025-05-23 21:24 Ke_scholar 阅读(11) 评论(0) 推荐(0)
摘要:struct DSU { vector<int> fa, sz, st; DSU(int n) { fa.resize(n); sz.assign(n, 1); iota(fa.begin(), fa.end(), 0); } int find(int x) { return fa[x] == x 阅读全文
posted @ 2025-05-14 17:10 Ke_scholar 阅读(18) 评论(0) 推荐(0)
摘要:偷的 struct myhash { static uint64_t fxn(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049 阅读全文
posted @ 2024-10-07 03:23 Ke_scholar 阅读(23) 评论(0) 推荐(0)
摘要:自适应辛普森法 double a, b, c, d, l, r; const double eps = 1e-8; double F(double x) { //需要积分的公式 return (c * x + d) / (a * x + b); } double simpson(double l, 阅读全文
posted @ 2024-10-02 12:32 Ke_scholar 阅读(24) 评论(0) 推荐(0)
摘要:简单封装了一下浮点加法乘法,未验证 struct BigFloat { static const int N = 100, n = 16; //位数,保留位数 vector<int> integer;//整数部分(逆序存储)123. -->321. vector<int> decimal;//小数部 阅读全文
posted @ 2024-08-13 21:20 Ke_scholar 阅读(44) 评论(0) 推荐(0)
摘要:复杂度 \(O(n^\frac 23)\),计算 \(1\sim n\) 的素数个数 #define div(a, b) (1.0 * (a) / (b)) #define half(x) (((x) - 1) / 2) i64 Meissel_Lehmer(i64 n) { if (n <= 3) 阅读全文
posted @ 2024-08-09 20:01 Ke_scholar 阅读(41) 评论(0) 推荐(0)
摘要:using i128 = __int128; istream &operator>>(istream &is, i128 &x) { string s; is >> s; bool neg = false; x = 0; for (char c : s) { if (c == '-') neg = 阅读全文
posted @ 2024-08-09 18:51 Ke_scholar 阅读(63) 评论(0) 推荐(0)
摘要:template<class Node> struct PersidentSegmentTree { #define lc(u) tr[u].l #define rc(u) tr[u].r const int n; int tot = 0; vector<Node> tr; vector<int> 阅读全文
posted @ 2024-08-09 11:04 Ke_scholar 阅读(33) 评论(0) 推荐(0)
摘要:jiangly的(偷一下 i64 mul(i64 a, i64 b, i64 m) { return static_cast<__int128>(a) * b % m; } i64 power(i64 a, i64 b, i64 m) { i64 res = 1 % m; for (; b; b > 阅读全文
posted @ 2024-08-08 21:17 Ke_scholar 阅读(51) 评论(2) 推荐(1)
摘要:已通过洛谷【模板1】、【模板2区间第k小】、 【区间小于x的个数-牛客】、【区间小于x的个数-代码源 杭电4417怀疑数据可能有问题(?,调了一下午没调出来,如有大佬改出来了麻烦告知下错哪了qwq /* 可持久化线段树(主席树)模板 * @tparam Node: 节点结构体类型,需包含左右子节点和 阅读全文
posted @ 2024-08-07 19:42 Ke_scholar 阅读(31) 评论(2) 推荐(0)
摘要:template<class Info> struct SegmentTree { int n; std::vector<Info> info; SegmentTree() : n(0) {} SegmentTree(int n_, Info v_ = Info()) { init(n_, v_); 阅读全文
posted @ 2024-08-03 01:34 Ke_scholar 阅读(45) 评论(2) 推荐(0)
摘要:dsu on tree模板运用 例题以及代码: U41492 树上数颜色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 记录详情 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) Lomsat gelral - 洛谷 | 计算机科学教育新生态 (luogu.com 阅读全文
posted @ 2024-07-28 20:26 Ke_scholar 阅读(39) 评论(0) 推荐(0)
摘要:struct Tree { int n, d = 0; //顶点,直径,树中心 //自顶向下u到其叶子结点最远距离d1,次长距离d2(与最长路径无公共边) //p1,p2表示结点u向下更新时是由哪个结点更新来的 //up表示结点u向上到祖宗结点的最远距离 vector<int> d1, d2, p1 阅读全文
posted @ 2024-07-27 16:41 Ke_scholar 阅读(29) 评论(0) 推荐(0)
摘要:自然溢出哈希 struct Hash { using u64 = unsigned long long; u64 base = 13331; vector<u64> pow, hash; Hash(string &s) { s = " " + s; int N = s.size(); pow.res 阅读全文
posted @ 2024-07-25 20:01 Ke_scholar 阅读(445) 评论(6) 推荐(1)
摘要:#define L(x) (1 << (x)) const double PI = acos(-1.0); const int N = 1e7 + 10; double ax[N], ay[N], bx[N], by[N]; char sa[N / 2], sb[N / 2]; int sum[N] 阅读全文
posted @ 2024-07-24 19:57 Ke_scholar 阅读(61) 评论(0) 推荐(0)
摘要:jiangly的板子 // 取模机 // using i64 = long long; template<class T> constexpr T power(T a, i64 b) { T res {1}; for (; b; b /= 2, a *= a) { if (b % 2) { res 阅读全文
posted @ 2024-07-24 12:05 Ke_scholar 阅读(106) 评论(1) 推荐(0)
摘要:struct SCC { int top = 0, cntscc = 0, dfncnt = 0, n; vector<int> dfn, low, stk, instk; vector<int> sccnum, sccid; vector<vector<int>> g, scc; SCC(int 阅读全文
posted @ 2024-07-18 20:39 Ke_scholar 阅读(29) 评论(1) 推荐(0)
摘要:取分块大小 \(n^\frac{2}{3}\) 最优。 例题:数颜色 #include<bits/stdc++.h> using namespace std; using i64 = long long; const int N = 1e6 + 5; int cnt[N], a[N]; struct 阅读全文
posted @ 2024-07-17 12:37 Ke_scholar 阅读(25) 评论(1) 推荐(0)
摘要:#include<bits/stdc++.h> using namespace std; using i64 = long long; const int N = 1e6 + 5; int cnt[N], a[N]; struct query { int l, r, id; }; int main( 阅读全文
posted @ 2024-07-17 10:24 Ke_scholar 阅读(32) 评论(1) 推荐(0)
摘要:#include<bits/stdc++.h> using namespace std; using i64 = long long; const int N = 1e6 + 5; //本模板是从左往右扫的,从下往上扫同理 #define ls (rt<<1) #define rs (rt<<1|1 阅读全文
posted @ 2024-05-18 22:20 Ke_scholar 阅读(31) 评论(1) 推荐(0)
摘要:struct BigInteger { static const int BASE = 100000000; static const int WIDTH = 8; vector<int> s; bool sign; BigInteger(long long num = 0) { *this = n 阅读全文
posted @ 2024-05-06 17:10 Ke_scholar 阅读(92) 评论(3) 推荐(1)
摘要:Exgcd 模板 pair<int, int> exgcd(int a, int b) { if (b == 0)return make_pair(1, 0); auto [x, y] = exgcd(b, a % b); pair<int, int> ans = make_pair(y, x - 阅读全文
posted @ 2024-05-03 14:26 Ke_scholar 阅读(23) 评论(0) 推荐(0)
摘要:判断负环模板 Bellman_ford \(\mathcal{O}(nm)\) struct Bellman_ford { using i64 = long long; using PII = pair<i64, i64>; int n; vector<i64> dis; vector<array< 阅读全文
posted @ 2024-04-28 00:51 Ke_scholar 阅读(17) 评论(0) 推荐(0)
摘要:struct DIJ { using i64 = long long; using PII = pair<i64, i64>; vector<i64> dis, path, node; vector<vector<array<int, 3>>> G; int n; DIJ() {} DIJ(int 阅读全文
posted @ 2024-03-31 12:58 Ke_scholar 阅读(24) 评论(0) 推荐(0)
摘要:struct Node { int data; Node* left; Node* right; Node* newNode(int v) { Node* node = new Node; node->data = v; node->right = node->left = NULL; return 阅读全文
posted @ 2024-03-30 21:41 Ke_scholar 阅读(23) 评论(0) 推荐(0)
摘要:KMP模板 struct Kmp { vector<int> next; void getNext(string p) { int j = 0, k = -1, n = p.size(); next.resize(n + 1); next[0] = -1; while (j < n) { if (k 阅读全文
posted @ 2024-03-11 23:57 Ke_scholar 阅读(21) 评论(0) 推荐(0)
摘要:单点修改区间查最值-树状数组模板 #include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; using i64 = long long; typedef pair<i64, i64> 阅读全文
posted @ 2024-03-11 15:24 Ke_scholar 阅读(22) 评论(0) 推荐(0)
摘要:struct Two_D_Discrete { int n, tot1 = 1, tot2 = 1; vector<vector<int>> mp; vector<int> x, y, nx, ny; vector<pair<i64, i64>> a; vector<PII> New; Two_D_ 阅读全文
posted @ 2024-03-07 21:44 Ke_scholar 阅读(35) 评论(0) 推荐(0)
摘要:单点修改,区间查询/区间修改,单点查询 template<typename T> struct BIT { int n; vector<T> w; BIT() {} BIT(int n) { this->n = n; w.resize(n + 1); } void update(int x, int 阅读全文
posted @ 2023-12-26 08:58 Ke_scholar 阅读(45) 评论(1) 推荐(0)
摘要:例题:P3366 【模板】最小生成树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) Kruskal #include <bits/stdc++.h> #define debug(a) cout<<#a<<"="<<a<<'\n'; using namespace std; usi 阅读全文
posted @ 2023-12-24 12:42 Ke_scholar 阅读(32) 评论(0) 推荐(0)
摘要:例题素数密度 template<typename T> struct segment_sieve { vector<bool> is_prime, is_prime_small; vector<T> prime; segment_sieve() { is_prime.resize(1000010); 阅读全文
posted @ 2023-12-16 16:27 Ke_scholar 阅读(28) 评论(0) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; struct toposort { vector<vector<int>> e; vector<int> tp , din; int n ; toposort() {} toposort(int n) { t 阅读全文
posted @ 2023-12-12 23:09 Ke_scholar 阅读(34) 评论(0) 推荐(0)
摘要:include <bits/stdc++.h> using namespace std; struct LCA { int n; vector<int> dep; vector<vector<int>> e; vector<array<int, 21>> fa; LCA() {} LCA(int n 阅读全文
posted @ 2023-12-12 02:08 Ke_scholar 阅读(30) 评论(0) 推荐(0)
摘要:struct DIJ { using i64 = long long; using PII = pair<i64, i64>; vector<i64> dis; vector<vector<PII>> G; DIJ() {} DIJ(int n) { dis.assign(n + 1, 1e18); 阅读全文
posted @ 2023-12-09 23:44 Ke_scholar 阅读(26) 评论(0) 推荐(0)
摘要:已通过洛谷模板:(【矩阵快速幂】、【行列式计算】) template<class T> struct Matrix { i64 N; vector<vector<T>> A; Matrix() : N(0) {} Matrix(int n) : N(n), A(n, vector<T>(n, 0)) 阅读全文
posted @ 2023-12-05 15:20 Ke_scholar 阅读(39) 评论(2) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; struct trie { int n; vector<array<int, 26>> trans; vector<int> cnt; trie() : n(0) { new_node(); } int ne 阅读全文
posted @ 2023-12-04 12:51 Ke_scholar 阅读(68) 评论(0) 推荐(0)
摘要:二分图最大匹配模板(匈牙利算法) P3386 【模板】二分图最大匹配 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) struct augment_path { vector<vector<int> > g; vector<int> pa; // 匹配 vector<int> pb 阅读全文
posted @ 2023-12-02 12:32 Ke_scholar 阅读(55) 评论(0) 推荐(0)
摘要:struct EulerSieve { private: int n; vector<int> phi; // 欧拉函数数组 vector<int> mu; // 莫比乌斯函数数组 vector<int> nextp; // 最小质因子数组 vector<int> prime; // 素数列表 ve 阅读全文
posted @ 2023-11-30 17:48 Ke_scholar 阅读(45) 评论(2) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; template <typename T> class SparseTable { using VT = vector<T>; using VVT = vector<VT>; using func_type 阅读全文
posted @ 2023-11-26 01:51 Ke_scholar 阅读(53) 评论(0) 推荐(0)
摘要://快速幂 //底数128 long long ksm(__int128 a, long long b, long long p) { __int128 res = 1; while (b) { if (b & 1)res = res * a % p; b >>= 1; a = a * a % p; 阅读全文
posted @ 2023-10-31 19:28 Ke_scholar 阅读(29) 评论(0) 推荐(0)
摘要:欧拉定理求质因数: //欧拉定理求质因数 long long phi(long long x) { long long i; long long res = x; for (i = 2; i * i <= x; i++) { if (x % i == 0) { res = res / i * (i 阅读全文
posted @ 2023-10-31 19:26 Ke_scholar 阅读(37) 评论(0) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; struct UFS { int sz; vector<int> rank, p; void link(int x, int y) { if (x == y) return; if (rank[x] > ra 阅读全文
posted @ 2023-10-31 19:26 Ke_scholar 阅读(46) 评论(0) 推荐(0)