版本比较
模板
正文
增加 87 行删除 87 行
26 行未改动27282930313233343577 行未改动113114115116117118119120121127 行未改动全部展开24925025125225325425525625752 行未改动310311312313314315316317318161 行未改动全部展开48048148248348448548648748887 行未改动5765775785795805815825835849 行未改动59459559659759859960060160259 行未改动66266366466566666766866967067167214 行未改动68768868969069169269369469569669769869970030 行未改动7317327337347357367377387398 行未改动74874975075175275375475575675723 行未改动78178278378478578678778878979079179279379479542 行未改动83883984084184284384484584611 行未改动858859860861862863864865866108 行未改动全部展开975976977978979980981982983284 行未改动全部展开12681269127012711272127312741275127644 行未改动1321132213231324132513261327132813299 行未改动13391340134113421343134413451346134731 行未改动1379138013811382138313841385138613876 行未改动13941395139613971398139914001401140221 行未改动1424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614475 行未改动145314541455145614571458145914601461146214631464146514661467146814691470147131 行未改动1503150415051506150715081509151015119 行未改动1521152215231524152515261527152815291530153115321533153434 行未改动15691570157115721573157415751576157765 行未改动16431644164516461647164816491650165135 行未改动16871688168916901691169216931694169557 行未改动17531754175517561757175817591760176145 行未改动1807180818091810181118121813181418156 行未改动18221823182418251826182718281829183048 行未改动18791880188118821883188418851886188731 行未改动19191920192119221923192419251926192739 行未改动19671968196919701971197219731974197534 行未改动201020112012201320142015201620172018272 行未改动全部展开22912292229322942295229622972298229989 行未改动23892390239123922393239423952396239745 行未改动244324442445244624472448244924502451164 行未改动全部展开26162617261826192620262126222623262423 行未改动26482649265026512652265326542655265610 行未改动26672668266926702671267226732674267526 行未改动27022703270427052706270727082709271015 行未改动272627272728272927302731273227332734273527362737273827392740274127422743274427452746274712 行未改动27602761276227632764276527662767276839 行未改动28082809281028112812281328142815281687 行未改动29042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462 行未改动2949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297796 行未改动30743075307630773078307930803081308235 行未改动3118311931203121312231233124312531267 行未改动31343135313631373138313931403141314234 行未改动31773178317931803181318231833184318532 行未改动32183219322032213222322332243225322673 行未改动33003301330233033304330533063307330810 行未改动33193320332133223323332433253326332737 行未改动33653366336733683369337033713372337313 行未改动33873388338933903391339233933394339522 行未改动26 行未改动template<typename T> void read(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}```77 行未改动rd(a[i]);s.push(0);for(int i = n;i >= 1;i --){while(a[i] >= a[s.top()] && s.size() > 1)s.pop();p[i] = s.top();s.push(i);}127 行未改动全部展开return res == -0x3f3f3f3f3f ? -1 : res;}for(int i = l;pos[i] == pos[l];i ++) res = max(res, a[i] + tag[pos[i]] < c ? a[i] + tag[pos[i]] : -0x3f3f3f3f3f);for(int i = r;pos[i] == pos[r];i --) res = max(res, a[i] + tag[pos[i]] < c ? a[i] + tag[pos[i]] : -0x3f3f3f3f3f);for(int i = pos[l] + 1; i <= pos[r] - 1;i ++){auto p = lower_bound(sor + L(i), sor + R(i) + 1, c - tag[i]) - 1;res = max(res, p != sor + L(i) - 1 ? *p + tag[i]: -0x3f3f3f3f3f);}52 行未改动{long long l,r;rd(l),rd(r);printf("%lld\n",st_query(l,r));}return 0;}```### 快速幂([P1226](https://www.luogu.com.cn/problem/P1226))161 行未改动全部展开dp[j]=max(dp[j],dp[j-w[i]]+v[i]);}}cout<<dp[s];}```### 多重背包```cpp#include<bits/stdc++.h>87 行未改动}```### 二维费用背包```cpp#include<bits/stdc++.h>using namespace std;const int N = 1050;const int M = 1000;int v,w,dp[M][M],s;9 行未改动for(int j = vk;j >= v;j --)for(int k = wk;k >= w;k --)dp[j][k]=max(dp[j][k],dp[j - v][k - w] + s);}cout<<dp[vk][wk];return 0;}```59 行未改动{dp[j] = max(dp[j],dp[j - a[i].w] + a[i].v);o.push_back(make_pair(a[i].group,make_pair(a[i].v,a[i].w)));}}}cout << dp[m];return 0;}```14 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N];//表示从s出发到x节点的最短路长度int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路queue<int> q;void dij(){int x;for(int i=0;i<=n;i++){d[i]=INT_MAX;}30 行未改动for(int i=1;i<=n;i++){cout<<d[i]<<" ";}return 0;}```### dijkstra非堆优化```cpp8 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N];//表示从s出发到x节点的最短路长度int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路void dij(int x){for(int i=0;i<=n;i++)d[i]=INT_MAX;23 行未改动}dij(s);for(int i=1;i<=n;i++)cout<<d[i]<<" ";return 0;}```### dijkstra 堆优化```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e5+66;typedef long long LL;int n,s,m;int head[N],to[N],nxt[N],w[N],tot;42 行未改动d[i] = INT_MAX;dij();for(int j = 1;j <= n;j ++)cout << d[j] << " ";return 0;}```## 多源最短路11 行未改动for(int i=1;i<=m;i++){int x,y,w;cin>>x>>y>>w;dp[x][y]=min(dp[x][y],w);//可能存在重边dp[y][x]=min(dp[y][x],w);}for(int i=1;i<=n;i++){108 行未改动全部展开else ans += j * (d[j] + h[j] - h[i]);}cout << ans << endl;}return 0;}```284 行未改动全部展开for(int i = 1;i <= n;i ++)if(!vis[i])tarjan(i,i);for(int i = 1;i <= n;i ++)if(color[i])s.insert({min(father[i],i),max(father[i],i)});for(auto v:s){cout << v.first << " " << v.second << endl;44 行未改动else{if(v != fa || f) low[u] = min(low[u],dfn[v]);else f = 1;}}}int main(){9 行未改动for(int i = 1;i <= n;i ++)if(!vis[i])tarjan(i,i);for(int i = 1;i <= n;i ++)if(color[i])s.insert({min(father[i],i),max(father[i],i)});for(auto v:s){cout << v.first << " " << v.second << endl;31 行未改动ans[con].push_back(u);//放入u}}else low[u] = min(low[u], dfn[v]);}if(fa == u && son == 1) iscp[u] = 0;//多判断一下if(fa == u && son == 0) ans[++ con].push_back(u);}6 行未改动int u, v;cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for(int i = 1;i <= n;i ++) if(!dfn[i]) top = 0, tarjan(i,i);cout << con << '\n';for(int i = 1;i <= con;i ++){cout << ans[i].size();for(auto p : ans[i]) cout << ' ' << p;21 行未改动int p[N];int find(int x){return p[x]==x?x:p[x]=find(p[x]);}//查找祖先void kruskal(){sort(e+1,e+1+m);//排序for(int i=1;i<=n;i++){p[i]=i;//每个点一个子树,根都是自己sz[i]=1;}//并查集for(int i=1;i<=m;i++){int x=find(e[i].u);int y=find(e[i].v);if(x==y)continue;p[x]=y;//合并su+=e[i].w;sz[y]+=sz[x];}}5 行未改动kruskal();if(sz[find(1)]!=n)cout<<"orz";elsecout<<su;return 0;}```### prim(未优化)```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e3 + 66,INF = 0x3f3f3f3f;vector<pair<int,int> > G[N];int d[N],n,m,ans;bool vis[N];void prim()31 行未改动{cout << "orz" << endl;return 0;}cout << ans << endl;return 0;}```### prim(优化)9 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N],su;int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路priority_queue<pair<long long,int> >q;void prim(){int x;for(int i=0;i<=n;i++)d[i]=INT_MAX;d[1]=0;q.push(make_pair(-d[1],1));34 行未改动cout<<"orz";return 0;}cout<<su;return 0;}```## 负环```cpp65 行未改动const int N = 1e3 + 66;int n,t,dp[N][N];int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n;for(int i = 1;i <= n;i ++)for(int j = 1;j <= n;j ++)cin >> dp[i][j];35 行未改动q.push(to);vis[to] = 1;}}}return 0;}int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);57 行未改动{if(in[i] != out[i]){f = 1;if(out[i] - in[i] == 1)cntst ++,st = i;else if(in[i] - out[i] == 1)cnten ++;else45 行未改动ans[con].push_back(u);//放入u}}else low[u] = min(low[u], dfn[v]);}if(fa == u && son == 1) iscp[u] = 0;//多判断一下if(fa == u && son == 0) ans[++ con].push_back(u);}6 行未改动int u, v;cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for(int i = 1;i <= n;i ++) if(!dfn[i]) top = 0, tarjan(i,i);cout << con << '\n';for(int i = 1;i <= con;i ++){cout << ans[i].size();for(auto p : ans[i]) cout << ' ' << p;48 行未改动}for(int i = 1;i <= n;i ++)cout << ne[i] << " ";return 0;}```## 哈希**(题太多了,此处给的是哈希表)**```cpp31 行未改动int n;cin >> n;int ans = 0;for(int i = 1;i <= n;i ++){cin >> s + 1;if(i == 1 || !query(gethash(s))){ans ++;39 行未改动{int a;cin >> a;if(a == 0) {root = i;continue;}G[a].push_back(i), G[i].push_back(a);}int ans = INT_MAX;bool f = 0;// ans = dfs(root, 0);for(int i = 1;i <= n;i ++) ans = min(dfs(i, 0), ans);34 行未改动{int a;cin >> a;if(a == 0) {root = i;continue;}G[a].push_back(i), G[i].push_back(a);}int ans = INT_MAX;bool f = 0;// ans = dfs(root, 0);for(int i = 1;i <= n;i ++) ans = min(dfs(i, 0), ans);272 行未改动全部展开ll ans = 0;for(int i = 1;i <= n;i ++) ans = max(vis[i], ans);cout << ans << '\n';for(int i = 1;i <= n;i ++) if(vis[i] == ans){cout << s[i] << '\n';}}return 0;}```89 行未改动for(auto p : e[root])if(p != fa){m[root] = max(m[root],sz[p]);dfss(p,root);}}void dfsss(int root,int fa,int deep){45 行未改动}}inline int lca(int u,int v){if(deep[u] < deep[v]) swap(u,v);for(int i = 20;i >= 0;i --)if(deep[u] - deep[v] >= (1 << i))u = fa[u][i];if(u == v) return u;164 行未改动全部展开cin >> od;if(od == 1){cin >> x;insert(x);}else if(od == 2) cout << a[1] << '\n';else pop();}return 0;}```23 行未改动{int x,y,z;cin >> z >> x >> y;if(z == 1) merge(x,y);else{if(find(x) == find(y)) cout << "Y\n" ;else cout << "N\n";}10 行未改动template<typename T> void rd(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}const int N = 5e5 + 66;26 行未改动rd(od),rd(x),rd(y);if(od == 1) add(x,y);else cout << (query(y) - query(x - 1)) << endl;}return 0;}```### 广义扫描线```cpp15 行未改动int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m;for(int i = 1;i <= n;i ++) cin >> a[i];//以值做位置for(int i = 1;i <= m;i ++){int l, r, k;cin >> l >> r >> k;qst[l - 1].push_back({-1, i, k});qst[r].push_back({1, i, k});//记录长度}for(int i = 1;i <= n;i ++){bit.add(a[i], 1);//加入这个位置for(auto p : qst[i]) ans[p.idx] += p.zf * bit.query(p.k);//判断是否是减去的}for(int i = 1;i <= m;i ++) cout << ans[i] << '\n';return 0;}```## 线段树### 模板112 行未改动{int mid = l + r >> 1;ls = new SegmentTree(l, mid, a), rs = new SegmentTree(mid + 1, r, a);pushup();}else ls = rs = nullptr, val = a[l];}inline void pushup(){val = ls -> val + rs -> val;}inline bool inRange(int x, int y){return x <= l && r <= y;}39 行未改动for(int i = 1;i <= m;i ++){ll op, x, y;cin >> op >> x >> y;if(op == 1){ll val;cin >> val;root -> update(x, y, val);}87 行未改动using namespace std;constexpr int N = 1e5 + 66;int n, bur[N * 2], tot;struct line{ll xl, xr, y, k;//左 右 第几行 上还是下line() = default;line(ll xl, ll xr, ll y, ll k): xl(xl), xr(xr), y(y), k(k){}friend bool operator < (line a, line b) {return a.y < b.y;}}li[N * 2];//记录线段struct Segmenttree{ll len[N << 4], tag[N << 4], ans[N << 4];#define ls(u) (u * 2)#define rs(u) (u * 2 + 1)void update(int u){if(tag[u] > 0) ans[u] = len[u];//如果有else ans[u] = ans[ls(u)] + ans[rs(u)];//否则}//改变void build(ll u, ll l, ll r){len[u] = bur[r] - bur[l - 1];if(l == r) return ;int mid = l + r >> 1;build(ls(u), l, mid);build(rs(u), mid + 1, r);}//建树void add(ll u, ll l, ll r, ll ql, ll qr, ll cag){if(ql <= l && r <= qr){tag[u] += cag;//标记永久化update(u);return ;}ll mid = l + r >> 1;if(ql <= mid)add(ls(u), l, mid, ql, qr, cag);if(mid + 1 <= qr) add(rs(u), mid + 1, r, ql, qr, cag);update(u);}//区间加}Seg;int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);2 行未改动{int xl, xr, yl, yr;cin >> xl >> yl >> xr >> yr;li[i] = line(xl, xr, yl, 1);li[i + n] = line(xl, xr ,yr, -1);//记录线段bur[++ tot] = xl, bur[++ tot] = xr;//离散化}sort(bur + 1, bur + 1 + tot);tot = unique(bur + 1, bur + 1 + tot) - bur - 1;for(int i = 1;i <= n * 2;i ++){li[i].xl = lower_bound(bur + 1, bur + 1 + tot, li[i].xl) - bur + 1;li[i].xr = lower_bound(bur + 1, bur + 1 + tot, li[i].xr) - bur + 1;}//离散化完毕sort(li + 1, li + 1 + 2 * n);Seg.build(1, 1, tot);ll ans = 0;for(int i = 1;i <= 2 * n;i ++){ans += (li[i].y - li[i - 1].y) * Seg.ans[1];//与上一个线段所组成的矩形面积 (高) * 长Seg.add(1, 1, tot, li[i].xl, li[i].xr - 1, li[i].k);}cout << ans << '\n';return 0;}```### 动态开点线段树```cpp96 行未改动{L ^= (l[i] + 1) * i;R ^= (r[i] + 1) * i;}cout << L << " " << R;return 0;}```# 数学35 行未改动if(s == 0)cout << "No" << endl;elsecout << "Yes" << endl;}}```## 数论7 行未改动template<typename T> void read(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}void exgcd(int a,int b,int &x,int &y)34 行未改动{ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n >> p;inv[1] = 1;for(int i = 2;i <= n;i ++) inv[i] = (p - p / i) * inv[p % i] % p;for(int i = 1;i <= n;i ++) cout << inv[i] << '\n';}```### EXgcd(有理数取余)```cpp32 行未改动do{x = x * 10 + c - '0';c = getchar();}while(isdigit(c));x *= f;}const long long N = 1e8 + 6;long long p[N],tot;bool pr[N];void Ol(long long m){for(int i = 1;i <= N;i ++) pr[i] = 1;pr[1] = pr[0] = 0;73 行未改动Matrix operator * (Matrix B){Matrix C;C.n = n,C.m = B.m;for(int i = 1;i <= n;i ++)for(int j = 1;j <= C.m;j ++)for(int k = 1;k <= m;k ++)C.a[i][j] += a[i][k] * B.a[k][j];return C;10 行未改动{for(int i = 1;i <= n;i ++)for(int j = 1;j <= m;j ++)cout << a[i][j] << (j == m ? '\n' : ' ');}};int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);37 行未改动Matrix operator * (Matrix B){Matrix C;C.n = n,C.m = B.m;for(int i = 1;i <= n;i ++)for(int j = 1;j <= C.m;j ++){for(int k = 1;k <= m;k ++)C.a[i][j] += (a[i][k] * B.a[k][j]) % mod;13 行未改动{for(int i = 1;i <= n;i ++)for(int j = 1;j <= m;j ++)cout << a[i][j] << (j == m ? '\n' : ' ');}};inline Matrix qpow(Matrix A, long long k){Matrix P;P.n = P.m = A.n;22 行未改动26 行未改动27282930313233343577 行未改动113114115116117118119120121127 行未改动全部展开24925025125225325425525625752 行未改动310311312313314315316317318161 行未改动全部展开48048148248348448548648748887 行未改动5765775785795805815825835849 行未改动59459559659759859960060160259 行未改动66266366466566666766866967067167214 行未改动68768868969069169269369469569669769869970030 行未改动7317327337347357367377387398 行未改动74874975075175275375475575675723 行未改动78178278378478578678778878979079179279379479542 行未改动83883984084184284384484584611 行未改动858859860861862863864865866108 行未改动全部展开975976977978979980981982983284 行未改动全部展开12681269127012711272127312741275127644 行未改动1321132213231324132513261327132813299 行未改动13391340134113421343134413451346134731 行未改动1379138013811382138313841385138613876 行未改动13941395139613971398139914001401140221 行未改动1424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614475 行未改动145314541455145614571458145914601461146214631464146514661467146814691470147131 行未改动1503150415051506150715081509151015119 行未改动1521152215231524152515261527152815291530153115321533153434 行未改动15691570157115721573157415751576157765 行未改动16431644164516461647164816491650165135 行未改动16871688168916901691169216931694169557 行未改动17531754175517561757175817591760176145 行未改动1807180818091810181118121813181418156 行未改动18221823182418251826182718281829183048 行未改动18791880188118821883188418851886188731 行未改动19191920192119221923192419251926192739 行未改动19671968196919701971197219731974197534 行未改动201020112012201320142015201620172018272 行未改动全部展开22912292229322942295229622972298229989 行未改动23892390239123922393239423952396239745 行未改动244324442445244624472448244924502451164 行未改动全部展开26162617261826192620262126222623262423 行未改动26482649265026512652265326542655265610 行未改动26672668266926702671267226732674267526 行未改动27022703270427052706270727082709271015 行未改动272627272728272927302731273227332734273527362737273827392740274127422743274427452746274712 行未改动27602761276227632764276527662767276839 行未改动28082809281028112812281328142815281687 行未改动29042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462 行未改动2949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297796 行未改动30743075307630773078307930803081308235 行未改动3118311931203121312231233124312531267 行未改动31343135313631373138313931403141314234 行未改动31773178317931803181318231833184318532 行未改动32183219322032213222322332243225322673 行未改动33003301330233033304330533063307330810 行未改动33193320332133223323332433253326332737 行未改动33653366336733683369337033713372337313 行未改动33873388338933903391339233933394339522 行未改动26 行未改动template<typename T> void read(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}```77 行未改动rd(a[i]);s.push(0);for(int i = n;i >= 1;i --){while(a[i] >= a[s.top()] && s.size() > 1)s.pop();p[i] = s.top();s.push(i);}127 行未改动全部展开return res == -0x3f3f3f3f3f ? -1 : res;}for(int i = l;pos[i] == pos[l];i ++) res = max(res, a[i] + tag[pos[i]] < c ? a[i] + tag[pos[i]] : -0x3f3f3f3f3f);for(int i = r;pos[i] == pos[r];i --) res = max(res, a[i] + tag[pos[i]] < c ? a[i] + tag[pos[i]] : -0x3f3f3f3f3f);for(int i = pos[l] + 1; i <= pos[r] - 1;i ++){auto p = lower_bound(sor + L(i), sor + R(i) + 1, c - tag[i]) - 1;res = max(res, p != sor + L(i) - 1 ? *p + tag[i]: -0x3f3f3f3f3f);}52 行未改动{long long l,r;rd(l),rd(r);printf("%lld\n",st_query(l,r));}return 0;}```### 快速幂([P1226](https://www.luogu.com.cn/problem/P1226))161 行未改动全部展开dp[j]=max(dp[j],dp[j-w[i]]+v[i]);}}cout<<dp[s];}```### 多重背包```cpp#include<bits/stdc++.h>87 行未改动}```### 二维费用背包```cpp#include<bits/stdc++.h>using namespace std;const int N = 1050;const int M = 1000;int v,w,dp[M][M],s;9 行未改动for(int j = vk;j >= v;j --)for(int k = wk;k >= w;k --)dp[j][k]=max(dp[j][k],dp[j - v][k - w] + s);}cout<<dp[vk][wk];return 0;}```59 行未改动{dp[j] = max(dp[j],dp[j - a[i].w] + a[i].v);o.push_back(make_pair(a[i].group,make_pair(a[i].v,a[i].w)));}}}cout << dp[m];return 0;}```14 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N];//表示从s出发到x节点的最短路长度int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路queue<int> q;void dij(){int x;for(int i=0;i<=n;i++){d[i]=INT_MAX;}30 行未改动for(int i=1;i<=n;i++){cout<<d[i]<<" ";}return 0;}```### dijkstra非堆优化```cpp8 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N];//表示从s出发到x节点的最短路长度int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路void dij(int x){for(int i=0;i<=n;i++)d[i]=INT_MAX;23 行未改动}dij(s);for(int i=1;i<=n;i++)cout<<d[i]<<" ";return 0;}```### dijkstra 堆优化```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e5+66;typedef long long LL;int n,s,m;int head[N],to[N],nxt[N],w[N],tot;42 行未改动d[i] = INT_MAX;dij();for(int j = 1;j <= n;j ++)cout << d[j] << " ";return 0;}```## 多源最短路11 行未改动for(int i=1;i<=m;i++){int x,y,w;cin>>x>>y>>w;dp[x][y]=min(dp[x][y],w);//可能存在重边dp[y][x]=min(dp[y][x],w);}for(int i=1;i<=n;i++){108 行未改动全部展开else ans += j * (d[j] + h[j] - h[i]);}cout << ans << endl;}return 0;}```284 行未改动全部展开for(int i = 1;i <= n;i ++)if(!vis[i])tarjan(i,i);for(int i = 1;i <= n;i ++)if(color[i])s.insert({min(father[i],i),max(father[i],i)});for(auto v:s){cout << v.first << " " << v.second << endl;44 行未改动else{if(v != fa || f) low[u] = min(low[u],dfn[v]);else f = 1;}}}int main(){9 行未改动for(int i = 1;i <= n;i ++)if(!vis[i])tarjan(i,i);for(int i = 1;i <= n;i ++)if(color[i])s.insert({min(father[i],i),max(father[i],i)});for(auto v:s){cout << v.first << " " << v.second << endl;31 行未改动ans[con].push_back(u);//放入u}}else low[u] = min(low[u], dfn[v]);}if(fa == u && son == 1) iscp[u] = 0;//多判断一下if(fa == u && son == 0) ans[++ con].push_back(u);}6 行未改动int u, v;cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for(int i = 1;i <= n;i ++) if(!dfn[i]) top = 0, tarjan(i,i);cout << con << '\n';for(int i = 1;i <= con;i ++){cout << ans[i].size();for(auto p : ans[i]) cout << ' ' << p;21 行未改动int p[N];int find(int x){return p[x]==x?x:p[x]=find(p[x]);}//查找祖先void kruskal(){sort(e+1,e+1+m);//排序for(int i=1;i<=n;i++){p[i]=i;//每个点一个子树,根都是自己sz[i]=1;}//并查集for(int i=1;i<=m;i++){int x=find(e[i].u);int y=find(e[i].v);if(x==y)continue;p[x]=y;//合并su+=e[i].w;sz[y]+=sz[x];}}5 行未改动kruskal();if(sz[find(1)]!=n)cout<<"orz";elsecout<<su;return 0;}```### prim(未优化)```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e3 + 66,INF = 0x3f3f3f3f;vector<pair<int,int> > G[N];int d[N],n,m,ans;bool vis[N];void prim()31 行未改动{cout << "orz" << endl;return 0;}cout << ans << endl;return 0;}```### prim(优化)9 行未改动nxt[tot]=head[x];w[tot]=val;head[x]=tot++;}long long d[N],su;int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路priority_queue<pair<long long,int> >q;void prim(){int x;for(int i=0;i<=n;i++)d[i]=INT_MAX;d[1]=0;q.push(make_pair(-d[1],1));34 行未改动cout<<"orz";return 0;}cout<<su;return 0;}```## 负环```cpp65 行未改动const int N = 1e3 + 66;int n,t,dp[N][N];int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n;for(int i = 1;i <= n;i ++)for(int j = 1;j <= n;j ++)cin >> dp[i][j];35 行未改动q.push(to);vis[to] = 1;}}}return 0;}int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);57 行未改动{if(in[i] != out[i]){f = 1;if(out[i] - in[i] == 1)cntst ++,st = i;else if(in[i] - out[i] == 1)cnten ++;else45 行未改动ans[con].push_back(u);//放入u}}else low[u] = min(low[u], dfn[v]);}if(fa == u && son == 1) iscp[u] = 0;//多判断一下if(fa == u && son == 0) ans[++ con].push_back(u);}6 行未改动int u, v;cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for(int i = 1;i <= n;i ++) if(!dfn[i]) top = 0, tarjan(i,i);cout << con << '\n';for(int i = 1;i <= con;i ++){cout << ans[i].size();for(auto p : ans[i]) cout << ' ' << p;48 行未改动}for(int i = 1;i <= n;i ++)cout << ne[i] << " ";return 0;}```## 哈希**(题太多了,此处给的是哈希表)**```cpp31 行未改动int n;cin >> n;int ans = 0;for(int i = 1;i <= n;i ++){cin >> s + 1;if(i == 1 || !query(gethash(s))){ans ++;39 行未改动{int a;cin >> a;if(a == 0) {root = i;continue;}G[a].push_back(i), G[i].push_back(a);}int ans = INT_MAX;bool f = 0;// ans = dfs(root, 0);for(int i = 1;i <= n;i ++) ans = min(dfs(i, 0), ans);34 行未改动{int a;cin >> a;if(a == 0) {root = i;continue;}G[a].push_back(i), G[i].push_back(a);}int ans = INT_MAX;bool f = 0;// ans = dfs(root, 0);for(int i = 1;i <= n;i ++) ans = min(dfs(i, 0), ans);272 行未改动全部展开ll ans = 0;for(int i = 1;i <= n;i ++) ans = max(vis[i], ans);cout << ans << '\n';for(int i = 1;i <= n;i ++) if(vis[i] == ans){cout << s[i] << '\n';}}return 0;}```89 行未改动for(auto p : e[root])if(p != fa){m[root] = max(m[root],sz[p]);dfss(p,root);}}void dfsss(int root,int fa,int deep){45 行未改动}}inline int lca(int u,int v){if(deep[u] < deep[v]) swap(u,v);for(int i = 20;i >= 0;i --)if(deep[u] - deep[v] >= (1 << i))u = fa[u][i];if(u == v) return u;164 行未改动全部展开cin >> od;if(od == 1){cin >> x;insert(x);}else if(od == 2) cout << a[1] << '\n';else pop();}return 0;}```23 行未改动{int x,y,z;cin >> z >> x >> y;if(z == 1) merge(x,y);else{if(find(x) == find(y)) cout << "Y\n" ;else cout << "N\n";}10 行未改动template<typename T> void rd(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}const int N = 5e5 + 66;26 行未改动rd(od),rd(x),rd(y);if(od == 1) add(x,y);else cout << (query(y) - query(x - 1)) << endl;}return 0;}```### 广义扫描线```cpp15 行未改动int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m;for(int i = 1;i <= n;i ++) cin >> a[i];//以值做位置for(int i = 1;i <= m;i ++){int l, r, k;cin >> l >> r >> k;qst[l - 1].push_back({-1, i, k});qst[r].push_back({1, i, k});//记录长度}for(int i = 1;i <= n;i ++){bit.add(a[i], 1);//加入这个位置for(auto p : qst[i]) ans[p.idx] += p.zf * bit.query(p.k);//判断是否是减去的}for(int i = 1;i <= m;i ++) cout << ans[i] << '\n';return 0;}```## 线段树### 模板112 行未改动{int mid = l + r >> 1;ls = new SegmentTree(l, mid, a), rs = new SegmentTree(mid + 1, r, a);pushup();}else ls = rs = nullptr, val = a[l];}inline void pushup(){val = ls -> val + rs -> val;}inline bool inRange(int x, int y){return x <= l && r <= y;}39 行未改动for(int i = 1;i <= m;i ++){ll op, x, y;cin >> op >> x >> y;if(op == 1){ll val;cin >> val;root -> update(x, y, val);}87 行未改动using namespace std;constexpr int N = 1e5 + 66;int n, bur[N * 2], tot;struct line{ll xl, xr, y, k;//左 右 第几行 上还是下line() = default;line(ll xl, ll xr, ll y, ll k): xl(xl), xr(xr), y(y), k(k){}friend bool operator < (line a, line b) {return a.y < b.y;}}li[N * 2];//记录线段struct Segmenttree{ll len[N << 4], tag[N << 4], ans[N << 4];#define ls(u) (u * 2)#define rs(u) (u * 2 + 1)void update(int u){if(tag[u] > 0) ans[u] = len[u];//如果有else ans[u] = ans[ls(u)] + ans[rs(u)];//否则}//改变void build(ll u, ll l, ll r){len[u] = bur[r] - bur[l - 1];if(l == r) return ;int mid = l + r >> 1;build(ls(u), l, mid);build(rs(u), mid + 1, r);}//建树void add(ll u, ll l, ll r, ll ql, ll qr, ll cag){if(ql <= l && r <= qr){tag[u] += cag;//标记永久化update(u);return ;}ll mid = l + r >> 1;if(ql <= mid)add(ls(u), l, mid, ql, qr, cag);if(mid + 1 <= qr) add(rs(u), mid + 1, r, ql, qr, cag);update(u);}//区间加}Seg;int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);2 行未改动{int xl, xr, yl, yr;cin >> xl >> yl >> xr >> yr;li[i] = line(xl, xr, yl, 1);li[i + n] = line(xl, xr ,yr, -1);//记录线段bur[++ tot] = xl, bur[++ tot] = xr;//离散化}sort(bur + 1, bur + 1 + tot);tot = unique(bur + 1, bur + 1 + tot) - bur - 1;for(int i = 1;i <= n * 2;i ++){li[i].xl = lower_bound(bur + 1, bur + 1 + tot, li[i].xl) - bur + 1;li[i].xr = lower_bound(bur + 1, bur + 1 + tot, li[i].xr) - bur + 1;}//离散化完毕sort(li + 1, li + 1 + 2 * n);Seg.build(1, 1, tot);ll ans = 0;for(int i = 1;i <= 2 * n;i ++){ans += (li[i].y - li[i - 1].y) * Seg.ans[1];//与上一个线段所组成的矩形面积 (高) * 长Seg.add(1, 1, tot, li[i].xl, li[i].xr - 1, li[i].k);}cout << ans << '\n';return 0;}```### 动态开点线段树```cpp96 行未改动{L ^= (l[i] + 1) * i;R ^= (r[i] + 1) * i;}cout << L << " " << R;return 0;}```# 数学35 行未改动if(s == 0)cout << "No" << endl;elsecout << "Yes" << endl;}}```## 数论7 行未改动template<typename T> void read(T&x){char c;int sign = 1;x = 0;// 判断是否是负数,以及去掉前面多余的字符do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));x *= sign;}void exgcd(int a,int b,int &x,int &y)34 行未改动{ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n >> p;inv[1] = 1;for(int i = 2;i <= n;i ++) inv[i] = (p - p / i) * inv[p % i] % p;for(int i = 1;i <= n;i ++) cout << inv[i] << '\n';}```### EXgcd(有理数取余)```cpp32 行未改动do{x = x * 10 + c - '0';c = getchar();}while(isdigit(c));x *= f;}const long long N = 1e8 + 6;long long p[N],tot;bool pr[N];void Ol(long long m){for(int i = 1;i <= N;i ++) pr[i] = 1;pr[1] = pr[0] = 0;73 行未改动Matrix operator * (Matrix B){Matrix C;C.n = n,C.m = B.m;for(int i = 1;i <= n;i ++)for(int j = 1;j <= C.m;j ++)for(int k = 1;k <= m;k ++)C.a[i][j] += a[i][k] * B.a[k][j];return C;10 行未改动{for(int i = 1;i <= n;i ++)for(int j = 1;j <= m;j ++)cout << a[i][j] << (j == m ? '\n' : ' ');}};int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);37 行未改动Matrix operator * (Matrix B){Matrix C;C.n = n,C.m = B.m;for(int i = 1;i <= n;i ++)for(int j = 1;j <= C.m;j ++){for(int k = 1;k <= m;k ++)C.a[i][j] += (a[i][k] * B.a[k][j]) % mod;13 行未改动{for(int i = 1;i <= n;i ++)for(int j = 1;j <= m;j ++)cout << a[i][j] << (j == m ? '\n' : ' ');}};inline Matrix qpow(Matrix A, long long k){Matrix P;P.n = P.m = A.n;22 行未改动