版本比较
爱捷wiki(代码版)
正文
增加 1542 行删除 70 行
12324456678910111213141516171872 行未改动91929394889596979899158 行未改动全部展开2582592602612552622632642582652662602612672682642692702662712722682692732742722752762772782762792802812822802832842852862842872882892902882912922932942952962972982962993003013023032 行未改动3063073083093073103113123133146 行未改动32132232332432232532632732832919 行未改动3493503513523503533543553563572 行未改动3603613623633643653623663673643683693663703713723733743753723763773743783793803813827 行未改动39039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345416 行未改动4714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177252 行未改动18251826182718281829183018311832183318341835183618 行未改动18551856185718581859186018611862501502186318641865186650418671868186918701871187218731874187518761877187818791880188118821883188410 行未改动189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193453553619351936193719381939194019411942194319445411945194619471948194919501951195210 行未改动19631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012569570571572573574201320142015201620172018201957620202021578579580581582583202220232024202520262027202820292030587588589590591203120322033203420352036593203720382039204020412042204359559659759859960060120442045204620472048204920502051603205220532054205520566086096102057205820592060612613614206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820896222090209120922093626627628629209420952096209720982099210021012102---
# 爱捷wiki(代码版)这些代码是作者于2023年蓝桥杯时抄写的,
作者整理了一下码风,一篇好的wiki需要头图:
另有[烂梗版](https://www.luogu.me/article/pwbppy6z)。[](http://aijieoj.cn/)
这些代码部分是作者于2023年蓝桥杯时抄写的,
作者整理了一下码风。
其他文章请点击:[文章中转站](https://www.luogu.me/article/mjgcb2o2)
## 排序篇(样例代码数组大小为1005)
### 冒泡排序 $O(n^2)$
72 行未改动 return 0;}```
### 桶排序 $O(n)$### 计数排序 $O(n)$
```#include<bits/stdc++.h>using namespace std;158 行未改动全部展开```
## 数据结构篇(描述)
$\tiny 注:资料源于网络搜集及作者主观评价$$\tiny 注:资料源于网络搜集及作者评价$### 数组
连续内存空间存储相同类型元素,大小固定(静态)连续内存空间存储相同类型元素,大小是固定的。
这东西平常最常用。
### Vector
既能随机访问还能任意扩展既能随机访问+任意扩展
和数组有一个方面得竞争一下:话说邻接矩阵和邻接表……
$\small公元2025年,邻接矩阵与邻接表又双叒叕大战开始了。$
### 链表
这玩意怎么说,虽然不能随机访问,可是插入删除$O(1)$诶。(~~话说listbug很少吧~~)不能随机访问,可是插入删除$O(1)$诶。(~~话说list的bug很少吧~~)
### 哈希表
为数不多的能将查找做到$O(1)$的数据结构。一种传奇的能将查找做到$O(1)$的数据结构。
### Queue
FIFO,不能随机访问,给bfs用行。FIFO,不能随机访问,还是给bfs吧。
### Stack
LIFO,同样不能随机访问,给dfs用行。LIFO,同样不能随机访问,还是给dfs吧。
### Deque
得亏可随机访问,不然和上面两家一样了。可随机访问,不然和上面两家一样了。
## 数据结构篇(时间复杂度)
|操作|数组|Vector|链表|哈希表|Queue|Stack|Deque||:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:||访问元素|$O(1)$|$O(1)$|$O(n)$|$O(1)$|$O(n)$|$O(n)$|$O(1)$||头部插入|$O(n)$|$O(n)$|$O(1)$|$O(1)$|-|-|$O(1)$||尾部插入|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$| |尾部插入|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$|$O(1)$||中间插入|$O(n)$|$O(n)$|$O(1)$|-|-|-|-||头部删除|$O(n)$|$O(n)$|$O(1)$|-|$O(1)$|-|$O(1)$||尾部删除|$O(1)$|$O(1)$|$O(1)$|-|-|$O(1)$|$O(1)$||中间删除|$O(n)$|$O(n)$|$O(1)$|-|-|-|-|2 行未改动## 数据结构篇(缺点)
### 数组
缺点很明显——空间浪费很明显的缺点:容易空间浪费。
```#include<bits/stdc++.h>using namespace std;6 行未改动```
### Vector
可能每次要写长的跟棍似的的代码没有auto的就只能写长点了。
```std::vector<int>::iterator t=upper_bound(vec.begin(),vec.end(),value);```19 行未改动### 哈希表
冲突问题
这样:一个好的哈希表应当是这样的:
|哈希表||:-:||信息|2 行未改动|信息||信息||信息||信息||信息……|
和这样:一个非常不好的哈希表是这样的:
|哈希表|哈希表|哈希表|哈希表|哈希表|哈希表|哈希表||哈希表|哈希表|哈希表|哈希表|哈希表|哈希表|哈希表……||:-:|:-:|:-:|:-:|:-:|:-:|:-:||信息|冲突链|冲突链|冲突链|冲突链|冲突链|冲突链||信息→|链→信息|链→信息|链→信息|链→信息|链→信息|链……|
### Queue&Stack&Deque
Queue和Stack不能随机访问
并且在上面的复杂度分析可以看到见上复杂度分析又可得知
Deque的属性比Queue和Stack好的不是一点Deque的属性比Queue和Stack好的多
那么为什么还会有Queue和Stack捏?
当然是因为常数巨大捏:7 行未改动|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|$TLE$|
## 模板篇
### 快读
```inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-')f = -1; ch = getchar(); } while (ch <= '9' && ch >= '0')x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar(); return x * f;}```
### 快写
```inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } int f[100001], ans = 0; while (x) { f[++ans] = x % 10 + '0'; x /= 10; } while (ans > 0) putchar(f[ans--]); putchar('\n');}```
### 邻接矩阵
```int a[N][N];for(int i=1;i<=n;i++){ int x,y; cin>>x>>y; a[x][y]=1/*权值*/; a[y][x]=1/*权值*/;}```
### vector邻接表存图
```vector<int>g[N];for(int i=1;i<n;i++){ cin>>u>>v; g[u].push_back(v); g[v].push_back(u);}```
### 快速幂
```#include<bits/stdc++.h>16 行未改动 cout<<qmi(a,b,mod); return 0;}```
### 龟速乘
```while(b){ if(b&1)ans=(ans+a)%p; a=(a+a)%p; b=b>>1;}```
### 矩阵快速幂
```#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN=1000;const int Mod=1e9+7;long long n,k;struct Node{ long long mx[MAXN][MAXN];}a,b,s;Node operator *(const Node &a,const Node &b){ Node c; memset(c.mx,0,sizeof(c.mx)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int p=1;p<=n;p++) c.mx[i][j]=(c.mx[i][j]+(b.mx[i][p]*a.mx[p][j])%Mod)%Mod; return c;}int main(){ cin>>n>>k; for(int i=1;i<=n;i++)s.mx[i][i]=1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a.mx[i][j]; while(k){ if(k&1)s=s*a; a=a*a; k>>=1; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<s.mx[i][j]<<" "; } cout<<endl; } return 0;}```
### 单向链表
```#include<bits/stdc++.h>#include<cstdio>#define endl '\n'#define int long long
const int N = 1e4 + 10;
using namespace std;
struct node{ int date; struct node* next;};
node* head=new node;
void add(int x,int y){ node* p=new node; p->date=y; p->next=NULL; node* q=head; while(q!=NULL){ if(q->date==x){ p->next=q->next; q->next=p; break; } q=q->next; }}
int ask(int x){ node* q=head; while(q!=NULL){ if(q->date==x){ if(q->next!=NULL){ return q->next->date; } else return 0; } q=q->next; }}
void del(int x){ node* q=head; while(q!=NULL){ if(q->date==x){ //node* r=q->next; q->next=q->next->next; //free(r); return; } q=q->next; }}
signed main(){
head->date=1; head->next=NULL; int n; cin>>n; while(n--){ int op; cin>>op; if(op==1){ int x,y; cin>>x>>y; add(x,y); } else if(op==2){ int x; cin>>x; cout<<ask(x)<<endl; } else{ int x; cin>>x; del(x); } }
return 0;}```
### 双向链表
```#include<bits/stdc++.h>#include<cstdio>#define endl '\n'#define int long long
const int N = 1e4+10;
using namespace std;
struct node{ int data; struct node* next; struct node* prev;};
int n,qs;int len;
node* head=new node;
void addl(int x,int y){ node* p=new node; p->data=x; p->next=NULL; p->prev=NULL; node* q =head; while(q!=NULL){ if(q->data==y){ if(q->prev!=NULL) q->prev->next=p; p->next=q; p->prev=q->prev; q->prev=p; break; } q=q->next; }}void addr(int x,int y){ node* p=new node; p->data=x; p->next=NULL; p->prev=NULL; node* q=head; while(q!=NULL){ if(q->data==y){ if(q->next!=NULL) q->next->prev=p; p->next=q->next; p->prev=q; q->next=p; break; } q=q->next; }}void init(){ head->data=1; head->next=NULL; head->prev=NULL; for(int i=2;i<=n;i++) addr(i,i-1);}
void del(int x){ node* q=head; while (q!=NULL){ if (q->data==x){ node* r=q; if(q->prev!=NULL) q->prev->next=q->next; if(q->next!=NULL) q->next->prev=q->prev; free(q); return; } q=q->next; }}
signed main(){
cin>>n>>qs; len=n; init(); while(qs--){ int op,x,y; cin>>op; if(op==1){ cin>>x>>y; if(x==y)continue; del(x); addl(x,y); } else if(op==2){ cin>>x>>y; if(x==y)continue; del(x); addr(x,y); } else if(op==3){ int x; cin>>x; del(x); len--; } } if(len<=0){ cout<<"Empty!"<<endl; return 0; } node* q=head; while(q!=NULL){ cout<<q->data<<" "; q=q->next; }
return 0;}```
### 分解质因数
```#include<bits/stdc++.h>const int N = 1e7 + 10, M = 1e5 + 10;using namespace std;bool st[N];int primes[N];int cnt, n;int fact[N];void get_primes() { for (int i = 2;i <= n;i++) { if (!st[i]) primes[cnt++] = i; for (int j = 0;i * primes[j] <= n;j++) { st[primes[j] * i] = true; fact[primes[j] * i] = primes[j]; if (i % primes[j] == 0) break; } }}int main() { n = 10000000; int t; cin >> t; get_primes(); while (t--) { int a; cin >> a; while (fact[a] > 1) { int t = fact[a]; int cnt = 0; while (a % t == 0) a /= t, cnt++; cout << t << " " << cnt << endl; } if (a > 1) cout << a << " " << 1 << endl; cout << "-----------------" << endl; }
return 0;}```
### 线性筛
```void get() { for (int i = 2;i <= n;i++) { if (!st[i]) primes[cnt++] = i; for (int j = 0;i * primes[j] <= n;j++) { st[primes[j] * i] = true; fact[primes[j] * i] = primes[j]; if (i % primes[j] == 0) break; } }}```
### 埃氏筛
```void get() { for (int i = 2;i <= n;i++) { if (st[i] == 1)continue; for (int j = i + i;j <= n;j += i) { st[j] = 1; // if (fact[j] == 0) fact[j] = i; fact[j] = min(fact[j], i); } }}```
### 暴力筛
```void get() { for (int i = 2;i <= n;i++) { for (int j = i + i;j <= n;j += i) { st[i] = 1; fact[j] = min(fact[j], i); } }}```
### 字符串哈希
```#include<bits/stdc++.h>#define int long long#define endl "\n"#define mod 998244353using namespace std;const int N=1e6+6,inf=2e18;typedef pair<int,int>PII;const int MAXN=10010;const int jz=21133;char s[MAXN];int n,a[MAXN];int h(char c[]){ int len=strlen(c); int ans=0; for(int i=0;i<len;i++) ans=(ans*jz+(int)s[i])%mod; return ans;}signed main(){ cin>>n; for(int i=1;i<=n;i++){ scanf("%s",s); a[i]=h(s); } sort(a+1,a+n+1); int ans=0; for(int i=2;i<=n;i++) if(a[i]!=a[i-1])ans++; cout<<ans+1; return 0;}```
### 树状数组(单点修改,区间查询)
```#include<bits/stdc++.h>#define int long long#define endl "\n"using namespace std;const int N=1e6+6;int m,n,q,x,y,ans,a[N],c[N];int lowbit(int x){ return x&-x;}int query(int x){ int ans=0; for(int i=x;i>0;i-=lowbit(i)) ans+=c[i]; return ans;}void add(int x,int k){ for(int i=x;i<=n;i+=lowbit(i)) c[i]+=k;}signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i+=1){ int t; cin>>t; add(i,t); } while(m--){ cin>>q>>x>>y; if(q==1) add(x,y); if(q==2) cout<<query(y)-query(x-1)<<endl; } return 0;}```
### 树状数组(区间修改,单点查询)
```#include<bits/stdc++.h>#define int long long#define endl "\n"using namespace std;const int N=1e6+6;int m,n,q,x,y,ans,a[N],c[N];int lowbit(int x){ return x&-x;}int query(int x){ int ans=0; for(int i=x;i>0;i-=lowbit(i)) ans+=c[i]; return ans;}void add(int x,int k){ for(int i=x;i<=n;i+=lowbit(i)) c[i]+=k;}signed 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]; add(i,a[i]-a[i-1]); } while(m--){ int op; cin>>op; if(op==1){ int l,r,k; cin>>l>>r>>k; add(l,k); add(r+1,-k); } else{ int x;cin>>x; cout<<query(x)<<endl; } } return 0;}```
### 二维树状数组
```#include <cstdio>#define lowbit(x) (x&(-x))using namespace std;
int n = 100 , m = 100;int qwq[105][105];
void add(int x , int y , int k){ for(int i = x;i <= n;i += lowbit(i)) for(int j = y;j <= m;j += lowbit(j)) qwq[i][j] += k;}
int getsum(int x , int y){ int ans = 0; for(int i = x;i >= 1;i -= lowbit(i)) for(int j = y;j >= 1;j -= lowbit(j)) ans += qwq[i][j]; return ans;}
int main(void){ add(1 , 1 , 1); add(1 , 2 , 2); printf("%d\n" , query(1 , 2));}```
### KMP
```
#include<bits/stdc++.h>#define int long long#define endl "\n"#define mod 998244353using namespace std;const int N=1e6+6;int kmp[N],la,lb,j;string a,b,A,B;signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>A>>B; a='\0'+A,b='\0'+B; la=a.size()-1,lb=b.size()-1; for(int i=2;i<=lb;i++){ while(j&&b[i]!=b[j+1]) j=kmp[j]; if(b[j+1]==b[i]) j++; kmp[i]=j; } j=0; for(int i=1;i<=la;i++){ while(j>0&&b[j+1]!=a[i]) j=kmp[j]; if(b[j+1]==a[i]) j++; if(j==lb){ cout<<i-lb+1<<endl; j=kmp[j]; } } for(int i=1;i<=lb;i++) cout<<kmp[i]<<" "; return 0;}```
### 并查集
```#include<bits/stdc++.h>#define int long long#define endl "\n"using namespace std;const int N=1e6+6;int n,m,z,x,y,p[N];int fi(int x){ if(x!=p[x])p[x]=fi(p[x]); return p[x];}signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++)p[i]=i; while(m--){ cin>>z>>x>>y; if(z==1){ x=fi(x); y=fi(y); p[y]=x; } else{ x=fi(x); y=fi(y); if(x!=y)cout<<"N"<<endl; else cout<<"Y"<<endl; } } return 0;}```
### 堆
```#include<bits/stdc++.h>#define int long longusing namespace std;const int N=9e6+6;int a[N],m,n,si;void doun(int u){ int t=u; if(a[u*2]<a[t]&&u*2<=si)t=u*2; if(u*2+1<=si&&a[u*2+1]<a[t])t=u*2+1; if(t!=u){ swap(a[t],a[u]); doun(t); }}void up(int u){ int t=u; if(u/2>=1&&a[t]<a[u/2]){ t=u/2; } if(t!=u){ swap(a[t],a[u]); up(t); }}signed main(){ cin>>n; while(n--){ int op; cin>>op; if(op==1){ int x; cin>>x; a[++si]=x; up(si); } else if(op==2){ cout<<a[1]<<endl; } else if(op==3){ swap(a[1],a[si]); si--; doun(1); } } return 0;}```
### 单调栈
```#include<bits/stdc++.h>#define int long longusing namespace std;const int N=9e6+6;int n,a[N],ans[N];stack<int>s;signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=n;i>=1;i--){ while(s.size()&&a[i]>=a[s.top()])s.pop(); if(s.size())ans[i]=s.top(); else ans[i]=0; s.push(i); } for(int i=1;i<=n;i+=1)cout<<ans[i]<<" "; return 0;}```
### 最小生成树
```#include<bits/stdc++.h>#define int long long#define endl "\n"#define mod 998244353using namespace std;const int N=1e6+6;typedef pair<int,int>PII;inline int read(){ int s=0,w=1; char c=getchar(); while(c<48||c>57){ if(c=='-') w=-1; c=getchar(); } while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar(); return s*w;}int n,m,p[N],sz[N],cnt,res;array<int,3>a[N];int find(int x) { if (x != p[x]) p[x] = find(p[x]); return p[x];}signed main() { cin.tie(0)->sync_with_stdio(0); cout.tie(0); cin >> n >> m; for (int i = 1;i <= n;i++) p[i] = i; for (int i = 1;i <= m;i++) { int u, v, w; cin >> u >> v >> w; a[i] = { w,u,v }; } sort(a + 1, a + 1 + m); for (int i = 1;i <= m;i++) { int w = a[i][0], u = a[i][1], v = a[i][2]; u = find(u), v = find(v); if (u == v) continue; res += w; p[u] = p[v]; cnt++; } if (cnt == n - 1) cout << res << endl; else cout << "orz" << endl;}```
### 拓扑排序
```#include<bits/stdc++.h>#define int long long#define endl "\n"using namespace std;const int N=1e3+6;vector<int>g[N];int d[N],n,a;signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for (int i = 1;i <= n;i++) { int a; while (cin >> a) { if (a == 0) break; g[i].push_back(a); d[a]++; } } queue<int>q; for (int i = 1;i <= n;i++) { if (d[i] == 0) q.push(i); } while (q.size()) { int t = q.front(); q.pop(); cout << t << " "; for (auto it : g[t]) { if (--d[it] == 0) q.push(it); } } return 0;}```
### 滑动窗口/单调队列
```#include<bits/stdc++.h>#define int long longusing namespace std;const int N=1e6+6;typedef pair<int,int>PII;int a[N],ansx[N],ansi[N],n,k;priority_queue<PII>q;priority_queue<PII,vector<PII>,greater<PII> >p;signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i]; q.push({a[i],i}); p.push({a[i],i}); if(i>=k){ while(q.size()&&i-q.top().second>=k)q.pop(); while(p.size()&&i-p.top().second>=k)p.pop(); ansx[i]=q.top().first; ansi[i]=p.top().first; } } for(int i=k;i<=n;i++)cout<<ansi[i]<<" "; cout<<endl; for(int i=k;i<=n;i++)cout<<ansx[i]<<" "; return 0;}```
### st表
```#include<bits/stdc++.h>#define int long long#define endl "\n"using namespace std;const int N=1e6+6;inline int read(){ int s=0,w=1; char c=getchar(); while(c<48||c>57){ if(c=='-') w=-1; c=getchar(); } while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar(); return s*w;}int n,m,f[N][50],s,e;signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) cin>>f[i][0]; for(int j=1;j<=20;j++){ for(int i=1;i<=n;i++){ if(i+(1<<j)-1<=n) f[i][j]=min(f[i][j-1],f[i+(1<<j-1)][j-1]); } } for(int i=1;i<=m;i++){ int l,r; cin>>l>>r; int o=log2(r-l+1); cout<<min(f[l][o],f[r-(1<<o)+1][o])<<" ";
} return 0;}```
### 带权并查集
```#include<bits/stdc++.h>using namespace std;int fa[30001],front[30001],num[30001],x,y,i,j,n,T,ans;char ins;int find(int n){ if(fa[n]==n)return fa[n]; int fn=find(fa[n]); front[n]+=front[fa[n]]; return fa[n]=fn;}int main(){ cin>>T; for(i=1;i<=30000;++i){ fa[i]=i; front[i]=0; num[i]=1; } while(T--){ cin>>ins>>x>>y; int fx=find(x); int fy=find(y); if(ins=='M'){ front[fx]+=num[fy]; fa[fx]=fy; num[fy]+=num[fx]; num[fx]=0; } if(ins=='C'){ if(fx!=fy)cout<<"-1"<<endl;else cout<<abs(front[x]-front[y])-1<<endl; } } return 0;}```
### 字典树
```#include<bits/stdc++.h>#define int long long#define endl "\n"#define mod 998244353using namespace std;const int N=3e6+6,inf=2e18;typedef pair<int,int>PII;int T,q,n,a[N][65],cnt[N],idx;char s[N];int getnum(char x){ if(x>='A'&&x<='Z') return x-'A'; else if(x>='a'&&x<='z') return x-'a'+26; else return x-'0'+52;}void insert(char str[]){ int p=0,len=strlen(str); for(int i=0;i<len;i++){ int c=getnum(str[i]); if(!a[p][c]) a[p][c]=++idx; p=a[p][c]; cnt[p]++; }}int find(char str[]){ int p=0,len=strlen(str); for(int i=0;i<len;i++){ int c=getnum(str[i]); if(!a[p][c]) return 0; p=a[p][c]; } return cnt[p];}signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>T; while(T--){ for(int i=0;i<=idx;i++) for(int j=0;j<=122;j++) a[i][j]=0; for(int i=0;i<=idx;i++) cnt[i]=0; idx=0; cin>>n>>q; for(int i=1;i<=n;i++){ cin>>s; insert(s); } for(int i=1;i<=q;i++){ cin>>s; cout<<find(s)<<endl; } } return 0;}
```
### 线段树
```#include<bits/stdc++.h>#define int long long
using namespace std;const int N=1e6+6;int a[N],t[N<<2],lz[N<<2];void lazy(int ro,int l,int r,int k);void push_down(int ro,int l,int r);void push_up(int ro,int l,int r);void built(int ro,int l,int r);void update(int ro,int l,int r,int x,int y,int k);int aks(int ro,int l,int r,int x,int y);signed main() { cin.tie(nullptr)->sync_with_stdio(false); int n,m;cin>>n>>m; for(int i=1;i<=n;++i)cin>>a[i]; built(1,1,n); for(;m;m--) { int op;cin>>op; if(op==1) { int x,y,k;cin>>x>>y>>k; update(1,1,n,x,y,k); } else if(op==2) { int x,y;cin>>x>>y; cout<<aks(1,1,n,x,y)<<endl; } } return 0;}void lazy(int ro,int l,int r,int k) { t[ro]+=k*(r-l+1),lz[ro]+=k;}void push_down(int ro,int l,int r) { if(!lz[ro])return; int mid=l+r>>1; lazy(ro<<1,l,mid,lz[ro]),lazy(ro<<1|1,mid+1,r,lz[ro]); lz[ro]=0;}void push_up(int ro) { t[ro]=t[ro<<1]+t[ro<<1|1];}void built(int ro,int l,int r) { lz[ro]=0; if(l==r) { t[ro]=a[l];return; } int mid=l+r>>1; built(ro<<1,l,mid),built(ro<<1|1,mid+1,r); push_up(ro);}void update(int ro,int l,int r,int x,int y,int k) { if(x<=l&&r<=y) { lazy(ro,l,r,k); return; } push_down(ro,l,r); int mid=l+r>>1; if(x<=mid)update(ro<<1,l,mid,x,y,k); if(mid<y)update(ro<<1|1,mid+1,r,x,y,k); push_up(ro);}int aks(int ro,int l,int r,int x,int y){ if(x<=l&&r<=y) { return t[ro]; } push_down(ro,l,r); int res=0; int mid=l+r>>1; if(x<=mid)res+=aks(ro<<1,l,mid,x,y); if(mid<y)res+=aks(ro<<1|1,mid+1,r,x,y); return res;}```
### 01背包
```#include <cstdio>#include <algorithm>using namespace std;
long long W , N , dp[100005] , w[10005] , v[10005];
int main(void){ scanf("%lld%lld" , &N , &W); for(int i = 1;i <= N;i++) scanf("%lld%lld" , &w[i] , &v[i]); for(int i = 1;i <= N;i++) for(int j = W;j >= 0;j--) if(j - w[i] >= 0) dp[j] = max(dp[j - w[i]] + v[i] , dp[j]); printf("%lld\n" , dp[W]);}```
### 完全背包
```#include <cstdio>#include <iostream>#define maxn 500005using namespace std;
int dp[maxn];int N , W;int w[505] , v[505];
int main(void){ scanf("%d%d" , &N , &W); for(int i = 1;i <= N;i++) scanf("%d%d" , &w[i] , &v[i]); for(int i = 1;i <= N;i++) for(int j = w[i];j <= W;j++) dp[j] = max(dp[j] , dp[j - w[i]] + v[i]); printf("%d\n" , dp[W]);}```
### 多重背包
```#include <cstdio>#include <iostream>using namespace std;
int dp[505][50005];int N , W;int w[505] , v[505];
int main(void){ scanf("%d%d" , &N , &W); for(int i = 1;i <= N;i++) scanf("%d%d" , &w[i] , &v[i]); for(int i = 1;i <= N;i++){ for(int j = 1;j <= W;j++){ for(int k = 0;k <= min(2 , j / w[i]);k++){ dp[i][j] = max(dp[i][j] , dp[i - 1][j - w[i] * k] + v[i] * k); } } } printf("%d\n" , dp[N][W]);}```
### 差分
```#include <iostream>using namespace std;
int n , m , last;int sum[10005] , b[10005] , s[10005];
int main(void){ cin.tie(0); cout.tie(0); cin >> n; for(int i = 1;i <= n;i++){ int x; cin >> x; b[i] = x - last; last = x; } cin >> m; for(int i = 1;i<=m;i++){ int l , r , x; cin >> l >> r >> x; b[l] += x; b[r + 1] -= x; } for(int i = 1;i <= n;i++){ s[i] = s[i - 1] + b[i]; sum[i] = sum[i - 1] + s[i]; } cin >> l >> r; cout << sum[r] - sum[l - 1] << "\n";}```
### GCD
```inline long long gcd(long long x , long long y){ return (y > 0? gcd(y , x % y) : x);}```
### LCM
```inline long long lcm(long long x , long long y){ return x / gcd(x , y) * y;}```
### LCA最近公共祖先
```int N , M , S;vector <int> g[500005];int shen[500005];int fa[500005][22] , lg[500005];bool vis[500005];
void dfs(int x , int y , int father){ if(vis[x]) return ; vis[x] = true; shen[x] = y; fa[x][0] = father; for(int i = 1;i <= lg[shen[x]];i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; //2^i = 2^(i - 1) + 2^(i - 1) for(int next : g[x]) dfs(next , y + 1 , x);}
int LCM(int x , int y){ if(shen[x] < shen[y]) swap(x , y); while(shen[x] > shen[y]) x = fa[x][lg[shen[x] - shen[y]] - 1]; if(x == y) return x; for(int k = lg[shen[y]] - 1;k >= 0;k--){ if(fa[x][k] != fa[y][k]){ x = fa[x][k]; y = fa[y][k]; } } return fa[x][0];}
int main(void){ scanf("%d%d%d" , &N , &M , &S); for(int i = 1;i < N;i++){ int a , b; scanf("%d%d" , &a , &b); g[a].push_back(b); g[b].push_back(a); } for(int i = 1;i <= N;i++) lg[i] = lg[i - 1] + (1 << lg[i - 1] == i); dfs(S , 1 , 0); while(M--){ int a , b; scanf("%d%d" , &a , &b); printf("%d\n" , LCM(a , b)); }}```
### 二分图最大权完美匹配
```#include <cstdio>#include <vector>#include <cstring>#define inf 0x7fffffffffffffffusing namespace std;
int n , m , e;vector <int> g[505];// vector <long long> gv[505];bool visl[505] , visr[505];long long www[505][505];long long lj[505] , aaa[505] , bbb[505]; //a,b好感long long qwq , ans; //要降的期待值
char buf[1 << 21] , *p1 , *p2;#define GC p1 == p2 && (p2 = (p1 = buf) + fread(buf , 1 , 1 << 21 , stdin) , p1 == p2) ? EOF : *p1++inline int read() { int x = 0; bool f = false; char ch = GC; while(ch > '9' || ch < '0'){ if(ch == '-') f = true; ch = GC; } while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ 48); ch = GC; } return f? -x : x;}
bool dfs(int x){ visl[x] = true; int len = g[x].size(); for(int i = 0;i < len;i++){ int next = g[x][i]; if(visr[next]) continue; if(aaa[x] + bbb[next] == www[x][next]){ visr[next] = true; if(!lj[next] || dfs(lj[next])){ lj[next] = x; visr[next] = true; return true; } } else qwq = min(qwq , aaa[x] + bbb[next] - www[x][next]); } return false;}
void find(){ for(int i = 1;i <= n;i++){ aaa[i] = -inf; for(int next : g[i]) aaa[i] = max(www[i][next] , aaa[i]); } for(int i = 1;i <= n;i++){ while(true){ memset(visl , 0 , sizeof(visl)); memset(visr , 0 , sizeof(visr)); qwq = inf; if(dfs(i)) break; for(int j = 1;j <= n;j++) if(visl[j]) aaa[j] -= qwq; for(int j = 1;j <= m;j++) if(visr[j]) bbb[j] += qwq; } }}
int main(void){ n = read() , e = read(); m = n; while(e--){ int u , v; long long w; u = read() , v = read() , w = read(); g[u].push_back(v); www[u][v] = w; } find(); for(int i = 1;i <= n;i++) ans += www[lj[i]][i]; printf("%lld\n" , ans); for(int i = 1;i <= n;i++) printf("%d " , lj[i]);}```
## 小技巧篇
### 位运算加法
52 行未改动 string a,b; cin>>a>>b; int lena=a.length(); int lenb=b.length(); if(a=="0"&&b=="0"){ cout<<0<<endl; return 0; } int a1[100000]={},b1[100000]={},c1[100000]={}; for(int i=0;i<lena;i++){ a1[lena-1-i]=a[i]-'0'; }18 行未改动### 高精度减
```#include<bits/stdc++.h>#define endl '\n'#define int long longconst int N = 2e5 + 10;using namespace std;int main(){typedef pair<int, int> PII;int a[100000]={0},b[100000]={0},res[100000]={0};void solve() { string s1,s2; int a[100000]={0},b[100000]={0},res[100000]={0}; cin>>s1>>s2; int fl=1; if(s1==s2){ cout<<0<<endl; return ; } if(s1.size()<s2.size()){ fl=0; swap(s1,s2); } if(s1.size()==s2.size()&&s1<s2){ fl=0; swap(s1,s2); } int l1=s1.length(); int l2=s2.length(); for(int i=0;i<l1;i++){ a[l1-i-1]=s1[i]-'0';10 行未改动 } } for(;res[i]==0;i--); string u=""; if(fl==0)cout<<'-'; for(;i>=0;i--) cout<<res[i];}signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int _ = 1;// cin >> _; while (_--) solve(); return 0;}```
### 高精度乘
```#include<bits/stdc++.h>#include<cstdio>#define endl '\n'#define ll long long#define isON(x) (x&1)#define lbt(x) ((x) & (-(x)))#define Rup(a,b) ((a+b-1)/b)#define all(x) x.begin(),x.end()#define gets(S) fgets(S,sizeof(S),stdin)#define S(a,n,x) sort(a+x,a+x+n)#define mod 998244353#define int long long//#define Fre#define Ct
const int N = 2e5 + 10;
using namespace std;int main(){
typedef pair<int, int> PII;
int a1[100000]={},b1[100000]={},c1[100000]={};
void solve() { string a,b; cin>>a>>b; int lena=a.length(); int lenb=b.length(); int a1[100000]={},b1[100000]={},c1[100000]={}; if(a=="0"||b=="0"){ cout<<0<<endl; return ; } for(int i=0;i<lena;i++){ a1[lena-1-i]=a[i]-'0'; } for(int i=0;i<lenb;i++){10 行未改动 for(;c1[i]==0;i--); for(;i>=0;i--){ cout<<c1[i]; }}
signed main() {
#ifdef Fre freopen("expert.in", "r", stdin); freopen("expert.out", "w", stdout);#endif
#ifdef Ct ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);#endif
int _ = 1;// cin >> _; while (_--) solve();
return 0;}```
### 高精度除(整除,此代码先输出商后输出余数)
```#include<bits/stdc++.h>#include<cstdio>#define endl '\n'#define ll long long#define isON(x) (x&1)#define lbt(x) ((x) & (-(x)))#define Rup(a,b) ((a+b-1)/b)#define all(x) x.begin(),x.end()#define gets(S) fgets(S,sizeof(S),stdin)#define S(a,n,x) sort(a+x,a+x+n)#define mod 998244353#define int long long//#define Fre#define Ct
const int N = 2e5 + 10;
using namespace std;
typedef pair<int, int> PII;#define N 100000int a[N]={0},b[N]={0},c[N]={0},tmp[N]={0},len_a,len_b,len_c,len_t;string s1,s2;void mymove(int distance){ for(int i=1;i<=len_b;i++){ tmp[i+distance-1]=b[i];
int a[N] = {0}, b[N] = {0}, c[N] = {0}, tmp[N] = {0}, len_a, len_b, len_c, len_t;string s1, s2;void mymove(int distance) { for (int i = 1; i <= len_b; i++) { tmp[i + distance - 1] = b[i]; } len_t=len_b+distance-1; len_t = len_b + distance - 1;}bool cmp(){ if(len_a>len_t) return true; if(len_a<len_t) return false; for(int i=len_a;i>=1;i--){ if(a[i]>tmp[i]) return true; if(a[i]<tmp[i]) return false;bool cmp() { if (len_a > len_t) return true; if (len_a < len_t) return false; for (int i = len_a; i >= 1; i--) { if (a[i] > tmp[i]) return true; if (a[i] < tmp[i]) return false; } return true;}void subtraction(){ for(int i=1;i<=len_t;i++){ a[i]-=tmp[i]; if(a[i]<0) a[i+1]--,a[i]+=10;void subtraction() { for (int i = 1; i <= len_t; i++) { a[i] -= tmp[i]; if (a[i] < 0) a[i + 1]--, a[i] += 10; } while(a[len_a]==0&&len_a>1) len_a--; while (a[len_a] == 0 && len_a > 1) len_a--;}
void solve() { cin >> s1 >> s2; if(s1=="0"){ cout<<0<<endl<<0;int main(){ cin>>s1>>s2;len_a=s1.length(),len_b=s2.length(); for(int i=0;i<len_a;i++) a[len_a-i]=s1[i]-'0'; for(int i=0;i<len_b;i++) b[len_b-i]=s2[i]-'0'; len_c=len_a-len_b+1; for(int i=len_c;i>=1;i--){ return ; } len_a = s1.length(), len_b = s2.length(); for (int i = 0; i < len_a; i++) a[len_a - i] = s1[i] - '0'; for (int i = 0; i < len_b; i++) b[len_b - i] = s2[i] - '0'; len_c = len_a - len_b + 1; for (int i = len_c; i >= 1; i--) { mymove(i); while(cmp()){ while (cmp()) { c[i]++; subtraction(); } } while(c[len_c]==0&&len_c>1) len_c--; for(int i=len_c;i>=1;i--){ cout<<c[i]; while (c[len_c] == 0 && len_c > 1) len_c--; for (int i = len_c; i >= 1; i--) { cout << c[i]; } cout<<endl; for(int i=len_a;i>=1;i--){ cout<<a[i]; cout << endl; for (int i = len_a; i >= 1; i--) { cout << a[i]; }}
signed main() {
#ifdef Fre freopen("expert.in", "r", stdin); freopen("expert.out", "w", stdout);#endif
#ifdef Ct ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);#endif
int _ = 1;// cin >> _; while (_--) solve();
return 0;}```
# ——THE END——
$致谢$ $致谢$
$\small真菌团队$
$\tiny 主编:灵感菇。$ $\tiny 链接:金针菇,鸡油菌,鸡枞菌,青头菌。$ $\tiny 图片:奶浆菌,平菇,香菇,口蘑。$ $\tiny Markdown:毒蝇伞,珊瑚菌,出血齿菌,见手青。$ $\tiny 主编:灵感菇。$$\tiny 链接:金针菇,鸡油菌,鸡枞菌,青头菌。$$\tiny 图片:奶浆菌,平菇,香菇,口蘑。$$\tiny Markdown:毒蝇伞,珊瑚菌,出血齿菌,见手青。$$\tiny Katex:杏鲍菇,酵母菌,玉米瘤黑粉菌。$
一篇好的代码wiki需要尾图:
[](https://www.luogu.com.cn/user/645456)