あいさか たいがblogAisaka_Taiga的博客
//https://img2018.cnblogs.com/blog/1646268/201908/1646268-20190806114008215-138720377.jpg

模板题合集

Toretto·2023-10-19 14:38·46 次阅读

模板题合集

B3614 【模板】栈#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 14:11:38 * @LastEditTime: 2023-10-19 14:33:54 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\B3614.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int unsigned long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, stk[N], top; inline void print1(int x){cout << x << endl; return ;} inline void work() { top = 0; n = read(); string s; for(int i = 1; i <= n; i ++) { cin >> s; if(s == "push") stk[++ top] = read(); if(s == "pop") {if(top == 0) puts("Empty"); else top --;} if(s == "query") {if(top == 0) puts("Anguei!"); else print1(stk[top]);} if(s == "size") cout << top << endl; } return ; } signed main() { int T = read(); while(T --) work(); return 0; }

P3375 【模板】KMP#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 14:58:36 * @LastEditTime: 2023-10-19 15:01:25 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3375.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } string s1, s2; int n, m, nxt[N]; signed main() { cin >> s1 >> s2; n = s1.size(), m = s2.size(); s1 = ' ' + s1, s2 = ' ' + s2; int j = 0; for(int i = 2; i <= m; i ++) { while(j && s2[i] != s2[j + 1]) j = nxt[j]; if(s2[i] == s2[j + 1]) j ++; nxt[i] = j; } j = 0; for(int i = 1; i <= n; i ++) { while(j && s1[i] != s2[j + 1]) j = nxt[j]; if(s1[i] == s2[j + 1]) j ++; if(j == m) cout << i - m + 1 << endl, j = nxt[j]; } for(int i = 1; i <= m; i ++) cout << nxt[i] << " "; return 0; }

B3647 【模板】Floyd#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 15:28:48 * @LastEditTime: 2023-10-19 16:22:52 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\B3647.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1010 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, m, mp[N][N]; signed main() { n = read(), m = read(); memset(mp, 0x3f, sizeof mp); for(int i = 1; i <= n; i ++) mp[i][i] = 0; for(int i = 1; i <= m; i ++) { int u = read(), v = read(), w = read(); mp[u][v] = mp[v][u] = w; } for(int k = 1; k <= n; k ++) for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) if(mp[i][j] > mp[i][k] + mp[k][j]) mp[i][j] = mp[i][k] + mp[k][j]; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) cout << mp[i][j] << " "; cout << endl; } return 0; }

B3616 【模板】队列#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 18:51:16 * @LastEditTime: 2023-10-19 18:52:14 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\3616.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, q[N], l, r; signed main() { n = read(); l = 0, r = -1; for(int i = 1; i <= n; i ++) { int op = read(); if(op == 1) q[++ r] = read(); if(op == 2){if(l > r) puts("ERR_CANNOT_POP"); else l ++;} if(op == 3){if(l > r) puts("ERR_CANNOT_QUERY"); else cout << q[l] << endl;} if(op == 4) cout << r - l + 1 << endl; } return 0; }

P1226 【模板】快速幂#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 19:02:01 * @LastEditTime: 2023-10-19 19:02:22 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P1226.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return 0; } int m, n, p; inline int ksm(int x, int y) { int res = 1; while(y) { if(y & 1) res = (res * x) % p; x = (x * x) % p, y >>= 1; } return res; } signed main() { cin >> n >> m >> p; int ans = ksm(n, m); cout << n << "^" << m << " mod " << p << "=" << ans << endl; return 0; }

P3367 【模板】并查集#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 19:23:45 * @LastEditTime: 2023-10-19 19:33:22 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3367.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1001000 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, m, f[N]; inline int fid(int x){return f[x] == x ? x : f[x] = fid(f[x]);} inline void merge(int u, int v) { int xx = fid(u), yy = fid(v); if(xx != yy) f[yy] = xx; return ; } inline int ask(int u, int v){return fid(u) == fid(v) ? 1 : 0;} signed main() { n = read(), m = read(); for(int i = 1; i <= n; i ++) f[i] = i; for(int i = 1; i <= m; i ++) { int op = read(), x = read(), y = read(); if(op == 1) merge(x, y); if(op == 2) ask(x, y) ? puts("Y") : puts("N"); } return 0; }

B3656 【模板】双端队列 1#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 20:21:01 * @LastEditTime: 2023-10-19 20:32:41 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\B3656.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n; string s; list<int> q[N]; signed main() { n = read(); for(int i = 1; i <= n; i ++) { cin >> s; int x = read(); if(s == "push_back") q[x].push_back(read()); if(s == "pop_back") if(!q[x].empty()) q[x].pop_back(); if(s == "push_front") q[x].push_front(read()); if(s == "pop_front") if(!q[x].empty()) q[x].pop_front(); if(s == "size") cout << q[x].size() << endl; if(s == "front") if(!q[x].empty()) cout << q[x].front() << endl; if(s == "back") if(!q[x].empty()) cout << q[x].back() << endl; } return 0; }

P3383 【模板】线性筛素数#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-19 21:47:46 * @LastEditTime: 2023-10-19 22:24:08 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3383.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> // #define int long long #define N 100010000 #define endl '\n' using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, m, ans[N], vis[N], cnt; signed main() { n = read(), m = read(); for(int i = 2; i <= n; i ++) { if(!vis[i]) ans[++ cnt] = i; for(int j = 1; j <= cnt && ans[j] * i <= n; j ++) { vis[ans[j] * i] = 1; if(i % ans[j] == 0) break; } } for(int i = 1; i <= m; i ++) cout << ans[read()] << endl; return 0; }

