版本比较
【板子】
正文
增加 445 行删除 59 行
112345218 行未改动全部展开224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366771 行未改动全部展开1138113911401141100711421143114411451146225 行未改动全部展开1372137313741375124113761377137813791380138 行未改动全部展开1519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627454 行未改动全部展开2082208320842085185120862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252103 行未改动全部展开23562357235823591963196423602361236223632364196719682365196919701971197223662367197423682369237019771978237123721980198119822373237423752376237723782379198623802381238219891990238323841992199319942385238623872388238923902391239223932394239523962004200520062397239823992400200820092010201120122013201420152016201720182019202020212022202320242025240124022026202720282029203020312403240424052406240724082409241024112035241224132414241560 行未改动247624772478247921042480248124822483248493 行未改动25782579258025812582258325842585258640 行未改动262726282629263022542631263226332634225922602261263526362637263852 行未改动26912692269326942322269526962697269826993 行未改动2703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272552 行未改动2778277927802781239627822783278427852786450 行未改动全部展开# DATA STRUCTURE# 【数据结构 / Data Structure】## 单调队列有些 dp 题要算 dp[i],注意单调性维护位置```cpp#include<bits/stdc++.h>218 行未改动全部展开
```
## 线段树### 清新版```cpp/*1 区间每个元素加上 k2 区间每个元素乘上 k4 输出区间和MADE BY SKYx*/
#include <bits/stdc++.h>using namespace std;#define ll long long// int->ll may useful#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se second
const int N=1e5+5;int n, q, x, y, k;struct node{ int l,r; int s,add,mul;
} tr[N<<2];
void build(int c,int l,int r){ auto &t=tr[c]; t.l=l; t.r=r; t.add=t.s=0; t.mul=1; if(l==r) { cin>>t.s; return; } int mid=(l+r)/2; build(c*2,l,mid); build(c*2+1,mid+1,r); t.s=(tr[c*2].s+tr[c*2+1].s); }
void pushdown(int c){ auto &t=tr[c]; if(t.mul!=1 || t.add!=0) { for(int i=0; i<2; i++) { auto &u=tr[c*2+i]; u.s=(u.s*t.mul+t.add*(u.r-u.l+1)); u.mul=u.mul*t.mul; u.add=(u.add*t.mul+t.add); } t.mul=1; t.add=0; }}void update(int c, int L, int R, int type, int v){ auto &t=tr[c]; if(R<t.l || t.r<L) return; if(L<=t.l && t.r<=R) { if (type==1) { t.s=(t.s+v*(t.r-t.l+1)); t.add=(t.add+v); } if (type==2) { t.s=t.s*v; t.mul=t.mul*v; t.add=t.add*v; } return; } pushdown(c); update(c*2,L,R,type,v); update(c*2+1,L,R,type,v); tr[c].s=(tr[c*2].s+tr[c*2+1].s);}
int query(int c, int L, int R){ auto &t=tr[c]; if(R<t.l || t.r<L) return 0; if(L<=t.l && t.r<=R) return t.s; pushdown(c); return (query(c*2,L,R)+query(c*2+1,L,R));}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>q; build(1,1,n);
while (q--) { int op; cin>>op; if (op == 1) { cin>>x>>y>>k; update(1,x,y,1,k); } if (op == 2) { cin>>x>>y>>k; update(1,x,y,2,k); } if (op == 4) { cin>>x>>y; cout<<query(1,x,y)<<'\n'; } } return 0;}/*
*/```
### 微缩版(带 mod)```cpp/*1 区间每个元素加上 k771 行未改动全部展开
```
# GRAPH THEORY & TREE# 【图论树论 / Graph Theory & Tree】## LCA```cpp#include <bits/stdc++.h>using namespace std;225 行未改动全部展开
return 0;}```## DIJ## Dijkstra```cpp#include <bits/stdc++.h>using namespace std;struct edge138 行未改动全部展开 cout<<dis[i]<<" "; return 0;}```## 差分约束| 原式 | 转化 | 建边 ||---|---|---|| $x_c-x_{c'}\le y$ | 不变 | `c' -> c`,权 $y$ || $x_c-x_{c'}\ge y$ | $x_{c'}-x_c\le -y$ | `c -> c'`,权 $-y$ || $x_c-x_{c'}< y$ | $x_c-x_{c'}\le y-1$ | `c' -> c`,权 $y-1$ || $x_c-x_{c'}> y$ | $x_{c'}-x_c\le -y-1$ | `c -> c'`,权 $-y-1$ || $x_c-x_{c'}= y$ | 拆成 $\le y$ 和 $\ge y$ | 上面两条边 |```cpp#include <bits/stdc++.h>using namespace std;#define ll long long#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se second
const int N = 10010;
int n, m;vector<pii> g[N];
int dis[N];int cnt[N];bool vis[N];
bool spfa(int s){ for (int i = 0; i <= n; i++) { dis[i] = INT_MAX; vis[i] = false; cnt[i] = 0; }
queue<int> q;
dis[s] = 0; vis[s] = true; q.push(s); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=0; for(pii e : g[x]) { int y=e.fi; int w=e.se; if(dis[y]>dis[x]+w) { dis[y]=dis[x]+w; cnt[y]=cnt[x]+1; if(cnt[y]>n) return 0; if(!vis[y]) { vis[y]=1; q.push(y); } } } }
return true;}
int main(){ ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; for(int i=1;i<=n;i++) { g[0].push_back({i,0}); } for(int i=1;i<=m;i++) { int c,cp,y; cin>>c>>cp>>y; g[cp].push_back({c,y}); } if(!spfa(0)) { cout<<"NO"<<endl; } else { for(int i=1;i<=n;i++) { cout<<dis[i]<<" "; } cout<<endl; }
return 0;}```
## Floyd```cpp for(int k=1;k<=n;k++) {454 行未改动全部展开 } return 0;}```# STRING## 二分图匹配```cpp#include <bits/stdc++.h>using namespace std;#define ll long long#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se secondconst int N=505;int mch[N];bool vis[N];vector<int> g[N];
bool dfs(int u){ for(int v : g[u]) { if(!vis[v]) { vis[v]=1; if(!mch[v] || (mch[v] && dfs(mch[v]))) { mch[v]=u; return 1; } } } return 0;}signed main(){ int sti=clock();
// freopen(".in","r",stdin);// freopen(".out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,e; cin>>n>>m>>e; for(int i=1;i<=e;i++) { int u,v; cin>>u>>v; g[u].push_back(v); } int ans=0; for(int i=1;i<=n;i++) { memset(vis,0,sizeof vis); if(dfs(i)) ans++; } cout<<ans<<endl;// cerr<<1.0*(clock()-sti)/CLOCKS_PER_SEC<<endl; return 0;}
```
# 【字符串 / String】## Hash找出一个最长的子串,并且使得这个子串在字符串中出现了至少两次(在不同的位置)。```cpp#include <bits/stdc++.h>using namespace std;#define int long long#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se second#define ull unsigned long longconst int N=2e6+10;
const ull P=307;const ull mod1=1e9+33;int n;string s;char ch[N];ull h1[N];ull h2[N];ull pw1[N];ull pw2[N];
void init(){ for(int i=0;i<s.size();i++) { ch[i+1]=s[i]; } for(int i=1;i<=n;i++) { h1[i]=h1[i-1]*P+ch[i]; h1[i]%=mod1; h2[i]=h2[i-1]*P+ch[i]; }
pw1[0]=1;pw2[0]=1; for(int i=1;i<=n;i++) { pw1[i]=pw1[i-1]*P%mod1; pw2[i]=pw2[i-1]*P; }
}inline ull query1(int l,int r){ return (h1[r]-h1[l-1]*pw1[r-l+1]%mod1+mod1)%mod1;}inline ull query2(int l,int r){ return h2[r]-h2[l-1]*pw2[r-l+1]; } unordered_set<ull> s1; unordered_set<ull> s2; bool check(int x){ s1.clear(); s2.clear(); for(int i=1;i+x-1<=n;i++) { ull p1=query1(i,i+x-1); ull p2=query2(i,i+x-1); if(s1.count(p1) && s2.count(p2)) return 1; s1.insert(p1); s2.insert(p2); } return 0;}signed main(){ int sti=clock();// freopen(".in","r",stdin);// freopen(".out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; cin>>s; init(); int l=1;int r=n;int ans=0; while(l<=r) { int mid=(l+r)/2; if(check(mid)) { ans=mid; l=mid+1; } else r=mid-1; } cout<<ans<<endl;// cerr<<1.0*(clock()-sti)/CLOCKS_PER_SEC<<endl; return 0;}
```
## KMP```cpp#include <bits/stdc++.h>using namespace std;103 行未改动全部展开## 字典树```cpp#include<bits/stdc++.h>using namespace std;int t[3000005][127],cnt[3000005],idx;char s[3000005];const int N=3e6+10; int tr[N][27],cnt[N],idx;string s;int getnum(char x){ if(x>='A'&&x<='Z') return x-'A'; return x-'a'; else if(x>='a'&&x<='z') return x-'a'+26; else return x-'0'+52;}
void insert(char str[])void insert(string& s){ int p=0; int len=strlen(str); for(int i=0;i<len;i++) for(int i=0;i<s.size();i++) { int c=getnum(str[i]); if(!t[p][c]) t[p][c]=++idx; p=t[p][c]; int c=getnum(s[i]); if(!tr[p][c]) tr[p][c]=++idx; p=tr[p][c]; cnt[p]++; }
}int find(char str[])int find(string& s){ int p=0; int len=strlen(str); for(int i=0;i<len;i++) for(int i=0;i<s.size();i++) { int c=getnum(str[i]); if(!t[p][c]) return 0; p=t[p][c]; int c=getnum(s[i]); if(!tr[p][c]) return 0; p=tr[p][c]; } return cnt[p];}
int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin>>T; while(T--) int n,q; cin>>n>>q; for(int i=1;i<=n;i++) { for(int i=0; i<=idx; i++) { for(int j=0;j<=122;j++) { t[i][j]=0; } } for(int i=0; i<=idx; i++) { cnt[i]=0; } int n,q; idx=0; cin>>n>>q; for(int i=1; i<=n; i++) { cin>>s; insert(s); cin>>s; insert(s); } for(int i=1; i<=q; i++) { cin>>s; cout<<find(s)<<'\n'; } } for(int i=1;i<=q;i++) { cin>>s; cout<<find(s)<<endl; }
return 0;}
```
## 01 TrieP10471。60 行未改动 return 0;}```
# MATH# 【数学 / Math】## 算术基本定理(唯一分解定理)```cpp#include <bits/stdc++.h>using namespace std;93 行未改动
$C(a, b) = \dfrac{a!}{b! (a-b)!} $
$P(a, b) = \dfrac{a!}{(a-b)!}$### 逆元与阶乘```cpp#include <bits/stdc++.h>using namespace std;#define int long long40 行未改动 for(int i=N-11;i>=1;i--) { inv[i]=inv[i+1]*(i+1)%mod; }
// cerr<<1.0*(clock()-sti)/CLOCKS_PER_SEC<<endl; return 0;}
```
```cpp#include <bits/stdc++.h>52 行未改动 cout<<p(x,y)<<endl; return 0;}```***### 杨辉三角```cpp c[0][0]=1; for(int i=1;i<=N;i++) {3 行未改动 c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } }```### 暴力展开```cppint C(int n,int k){ int res=1;
for(int i=1;i<=k;i++) { res=res*(n-k+i)/i; } return res;}```
## 卡特兰数```cpp printf("%d",c[2*n][n]-c[2*n][n-1]);```52 行未改动 if(n==1) return ans; else return ans=ans/n*(n-1);}```# 杂项# 【杂项 / Others】## 表达式求值支持 + - * / ^,正确的括号匹配。```cpp#include <bits/stdc++.h>450 行未改动全部展开