版本比较
模板
正文
增加 87 行删除 87 行
26 行未改动2728293031313233343577 行未改动113114115116117117118119120121127 行未改动全部展开24925025125225325325425525625752 行未改动310311312313314314315316317318161 行未改动全部展开48048148248348448448548648748887 行未改动5765775785795805805815825835849 行未改动59459559659759859859960060160259 行未改动66266366466566666666766866866967067167214 行未改动68768868969069169269169269369469569669669769869970030 行未改动7317327337347357357367377387398 行未改动74874975075175275375275375475575675723 行未改动78178278378478578578678778878979079179179279379479542 行未改动83883984084184284284384484584611 行未改动858859860861862862863864865866108 行未改动全部展开975976977978979979980981982983284 行未改动全部展开126812691270127112721272127312741275127644 行未改动13211322132313241325132513261327132813299 行未改动133913401341134213431343134413451346134731 行未改动13791380138113821383138313841385138613876 行未改动139413951396139713981398139914001401140221 行未改动1424142514261427142814281429143014311431143214331434143414351436143614371438143914401441144114421443144314441445144614475 行未改动145314541455145614571458145714581459146014611462146314641465146614671467146814691470147131 行未改动15031504150515061507150715081509151015119 行未改动1521152215231524152515261525152615271528152915301530153115321533153434 行未改动156915701571157215731573157415751576157765 行未改动164316441645164616471647164816491650165135 行未改动168716881689169016911691169216931694169557 行未改动175317541755175617571757175817591760176145 行未改动18071808180918101811181118121813181418156 行未改动182218231824182518261826182718281829183048 行未改动187918801881188218831883188418851886188731 行未改动191919201921192219231923192419251926192739 行未改动196719681969197019711971197219731974197534 行未改动2010201120122013201420142015201620172018272 行未改动全部展开229122922293229422952295229622972298229989 行未改动238923902391239223932393239423952396239745 行未改动2443244424452446244724472448244924502451164 行未改动全部展开261626172618261926202620262126222623262423 行未改动264826492650265126522652265326542655265610 行未改动266726682669267026712671267226732674267526 行未改动270227032704270527062706270727082709271015 行未改动272627272728272927302731273027312732273327342734273527362737273827392738273927402741274227432743274427452746274712 行未改动276027612762276327642764276527662767276839 行未改动280828092810281128122812281328142815281687 行未改动290429052906290729082908290929102911291229122913291429152916291629172918291929202921291929202921292229232924292529262927292829292929293029312932293329342934293529362937293829392940294129422941294229432944294529462 行未改动294929502951295229532954295329542955295629572958295929602961296229622963296429652966296729682968296929702971297229732973297429752976297796 行未改动307430753076307730783078307930803081308235 行未改动31183119312031213122312231233124312531267 行未改动313431353136313731383138313931403141314234 行未改动317731783179318031813181318231833184318532 行未改动321832193220322132223222322332243225322673 行未改动330033013302330333043304330533063307330810 行未改动331933203321332233233323332433253326332737 行未改动336533663367336833693369337033713372337313 行未改动338733883389339033913391339233933394339522 行未改动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 { 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) 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 ++) 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; 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> #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这个节点,更新最短路 long long d[N];//表示从s出发到x节点的最短路长度int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路queue<int> q;void dij(){ int x; 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; 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这个节点,更新最短路 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; return 0;}```### dijkstra 堆优化```cpp#include<bits/stdc++.h>using namespace std; 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; 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[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]) 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]) 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'; 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);//排序 sort(e+1,e+1+m);//排序 for(int i=1;i<=n;i++) { p[i]=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) if(x==y) continue; p[x]=y;//合并 p[x]=y;//合并 su+=e[i].w; sz[y]+=sz[x]; }}5 行未改动 kruskal(); if(sz[find(1)]!=n) cout<<"orz"; else cout<<su; return 0; cout<<su; return 0; }```
### prim(未优化)
```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e3 + 66,INF = 0x3f3f3f3f; 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; 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这个节点,更新最短路 long long d[N],su;int vis[N];//vis[x]表示是否选取过x这个节点,更新最短路priority_queue<pair<long long,int> >q;void prim(){ int x; 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; 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); 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; 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) 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'; 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); 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); 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 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 { 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; 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 ++) 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});//记录长度 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);//判断是否是减去的 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) 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;//左 右 第几行 上还是下 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];//记录线段 }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) #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)];//否则 }//改变 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;//标记永久化 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); }//区间加 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;//离散化 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];//与上一个线段所组成的矩形面积 (高) * 长 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; cout << L << " " << R; return 0;}```# 数学35 行未改动 if(s == 0) cout << "No" << endl; else cout << "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 { 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'; 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]; 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 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 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 行未改动