P3366 【模板】最小生成树#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 08:17:31 * @LastEditTime: 2023-10-20 08:19:22 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3366.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, m, f[N], num, ans; struct node{int u, v, w;}e[N]; inline int cmp(node a, node b){return a.w < b.w;} inline int fid(int x){return f[x] == x ? x : fid(f[x]);} signed main() { n = read(), m = read(); for(int i = 1; i <= n; i ++) f[i] = i; for(int i = 1; i <= m; i ++) e[i] = (node){read(), read(), read()}; sort(e + 1, e + m + 1, cmp); for(int i = 1; i <= m; i ++) { int xx = fid(e[i].u), yy = fid(e[i].v); if(xx == yy) continue; f[xx] = yy; ans += e[i].w; num ++; if(num == n - 1) break; } if(num == n - 1) cout << ans << endl; else cout << "orz" << endl; return 0; }

P3370 【模板】字符串哈希#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 08:39:37 * @LastEditTime: 2023-10-20 08:43:24 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3370.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define P 998244353 #define N 10010 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } const int base = 131; int n, a[N], ans = 1; char s[N]; inline int Hash() { int len = strlen(s + 1), res = 0; for(int i = 1; i <= len; i ++) res = (res * base + (int)(s[i] - ' ')) % P; return res; } signed main() { n = read(); for(int i = 1; i <= n; i ++) { scanf("%s", s + 1); a[i] = Hash(); } sort(a + 1, a + n + 1); for(int i = 1; i < n; i ++) if(a[i] != a[i + 1]) ans ++; cout << ans << endl; return 0; }

P1886 滑动窗口 /【模板】单调队列#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 09:49:19 * @LastEditTime: 2023-10-20 09:53:06 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P1886.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, k, q1[N], q2[N], a[N]; inline void max_() { int l = 0, r = -1; for(int i = 1; i <= n; i ++) { while(l <= r && q1[l] + k <= i) l ++; while(l <= r && a[i] > a[q1[r]]) r --; q1[++ r] = i; if(i >= k) cout << a[q1[l]] << " "; } cout << endl; return ; } inline void min_() { int l = 0, r = -1; for(int i = 1; i <= n; i ++) { while(l <= r && q2[l] + k <= i) l ++; while(l <= r && a[i] < a[q2[r]]) r --; q2[++ r] = i; if(i >= k) cout << a[q2[l]] << " "; } cout << endl; return ; } signed main() { n = read(), k = read(); for(int i = 1; i <= n; i ++) a[i] = read(); min_(); max_(); return 0; }

B3644 【模板】拓扑排序 / 家谱树#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 10:06:49 * @LastEditTime: 2023-10-20 10:08:43 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\B3644.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } int n, head[N], du[N], stk[N], top, cnt; struct node{int u, v, next;}e[N << 1]; inline void add(int u, int v){e[++ cnt] = (node){u, v, head[u]}; head[u] = cnt;} inline void topo() { queue<int> q; for(int i = 1; i <= n; i ++) if(!du[i]) q.push(i); while(!q.empty()) { int u = q.front(); q.pop(); stk[++ top] = u; for(int i = head[u]; i; i = e[i].next) { int v = e[i].v; du[v] --; if(du[v] == 0) q.push(v); } } return ; } signed main() { n = read(); for(int i = 1; i <= n; i ++) { while(1) { int v = read(); if(v == 0) break; add(i, v); du[v] ++; } } topo(); for(int i = 1; i <= n; i ++) cout << stk[i] << " "; return 0; }

P3382 【模板】三分#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 10:45:38 * @LastEditTime: 2023-10-20 10:56:15 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3382.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include <bits/stdc++.h> #define int long long #define DB double #define N 1000100 using namespace std; const DB eps = 1e-8; int n; DB L, R, a[N]; inline DB F(DB x) { DB res = 0; for(int i = n; i >= 0; i --) res = res * x + a[i]; return res; } signed main() { cin >> n >> L >> R; for(int i = n; i >= 0; i --) cin >> a[i]; while(fabs(R - L) >= eps) { DB mid = (L + R) / 2; if(F(mid + eps) > F(mid - eps)) L = mid; else R = mid; } printf("%.5lf\n", L); return 0; }

P3368 【模板】树状数组 2#

Copy
/* * @Author: Aisaka_Taiga * @Date: 2023-10-20 21:16:30 * @LastEditTime: 2023-10-20 21:19:47 * @LastEditors: Aisaka_Taiga * @FilePath: \Desktop\P3368.cpp * The heart is higher than the sky, and life is thinner than paper. */ #include<bits/stdc++.h> #define int long long #define N 1000100 using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();} while(c <= '9' && c >= '0') x = (x << 1) + (x << 3) +(c ^ 48), c = getchar(); return x * f; } int n, m, a[N], t[N]; inline int lb(int x){return x & -x;} inline void add(int x, int v){while(x <= n) t[x] += v, x += lb(x);} inline int ask(int x){int ans = 0; while(x >= 1) ans += t[x], x -= lb(x); return ans;} signed main() { n = read(), m = read(); for(int i = 1; i <= n; i ++) a[i] = read(); for(int i = 1; i <= m; i ++) { int op = read(); if(op == 1) { int x = read(), y = read(), k = read(); add(x, k); add(y + 1, -k); } if(op == 2) { int x = read(); cout << ask(x) + a[x] << endl; } } return 0; }
posted @   北烛青澜  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示
目录