版本比较
模板题代码集合
正文
增加 514 行删除 411 行
1234567891011121314151617181920212223242526272829303188 行未改动12012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316455 行未改动22022122222322422522622722829 行未改动258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395107 行未改动全部展开50350450550650750850951051151251351451551651731 行未改动54955055155255355455555655755811 行未改动570571572573574575576577578579580581582583584585586124 行未改动全部展开7117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958965 行未改动90290390490590690790890991091111 行未改动9239249259269279289299309319329339347 行未改动9429439449459469479489499509519529539543 行未改动9589599609619629639649659669 行未改动976977978979980981982983984985986987988989990991992993994995996997998999100010011002100315 行未改动10191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149163 行未改动全部展开131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405136 行未改动全部展开1542154315441545154615471548154915501551155215531554155542 行未改动15981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916703 行未改动167416751676167716781679168016811682168316841685168616871688168916901691169257 行未改动17501751175217531754175517561757175812 行未改动177117721773177417751776177717782 行未改动17811782178317841785178617871788178917901791179217931794179517961797179817991800211 行未改动全部展开20122013201420152016201720182019202020212022202320242025202689 行未改动211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193## P1177 【模板】排序```cpp#include <bits/stdc++.h>using namespace std;int main() {int n ,a[100010];cin >> n;for (int i = 1;i <= n;++i)cin >> a[i];sort(a+1 ,a+1+n);for (int i = 1;i <= n;++i)cout << a[i] << ' ';}```## P1226 【模板】快速幂```cpp#include<iostream>#define int long longusing namespace std;int Pow(int x,int y,int z){if(y==0)return 1;int res=Pow(x,y/2,z);if(y%2==1)return ((res*res)%z*x)%z;else return res*res%z;}signed main(){int x,y,z;cin>>x>>y>>z;cout<<x<<"^"<<y<<" mod "<<z<<"="<<Pow(x,y,z);return 0;}```## P1495【模板】中国剩余定理(CRT)/ 曹冲养猪88 行未改动## P1886 【模板】单调队列 / 滑动窗口```cpp#include<bits/stdc++.h>using namespace std;int n,k;int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cin>>n>>k;vector<int>a(n+1);for(int i=0;i<n;i++){cin>>a[i];}vector<int>ans,ans1;deque<int>q,q1;for(int i=0;i<n;i++){while(!q.empty()&&q.front()<=i-k){q.pop_front();}while(!q.empty()&&a[q.back()]>=a[i]){q.pop_back();}while(!q1.empty()&&q1.front()<=i-k){q1.pop_front();}while(!q1.empty()&&a[q1.back()]<=a[i]){q1.pop_back();}q.push_back(i);q1.push_back(i);if(i>=k-1){ans.push_back(a[q.front()]);ans1.push_back(a[q1.front()]);}}for(auto i:ans){cout<<i<<' ';}cout<<'\n';for(auto i:ans1){cout<<i<<' ';}return 0;}```55 行未改动for (int i = 1; i <= m; i++) {char op;int x, y;cin >> op >> x >> y;if (op == 'Q') {q[++qcnt] = {x, y, mcnt, qcnt, 0, 0};q[qcnt].blockL = (x - 1) / blockSize;q[qcnt].blockR = (y - 1) / blockSize;29 行未改动}```## P1919 【模板】高精度乘法 / A*B Problem 升级版```cpp#include<bits/stdc++.h>using namespace std;const int N=3e6+10;const double PI=acos(-1.0);struct C{double x,y;C(double a=0,double b=0):x(a),y(b){}C operator+(const C&b)const{return C(x+b.x,y+b.y);}C operator-(const C&b)const{return C(x-b.x,y-b.y);}C operator*(const C&b)const{return C(x*b.x-y*b.y,x*b.y+y*b.x);}};void change(C a[],int n){for(int i=1,j=n/2;i<n-1;i++){if(i<j)swap(a[i],a[j]);int k=n/2;while(j>=k){j-=k;k/=2;}if(j<k)j+=k;}}void fft(C a[],int n,int t){change(a,n);for(int h=2;h<=n;h<<=1){C wn(cos(2*PI/h),sin(t*2*PI/h));for(int j=0;j<n;j+=h){C w(1,0);for(int k=j;k<j+h/2;k++){C u=a[k];C v=w*a[k+h/2];a[k]=u+v;a[k+h/2]=u-v;w=w*wn;}}}if(t==-1){for(int i=0;i<n;i++){a[i].x/=n;}}}string s1,s2;C a[N],b[N];int ans[N];int main(){cin>>s1>>s2;int n1=s1.size(),n2=s2.size();int n=1;while(n<n1*2||n<n2*2){n*=2;}for(int i=0;i<n1;i++){a[i]=C(s1[n1-1-i]-'0',0);}for(int i=n1;i<n;i++){a[i]=C(0,0);}for(int i=0;i<n2;i++){b[i]=C(s2[n2-1-i]-'0',0);}for(int i=n2;i<n;i++){b[i]=C(0,0);}fft(a,n,1);fft(b,n,1);for(int i=0;i<n;i++){a[i]=a[i]*b[i];}fft(a,n,-1);for(int i=0;i<n;i++){ans[i]=(int)(a[i].x+0.5);}for(int i=0;i<n;i++){ans[i+1]+=ans[i]/10;ans[i]%=10;}int h=n1+n2-1;while(h>0&&ans[h]==0){h--;}for(int i=h;i>=0;i--){cout<<ans[i];}cout<<endl;return 0;}```## P2613 【模板】有理数取余```cpp#include <bits/stdc++.h>#define LL long longusing namespace std;const int MOD = 19260817;inline int read() {LL ret = 0 , f = 0;char ch = getchar_unlocked();while (!isdigit(ch)) {f |= ch == '-' ,ch = getchar_unlocked();}while (isdigit(ch)) {ret = ret * 10 + ch - '0' ,ch = getchar_unlocked();ret %= MOD;}return f ? -ret : ret;}LL x, y, a, b;void exgcd(LL a ,LL b) {if (!b) {x = 1 ,y = 0;return;}exgcd(b ,a % b);LL xx = x;x = y;y = xx - a / b * x;}int main() {a = read();b = read();if (!b) {cout << "Angry!\n";return 0;}exgcd(b, MOD);(x %= MOD) += MOD;cout << a * x % MOD << endl;return 0;}```## P2709 【模板】莫队 / 小B的询问107 行未改动全部展开#include <bits/stdc++.h>using namespace std;const int MAXN = 2e5 + 10;int n ,m;int fa[MAXN];//fathervoid init() {for (int i = 1;i <= n;++i) {fa[i] = i;}}int find(int x) {//find x fatherif (fa[x] == x) {return x;}return fa[x] = find(fa[x]);31 行未改动const int N = 5e5 + 10;int lowbit(int x) {return x & -x;}int n ,m ,a[N] ,tr[N] ,diff[N];void update(int x ,int k) {while (x <= N) {tr[x] += k;x += lowbit(x);}11 行未改动cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];update(i ,a[i] - a[i - 1]);}while (m--) {int opt ,x ,y ,z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;update(x ,z);update(y + 1 ,-z);}else {cin >> x;cout << query(x) << '\n';124 行未改动全部展开## P3372 【模板】线段树 1```cpp#include <bits/stdc++.h>using namespace std;const long long MAXN = 5e5 + 10;long long n ,m ,a[MAXN];struct Node {long long p ,v ,lazy;}tr[4*MAXN];void pushup(long long p) {tr[p].v = tr[p*2].v + tr[p*2+1].v;}void pushdown(long long p ,long long l ,long long r) {long long ls = p<<1 ,rs = p<<1|1 ,mid = l+r>>1;tr[ls].v += (mid-l+1) * tr[p].lazy;tr[ls].lazy+=tr[p].lazy;tr[rs].v += (r-mid) * tr[p].lazy;tr[rs].lazy+=tr[p].lazy;tr[p].lazy = 0;return;}void build(long long l ,long long r ,long long p) {if (l == r) {tr[p].v = a[l];return;}long long mid = (l+r) / 2;build(l , mid ,p<<1);build(mid+1 ,r ,p<<1|1);pushup(p);}long long ask(long long l ,long long r ,long long x,long long y ,long long p) {if (r < x || l > y) {return 0;}if (l >= x && r <= y) {return tr[p].v;}pushdown(p ,l ,r);long long mid = (l+r) / 2;return ask(l ,mid ,x ,y ,p<<1) + ask(mid+1 ,r ,x ,y ,p<<1|1);}void add(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += (r-l+1) * v;tr[p].lazy += v;return;}pushdown(p ,l ,r);long long mid = (l + r) / 2;add(l ,mid ,x ,y ,p*2 ,v);add(mid+1 ,r ,x ,y ,p*2+1 ,v);pushup(p);}int main() {ios::sync_with_stdio(false);cin.tie(NULL);cin >> n >> m;for (long long i = 1;i <= n;++i) {cin >> a[i];}build(1 ,n ,1);while (m--) {long long opt ,x ,y ,z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;add(1 ,n ,x ,y ,1 ,z);}else {cin >> x >> y;cout << ask(1 ,n ,x ,y ,1) << '\n';}}return 0;}```## P3373 【模板】线段树 2```cpp#include <bits/stdc++.h>using namespace std;const long long MAXN = 5e5 + 10;long long n ,m ,a[MAXN] ,q;struct Node {long long p ,v ,lazy1 ,lazy2;}tr[4*MAXN];void pushup(long long p) {tr[p].v = tr[p*2].v + tr[p*2+1].v;}void f(long long x ,long long l ,long long r ,long long k1 ,long long k2){tr[x].v = (k1 * tr[x].v + (r-l+1) * k2 % m) % m;(tr[x].lazy1 *= k1) %= m;tr[x].lazy2 = (k2 + k1*tr[x].lazy2) % m;}void pushdown(long long p ,long long l ,long long r) {long long ls = p<<1 ,rs = p<<1|1 ,mid = l+r>>1;f(ls ,l, mid ,tr[p].lazy1 ,tr[p].lazy2);f(rs ,mid+1 ,r ,tr[p].lazy1 ,tr[p].lazy2);tr[p].lazy1 = 1 , tr[p].lazy2 = 0;return;}void build(long long l ,long long r ,long long p) {tr[p].lazy1 = 1;if (l == r) {tr[p].v = a[l] % m;return;}long long mid = (l+r) / 2;build(l , mid ,p<<1);build(mid+1 ,r ,p<<1|1);pushup(p);}long long ask(long long l ,long long r ,long long x,long long y ,long long p) {if (r < x || l > y) {return 0;}if (l >= x && r <= y) {return tr[p].v;}pushdown(p ,l ,r);long long mid = (l+r) / 2;return (ask(l ,mid ,x ,y ,p<<1) + ask(mid+1 ,r ,x ,y ,p<<1|1)) % m;}void add(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += (r-l+1) * v;tr[p].v %= m;tr[p].lazy2 += v;tr[p].lazy2 %= m;return;}pushdown(p ,l ,r);long long mid = (l + r) / 2;add(l ,mid ,x ,y ,p*2 ,v);add(mid+1 ,r ,x ,y ,p*2+1 ,v);pushup(p);}void mul(long long l ,long long r ,long long x,long long y ,long long p ,long long v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {(tr[p].v *= v) %= m;(tr[p].lazy1 *= v) %= m;(tr[p].lazy2 *= v) %= m;return;}pushdown(p ,l ,r);long long mid = (l + r) / 2;mul(l ,mid ,x ,y ,p*2 ,v);mul(mid+1 ,r ,x ,y ,p*2+1 ,v);pushup(p);}int main() {ios::sync_with_stdio(false);cin.tie(NULL);cin >> n >> q >> m;for (long long i = 1;i <= n;++i) {cin >> a[i];}build(1 ,n ,1);while (q--) {long long opt ,x ,y ,z;cin >> opt;z %= m;if (opt == 1) {cin >> x >> y >> z;mul(1 ,n ,x ,y ,1 ,z);}else if (opt == 2){cin >> x >> y >> z;add(1 ,n ,x ,y ,1 ,z);}else {cin >> x >> y;cout << ask(1 ,n ,x ,y ,1) << '\n';}}return 0;}5 行未改动const int N = 5e5 + 10;int lowbit(int x) {return x & -x;}int n ,m ,a[N] ,tr[N];void update(int x ,int k) {while (x <= N) {tr[x] += k;x += lowbit(x);}11 行未改动cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];update(i ,a[i]);}while (m--) {int opt ,x ,y;cin >> opt >> x >> y;if (opt == 1) {update(x ,y);}7 行未改动## P3375 【模板】KMP```cpp#include <bits/stdc++.h>using namespace std;using ULL = unsigned long long;const int MAXN = 1e6 + 10;int n ,m;ULL nxt[MAXN];string s1 ,s2;void build_nxt(string s) {int j = 0 , t = nxt[0] = -1;while (j < m) {if (t < 0 || s[j] == s[t]) {3 行未改动t = nxt[t];}}}void match(string s1 ,string s2) {build_nxt(s2);int i = 0 , j = 0;while (i < n) {if (j == m) {9 行未改动}if (j == m) {cout << i - j + 1 << '\n';}for (int i = 1;i <= m;++i) {cout << nxt[i] << ' ';}}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> s1 >> s2;// s1 = ' ' + s1;// s2 = ' ' + s2;n = s1.size() ,m = s2.size();match(s1 , s2);return 0;}```## P3378【模板】堆```cpp#include <bits/stdc++.h>using namespace std;priority_queue<int,vector<int>,greater<int> >q;int main() {int n;cin >> n;while (n--) {15 行未改动## P3379【模板】最近公共祖先LCA```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;const int LOG = 20;int n, m, s, f[N][LOG], dep[N];vector<int> e[N];void dfs(int u, int fa) {f[u][0] = fa;dep[u] = dep[fa] + 1;for (int i = 1; i < LOG; i++) {f[u][i] = f[f[u][i - 1]][i - 1];}for (auto v : e[u]) {if (v != fa) {dfs(v, u);}}}int lca(int u, int v) {if (dep[u] < dep[v]) swap(u, v);int diff = dep[u] - dep[v];for (int i = 0; i < LOG; i++) {if (diff & (1 << i)) {u = f[u][i];}}if (u == v) return u;for (int i = LOG - 1; i >= 0; i--) {if (f[u][i] != f[v][i]) {u = f[u][i];v = f[v][i];}}return f[u][0];}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> m >> s;dep[0] = 0;for (int i = 1; i < n; i++) {int x, y;cin >> x >> y;e[x].push_back(y);e[y].push_back(x);}dfs(s, 0);while (m--) {int u, v;cin >> u >> v;cout << lca(u, v) << '\n';}return 0;}```## P3644【模板】拓扑排序 / 家谱树```cpp#include <bits/stdc++.h>using namespace std;const int N = 1010, M = 10010;int n, m;struct edge {int to, nxt;};edge e[M];int hd[N], ecnt, ind[N];void add(int u, int v) {e[++ecnt].to = v;e[ecnt].nxt = hd[u];hd[u] = ecnt;}queue<int> q;int main() {cin >> n;for (int i = 1; i <= n; ++i) {int u;while (cin >> u && u) {add(i, u);ind[u]++;}}for (int i = 1; i <= n; ++i) {if (!ind[i]) {q.push(i);}}vector<int> ans;while (!q.empty()) {int u = q.front();q.pop();ans.push_back(u);for (int i = hd[u]; i; i = e[i].nxt) {int v = e[i].to;ind[v]--;if (!ind[v]) q.push(v);}}for (auto it : ans) {cout << it << ' ';}cout << endl;return 0;}```## P3384【模板】重链剖分 / 树链剖分```cpp#include <bits/stdc++.h>using namespace std;163 行未改动全部展开memset(cnt, 0, sizeof cnt);dis[1] = 0;q.push(1);vis[1] = 1;while (!q.empty()) {int u = q.front();q.pop();vis[u] = 0;for (int i = hd[u]; i; i = G[i].nxt) {int v = G[i].to;if (dis[v] > dis[u] + G[i].dis) {dis[v] = dis[u] + G[i].dis;cnt[v] = cnt[u] + 1;if (cnt[v] >= n) return 1;if (!vis[v]) {vis[v] = 1;q.push(v);}}}}return 0;}void solve() {memset(hd, 0, sizeof hd);Gtop = 0;cin >> n >> m;for (int i = 1, u, v, w; i <= m; ++i) {cin >> u >> v >> w;add(u, v, w);if (w >= 0) add(v, u, w);}if (spfa()) cout << "YES\n";else cout << "NO\n";}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {solve();}return 0;}```## P3805【模板】Manacher```cpp#include<bits/stdc++.h>using namespace std;int main(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);string s;cin>>s;string t="!";for(char c:s){t+=c;t+="!";}int n=t.size();vector<int>p(n,0);int cent=0,maxR=0,ans=0;for(int i=0;i<n;i++){if(i<maxR){int mir=2*cent-i;p[i]=min(p[mir],maxR-i);}int L=i-(p[i]+1),R=i+(p[i]+1);while(L>=0&&R<n&&t[L]==t[R]){p[i]++,L--,R++;}if(i+p[i]>maxR){cent=i,maxR=i+p[i];}ans=max(ans,p[i]);}cout<<ans<<endl;return 0;}```## P3807【模板】卢卡斯定理 / Lucas 定理```cpp136 行未改动全部展开}return 0;}```## P3865【模板】ST表&RMQ问题```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5+10;int a[MAXN] ,f[MAXN][25] ,logn[MAXN];int n ,m;class FastIO {private:static const int SIZE = 1 << 20;42 行未改动}~FastIO() {fwrite(out, 1, pp - out, stdout);}}d;void init(){logn[1] = 0;logn[2] = 1;for (int i=3;i<=MAXN;i++) {logn[i] = logn[i / 2] + 1;}}void ST_init() {int k = logn[n];for (int j = 1;j <= k;++j) {for(int i = 1;i <= n - (1 << j) +1 ; ++i) {f[i][j] = max(f[i][j-1] ,f[i + (1 << (j - 1))][j - 1]);}}}int query(int l ,int r) {int k = logn[r-l+1];return max(f[l][k] ,f[r - (1 << k) + 1][k]);}int main(){d.read(n), d.read(m);for (int i = 1;i <= n;++i){d.read(a[i]);f[i][0] = a[i];}init();ST_init();while (m--) {int l ,r;d.read(l) ,d.read(r);d.write(query(l , r));d.write_char('\n');}return 0;}```## P4779 【模板】单源最短路径(标准版)```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int INF = 0x3f3f3f3f3f3f3f3f;int n, m, s;vector<pair<int, int>> G[500010];int dis[500010];bool vis[500010];void Dijkstra() {dis[s] = 0;while (true) {int mn = -1;for (int i = 1; i <= n; ++i) {if (!vis[i] && (mn == -1 || dis[i] < dis[mn])) {mn = i;}}if (mn == -1 || dis[mn] == INF) break;vis[mn] = 1;for (auto v : G[mn]) {int to = v.first;int w = v.second;if (dis[to] > dis[mn] + w) {dis[to] = dis[mn] + w;}}}}signed main() {ios::sync_with_stdio(false);3 行未改动int u, v, w;cin >> u >> v >> w;G[u].push_back({v, w});}memset(dis, 0x3f, sizeof dis);dis[s] = 0;Dijkstra();for (int i = 1; i <= n; ++i) {if (dis[i] == INF) {cout << INT_MAX << ' ';} else {cout << dis[i] << ' ';}}cout << endl;return 0;}```## P5357【模板】AC 自动机57 行未改动for (int i = trie.tot; i >= 0; --i) {cnt[fail[i]] += cnt[i];}}}ac;int n;string s[N];int main(){ios::sync_with_stdio(false);12 行未改动}return 0;}```## P5788【模板】单调栈```cpp#include <bits/stdc++.h>using namespace std;2 行未改动int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n;for (int i = 1;i <= n;++i) {cin >> a[i];}stack<int> st;for (int i = 1;i <= n;++i) {while (!st.empty() && a[st.top()] < a[i]) {ans[st.top()] = i;st.pop();}st.push(i);}for (int i = 1;i <= n;++i) {cout << ans[i] << ' ';}cout << '\n';return 0;211 行未改动全部展开```cpp#include <bits/stdc++.h>using namespace std;inline int read() {int ret = 0 , f = 0;char ch = getchar_unlocked();while (!isdigit(ch)) {f |= ch == '-' ,ch = getchar_unlocked();}while (isdigit(ch)) {ret = ret * 10 + ch - '0' ,ch = getchar_unlocked();}return f ? -ret : ret;}int main() {89 行未改动## P13825【模板】线段树1.5```cpp#include <bits/stdc++.h>using namespace std;const unsigned long long MAXN = 2e5 + 10;unsigned long long n ,m ,tot = 0 ,rt = 0;struct Node {unsigned long long ls , rs ,v ,lazy;}tr[40*MAXN];unsigned long long calc(unsigned long long l ,unsigned long long r) {return (l+r) * (r-l+1) / 2;}void pushup(unsigned long long p) {tr[p].v = tr[tr[p].ls].v + tr[tr[p].rs].v;return;}void pushdown(unsigned long long p ,unsigned long long l ,unsigned long long r) {if (tr[p].lazy== 0) {return;}unsigned long long &ls = tr[p].ls ,&rs = tr[p].rs ,mid = l+r>>1;if (!ls) ls = ++tot;if (!rs) rs = ++tot;tr[ls].v += 1ll * (mid-l+1) * tr[p].lazy;tr[ls].lazy += tr[p].lazy;tr[rs].v += 1ll * (r-mid) * tr[p].lazy;tr[rs].lazy += tr[p].lazy;tr[p].lazy = 0;return;}unsigned long long ask(unsigned long long l ,unsigned long long r ,unsigned long long x,unsigned long long y ,unsigned long long p) {if (l >= x && r <= y) {return tr[p].v;}if (r < x || l > y) {return 0;}pushdown(p ,l ,r);unsigned long long mid = (l+r) / 2;return ask(l ,mid ,x ,y ,tr[p].ls) + ask(mid+1 ,r ,x ,y ,tr[p].rs);}void add(unsigned long long l ,unsigned long long r ,unsigned long long x,unsigned long long y ,unsigned long long v ,unsigned long long &p){if (p == 0) {p = ++tot;}if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += 1ll * (r-l+1) * v;tr[p].lazy += v;return;}pushdown(p ,l ,r);unsigned long long mid = (l + r)>>1;add(l ,mid ,x ,y ,v ,tr[p].ls);add(mid+1 ,r ,x ,y ,v ,tr[p].rs);pushup(p);}int main() {ios::sync_with_stdio(false);cin.tie(NULL);cin >> n >> m;while (m--) {unsigned long long opt ,x ,y ,z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;add(1 ,n ,x ,y ,z ,rt);}else {cin >> x >> y;cout << ask(1 ,n ,x ,y ,rt) + calc(x , y) << '\n';}}return 0;}```123456789101112131415161718192021222324252627282930313233343588 行未改动12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516655 行未改动22222322422522622722822929 行未改动259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377107 行未改动全部展开48548648748848949049149249349449549649749849931 行未改动53153253353453553653753853954011 行未改动552553554555556557558559560561562563564565566567568124 行未改动全部展开6936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758765 行未改动88288388488588688788888989089111 行未改动9039049059069079089099109119129139147 行未改动9229239249259269279289299309319329333 行未改动9379389399409419429439449459 行未改动95595695795895996096196296396496596696796896997097197297397497597697797897998015 行未改动996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092163 行未改动全部展开125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387136 行未改动全部展开1524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155542 行未改动1598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017413 行未改动17451746174717481749175017511752175317541755175657 行未改动18141815181618171818181918201821182212 行未改动1835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118822 行未改动18851886188718881889189018911892189318941895189618971898189919001901190219031904211 行未改动全部展开21162117211821192120212121222123212421252126212721282129213089 行未改动22202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296## P1177 【模板】排序```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n, a[N];int main() {cin >> n;for (int i = 1; i <= n; ++i) cin >> a[i];sort (a + 1, a + 1 + n);for (int i = 1; i <= n; ++i)cout << a[i] << ' ';return 0;}```## P1226 【模板】快速幂```cpp#include <bits/stdc++.h>using namespace std;int qpow (int x, int y, int mod) {long long res = 1, bs = x;while (y) {if (y & 1) (res *= bs) %= mod;(bs *= bs) %= mod;y >>= 1;}return res;}signed main(){int x, y, z;cin >> x >> y >> z;cout << x << "^" << y << " mod " << z << "=" << qpow(x, y, z) << endl;return 0;}```## P1495【模板】中国剩余定理(CRT)/ 曹冲养猪88 行未改动## P1886 【模板】单调队列 / 滑动窗口```cpp#include<bits/stdc++.h>using namespace std;int n, k;int main(){cin >> n >> k;vector<int> a(n + 1);for (int i = 1 ; i <= n; ++i) {cin >> a[i];}vector<int> ans, ans1;deque<int> q, q1;for (int i = 1; i <= n; ++i) {while (!q.empty() && q.front() <= i - k) {q.pop_front();}while (!q.empty() && a[q.back()] >= a[i]) {q.pop_back();}while (!q1.empty() && q1.front() <= i - k) {q1.pop_front();}while (!q1.empty() && a[q1.back()] <= a[i]) {q1.pop_back();}q.push_back(i);q1.push_back(i);if(i >= k){ans.push_back(a[q.front()]);ans1.push_back(a[q1.front()]);}}for (auto i : ans) {cout << i << ' ';}cout << '\n';for (auto i : ans1) {cout << i << ' ';}return 0;}```55 行未改动for (int i = 1; i <= m; i++) {char op;int x, y;cin >> op >> x >> y;if (op == 'Q') {q[++qcnt] = {x, y, mcnt, qcnt, 0, 0};q[qcnt].blockL = (x - 1) / blockSize;q[qcnt].blockR = (y - 1) / blockSize;29 行未改动}```## P1919 【模板】高精度乘法 / A*B Problem 升级版```cpp#include <bits/stdc++.h>using namespace std;const double PI = acos(-1.0);typedef complex<double> Complex;void fft(vector<Complex> &a, bool invert) {int n = a.size();for (int i = 1, j = 0; i < n; i++) {int bit = n >> 1;for (; j & bit; bit >>= 1)j ^= bit;j ^= bit;if (i < j) swap(a[i], a[j]);}for (int len = 2; len <= n; len <<= 1) {double ang = 2 * PI / len * (invert ? -1 : 1);Complex wlen(cos(ang), sin(ang));for (int i = 0; i < n; i += len) {Complex w(1);for (int j = 0; j < len / 2; j++) {Complex u = a[i + j];Complex v = a[i + j + len / 2] * w;a[i + j] = u + v;a[i + j + len / 2] = u - v;w *= wlen;}}}if (invert) {for (Complex &x : a)x /= n;}}vector<int> mult(const vector<int> &a, const vector<int> &b) {int n = 1;int total_len = a.size() + b.size();while (n < total_len) n <<= 1;vector<Complex> fa(a.begin(), a.end()), fb(b.begin(), b.end());fa.resize(n);fb.resize(n);fft(fa, false);fft(fb, false);for (int i = 0; i < n; i++)fa[i] *= fb[i];fft(fa, true);vector<int> result(total_len + 1, 0);for (int i = 0; i < total_len; i++) {result[i] = (int)round(fa[i].real());}int carry = 0;for (int i = 0; i < total_len; i++) {result[i] += carry;carry = result[i] / 10;result[i] %= 10;}while (carry) {result[total_len++] = carry % 10;carry /= 10;}while (result.size() > 1 && result.back() == 0)result.pop_back();return result;}int main() {string s1, s2;cin >> s1 >> s2;vector<int> a(s1.size()), b(s2.size());for (int i = 0; i < s1.size(); i++) a[i] = s1[s1.size() - 1 - i] - '0';for (int i = 0; i < s2.size(); i++) b[i] = s2[s2.size() - 1 - i] - '0';vector<int> res = mult(a, b);for (int i = res.size() - 1; i >= 0; i--)cout << res[i];cout << '\n';return 0;}```## P2613 【模板】有理数取余```cpp#include <bits/stdc++.h>#define LL long longusing namespace std;const int MOD = 19260817;LL a, b;inline LL read() {LL ret = 0 , f = 0;char ch = getchar();while (!isdigit(ch)) {f |= ch == '-' ,ch = getchar();}while (isdigit(ch)) {ret = ret * 10 + ch - '0' ,ch = getchar();ret %= MOD;}return f ? -ret : ret;}LL qpow(LL a, LL b) {LL res = 1, bs = a;while (b) {if (b & 1)(res *= bs) %= MOD;(bs *= bs) %= MOD;b >>= 1;}return res;}int main() {a = read();b = read();if (!b || b % MOD == 0) {cout << "Angry!\n";return 0;}cout << a * qpow(b, MOD - 2) % MOD << endl;return 0;}```## P2709 【模板】莫队 / 小B的询问107 行未改动全部展开#include <bits/stdc++.h>using namespace std;const int MAXN = 2e5 + 10;int n ,m;int fa[MAXN];void init() {for (int i = 1;i <= n;++i) {fa[i] = i;}}int find(int x) {if (fa[x] == x) {return x;}return fa[x] = find(fa[x]);31 行未改动const int N = 5e5 + 10;int lowbit(int x) {return x & -x;}int n, m, a[N], tr[N];void update(int x, int k) {while (x <= N) {tr[x] += k;x += lowbit(x);}11 行未改动cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];update(i, a[i] - a[i - 1]);}while (m--) {int opt, x, y, z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;update(x, z);update(y + 1, -z);}else {cin >> x;cout << query(x) << '\n';124 行未改动全部展开## P3372 【模板】线段树 1```cpp#include <bits/stdc++.h>using namespace std;#define int long longconst int N = 5e5 + 10;int n ,m ,a[N];struct Node {int p ,v ,lazy;}tr[N << 2];void pushup(int p) {tr[p].v = tr[p << 1].v + tr[p << 1 | 1].v;}void pushdown(int p, int l, int r) {int ls = p << 1, rs = p << 1 | 1, mid = l+r >> 1;tr[ls].v += (mid - l + 1) * tr[p].lazy;tr[ls].lazy += tr[p].lazy;tr[rs].v += (r - mid) * tr[p].lazy;tr[rs].lazy += tr[p].lazy;tr[p].lazy = 0;return;}void build(int l, int r, int p) {if (l == r) {tr[p].v = a[l];return;}int mid = l + r >> 1;build(l, mid, p << 1);build(mid + 1, r, p << 1 | 1);pushup(p);}int ask(int l, int r, int x, int y, int p) {if (r < x || l > y) {return 0;}if (l >= x && r <= y) {return tr[p].v;}pushdown(p ,l ,r);int mid = l + r >> 1;return ask(l, mid, x, y, p << 1) + ask(mid + 1, r, x, y, p << 1 | 1);}void add(int l, int r, int x, int y, int p, int v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += (r - l + 1) * v;tr[p].lazy += v;return;}pushdown(p ,l ,r);int mid = l + r >> 1;add(l, mid, x, y, p << 1, v);add(mid + 1, r, x, y, p << 1 | 1, v);pushup(p);}signed main() {cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];}build(1, n, 1);while (m--) {int opt, x, y, z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;add(1, n, x, y, 1, z);}else {cin >> x >> y;cout << ask(1, n, x, y, 1) << '\n';}}return 0;}```## P3373 【模板】线段树 2```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;int n, m, a[N], q;#define int long longstruct Node {int p, v, lazy1, lazy2;} tr[N << 2];void pushup(int p) {tr[p].v = tr[p * 2].v + tr[p * 2 + 1].v;}void mktg(int x, int l, int r, int k1, int k2) {tr[x].v = (k1 * tr[x].v + (r - l + 1) * k2 % m) % m;(tr[x].lazy1 *= k1) %= m;tr[x].lazy2 = (k2 + k1 * tr[x].lazy2) % m;}void pushdown(int p, int l, int r) {int ls = p << 1, rs = p << 1 | 1, mid = l + r >> 1;mktg(ls, l, mid, tr[p].lazy1, tr[p].lazy2);mktg(rs, mid + 1, r, tr[p].lazy1, tr[p].lazy2);tr[p].lazy1 = 1, tr[p].lazy2 = 0;return;}void build(int l, int r, int p) {tr[p].lazy1 = 1;if (l == r) {tr[p].v = a[l] % m;return;}int mid = l + r >> 1;build(l, mid, p << 1);build(mid + 1, r, p << 1 | 1);pushup(p);}int ask(int l, int r, int x, int y, int p) {if (r < x || l > y) {return 0;}if (l >= x && r <= y) {return tr[p].v;}pushdown(p, l, r);int mid = l + r >> 1;return (ask(l, mid, x, y, p << 1) + ask(mid + 1, r, x, y, p << 1 | 1)) % m;}void add(int l, int r, int x, int y, int p, int v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += (r - l + 1) * v;tr[p].v %= m;tr[p].lazy2 += v;tr[p].lazy2 %= m;return;}pushdown(p, l, r);int mid = l + r >> 1;add(l, mid, x, y, p << 1, v);add(mid + 1, r, x, y, p << 1 | 1, v);pushup(p);}void mul(int l, int r, int x, int y, int p, int v) {if (r < x || l > y) {return;}if (l >= x && r <= y) {(tr[p].v *= v) %= m;(tr[p].lazy1 *= v) %= m;(tr[p].lazy2 *= v) %= m;return;}pushdown(p, l, r);int mid = l + r >> 1;mul(l, mid, x, y, p << 1, v);mul(mid + 1, r, x, y, p << 1 | 1, v);pushup(p);}signed main() {cin >> n >> q >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];}build(1, n, 1);while (q--) {int opt, x, y, z;cin >> opt;z %= m;if (opt == 1) {cin >> x >> y >> z;mul(1, n, x, y, 1, z);}else if (opt == 2) {cin >> x >> y >> z;add(1, n, x, y, 1, z);}else {cin >> x >> y;cout << ask(1, n, x, y, 1) << '\n';}}return 0;}5 行未改动const int N = 5e5 + 10;int lowbit(int x) {return x & -x;}int n, m, a[N], tr[N];void update(int x, int k) {while (x <= N) {tr[x] += k;x += lowbit(x);}11 行未改动cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];update(i, a[i]);}while (m--) {int opt, x, y;cin >> opt >> x >> y;if (opt == 1) {update(x ,y);}7 行未改动## P3375 【模板】KMP```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 1e6 + 10;int n ,m;int nxt[MAXN];string s1, s2;void build_nxt(string s) {int j = 0 , t = nxt[0] = -1;while (j < m) {if (t < 0 || s[j] == s[t]) {3 行未改动t = nxt[t];}}}void match(string s1, string s2) {build_nxt(s2);int i = 0 , j = 0;while (i < n) {if (j == m) {9 行未改动}if (j == m) {cout << i - j + 1 << '\n';}for (int i = 1; i <= m; ++i) {cout << nxt[i] << ' ';}}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> s1 >> s2;n = s1.size(), m = s2.size();match(s1 , s2);return 0;}```## P3378【模板】堆```cpp#include <bits/stdc++.h>using namespace std;priority_queue<int, vector<int>, greater<int> >q;int main() {int n;cin >> n;while (n--) {15 行未改动## P3379【模板】最近公共祖先LCA```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;const int LOG = 20;int n, m, s, f[N][LOG], dep[N];vector<int> e[N];void dfs(int u, int fa) {f[u][0] = fa;dep[u] = dep[fa] + 1;for (int i = 1; i < LOG; i++) {f[u][i] = f[f[u][i - 1]][i - 1];}for (auto v : e[u]) {if (v != fa) {dfs(v, u);}}}int lca(int u, int v) {if (dep[u] < dep[v]) swap(u, v);int diff = dep[u] - dep[v];for (int i = 0; i < LOG; i++) {if (diff & (1 << i)) {u = f[u][i];}}if (u == v) return u;for (int i = LOG - 1; i >= 0; i--) {if (f[u][i] != f[v][i]) {u = f[u][i];v = f[v][i];}}return f[u][0];}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> m >> s;dep[0] = 0;for (int i = 1; i < n; i++) {int x, y;cin >> x >> y;e[x].push_back(y);e[y].push_back(x);}dfs(s, 0);while (m--) {int u, v;cin >> u >> v;cout << lca(u, v) << '\n';}return 0;}```## P3383【模板】线性筛素数```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e8 + 10;int n, q;bitset<N> isp;vector<int> prm;void sieve() {isp.set();isp[1] = 0;for (int i = 2; i <= n; ++i) {if (isp[i]) {prm.push_back(i);}for (int p : prm) {if (i * p > n) break;isp[i * p] = 0;if (i % p == 0) break;}}}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> q;sieve();while (q--) {int k;cin >> k;cout << prm[k - 1] << '\n';}return 0;}```## P3384【模板】重链剖分 / 树链剖分```cpp#include <bits/stdc++.h>using namespace std;163 行未改动全部展开memset(cnt, 0, sizeof cnt);dis[1] = 0;q.push(1);vis[1] = 1;while (!q.empty()) {int u = q.front();q.pop();vis[u] = 0;for (int i = hd[u]; i; i = G[i].nxt) {int v = G[i].to;if (dis[v] > dis[u] + G[i].dis) {dis[v] = dis[u] + G[i].dis;cnt[v] = cnt[u] + 1;if (cnt[v] >= n) return 1;if (!vis[v]) {vis[v] = 1;q.push(v);}}}}return 0;}void solve() {memset(hd, 0, sizeof hd);Gtop = 0;cin >> n >> m;for (int i = 1, u, v, w; i <= m; ++i) {cin >> u >> v >> w;add(u, v, w);if (w >= 0) add(v, u, w);}if (spfa()) cout << "YES\n";else cout << "NO\n";}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {solve();}return 0;}```## B3644【模板】拓扑排序 / 家谱树```cpp#include <bits/stdc++.h>using namespace std;const int N = 1010, M = 10010;int n, m;struct edge {int to, nxt;};edge e[M];int hd[N], ecnt, ind[N];void add(int u, int v) {e[++ecnt].to = v;e[ecnt].nxt = hd[u];hd[u] = ecnt;}queue<int> q;int main() {cin >> n;for (int i = 1; i <= n; ++i) {int u;while (cin >> u && u) {add(i, u);ind[u]++;}}for (int i = 1; i <= n; ++i) {if (!ind[i]) {q.push(i);}}vector<int> ans;while (!q.empty()) {int u = q.front();q.pop();ans.push_back(u);for (int i = hd[u]; i; i = e[i].nxt) {int v = e[i].to;ind[v]--;if (!ind[v]) q.push(v);}}for (auto it : ans) {cout << it << ' ';}cout << endl;return 0;}```## P3805【模板】Manacher```cpp#include <bits/stdc++.h>using namespace std;int main() {string s;cin >> s;s = ' ' + s;string t = "!";for (int i = 1; i < s.size(); ++i) {t += s[i];t += "!";}int n = t.size();vector<int> p(n, 0);int cent = 0, maxr = 0, ans = 0;for (int i = 1; i <= n; i++) {if (i < maxr) {int mr = 2 * cent - i;p[i] = min(p[mr], maxr - i);}int L = i - (p[i] + 1), R = i + (p[i] + 1);while (L >= 0 && R < n && t[L] == t[R]) {p[i]++, L--, R++;}if (i + p[i] > maxr) {cent = i, maxr = i + p[i];}ans = max(ans, p[i]);}cout << ans << endl;return 0;}```## P3807【模板】卢卡斯定理 / Lucas 定理```cpp136 行未改动全部展开}return 0;}```## P3811【模板】模意义下的乘法逆元```cpp#include <bits/stdc++.h>using namespace std;#define int long longconst int N = 3e6 + 10;int n, p, inv[N];signed main() {cin >> n >> p;inv[1] = 1;cout << 1 << '\n';for (int i = 2; i <= n; ++i) {inv[i] = p - p / i * inv[p % i] % p;cout << inv[i] << '\n';}return 0;}```## P3865【模板】ST表&RMQ问题```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5+10;int a[N] ,f[N][25] ,logn[N];int n ,m;class FastIO {private:static const int SIZE = 1 << 20;42 行未改动}~FastIO() {fwrite(out, 1, pp - out, stdout);}} d;void init(){logn[1] = 0;logn[2] = 1;for (int i = 3; i <= N; ++i) {logn[i] = logn[i / 2] + 1;}}void ST_init() {int k = logn[n];for (int j = 1; j <= k; ++j) {for(int i = 1; i <= n - (1 << j) + 1; ++i) {f[i][j] = max(f[i][j - 1] ,f[i + (1 << (j - 1))][j - 1]);}}}int query(int l, int r) {int k = logn[r - l + 1];return max(f[l][k], f[r - (1 << k) + 1][k]);}int main(){d.read(n), d.read(m);for (int i = 1; i <= n; ++i){d.read(a[i]);f[i][0] = a[i];}init();ST_init();while (m--) {int l, r;d.read(l) ,d.read(r);d.write(query(l, r));d.write_char('\n');}return 0;}```## P3919 【模板】可持久化线段树 1(可持久化数组)```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int n, m, tot = 0;int a[N], Rt[N];struct Node {int l, r, val;};Node tr[N << 5];int build(int l, int r) {int pos = ++tot;if (l == r) {tr[pos].val = a[l];return pos;}int mid = l + r >> 1;tr[pos].l = build(l, mid);tr[pos].r = build(mid + 1, r);return pos;}int update(int l, int r, int pos, int new_pos, int new_val) {tr[++tot] = tr[pos];pos = tot;if (l == r) {tr[pos].val = new_val;}else {int mid = l + r >> 1;if (new_pos <= mid) tr[pos].l = update(l, mid, tr[pos].l, new_pos, new_val);else tr[pos].r = update(mid + 1, r, tr[pos].r, new_pos, new_val);}return pos;}int query(int l, int r, int pos, int qpos) {if (l == r) {return tr[pos].val;}int mid = l + r >> 1;if (qpos <= mid) return query(l, mid, tr[pos].l, qpos);else return query(mid + 1, r, tr[pos].r, qpos);}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i];}Rt[0] = build(1, n);for (int i = 1; i <= m; ++i) {int v, opt, p, c;cin >> v >> opt;if (opt == 1) {cin >> p >> c;Rt[i] = update(1, n, Rt[v], p, c);}else {cin >> p;cout << query(1, n, Rt[v], p) << endl;Rt[i] = Rt[v];}}return 0;}```## P4779 【模板】单源最短路径(标准版)```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 2e5 + 10;int n, m, s, dis[N];bool vis[N];vector<pair<int, int>> G[N];struct Node {int v, w;bool operator < (const Node& p) const {return w > p.w;}};priority_queue<Node> q;void Dijkstra() {memset(dis, 0x3f, sizeof dis);dis[s] = 0;q.push({s, 0});while (!q.empty()) {int u = q.top().v;int d = q.top().w;q.pop();if (vis[u]) continue;vis[u] = 1;for (auto [v, w] : G[u]) {if (dis[v] > dis[u] + w) {dis[v] = dis[u] + w;q.push({v, dis[v]});}}}}signed main() {ios::sync_with_stdio(false);3 行未改动int u, v, w;cin >> u >> v >> w;G[u].push_back({v, w});}Dijkstra();for (int i = 1; i <= n; ++i) {cout << dis[i] << ' ';}return 0;}```## P5357【模板】AC 自动机57 行未改动for (int i = trie.tot; i >= 0; --i) {cnt[fail[i]] += cnt[i];}}} ac;int n;string s[N];int main(){ios::sync_with_stdio(false);12 行未改动}return 0;}```## P5431【模板】模意义下的乘法逆元 2```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 5e6 + 10;int n, p, k, a[N];int pre[N], dpre[N], ans;int qpow (int x, int y, int mod) {long long res = 1, bs = x;while (y) {if (y & 1) (res *= bs) %= mod;(bs *= bs) %= mod;y >>= 1;}return res;}signed main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> p >> k;pre[0] = 1;for (int i = 1; i <= n; ++i) {cin >> a[i];pre[i] = pre[i - 1] * a[i] % p;}dpre[n] = qpow(pre[n], p - 2, p);for (int i = n; i >= 1; --i) {dpre[i - 1] = dpre[i] * a[i] % p;}int kk = k;for (int i = 1; i <= n; ++i) {int t = pre[i - 1] * dpre[i] % p;ans = (ans + t * kk) % p;(kk *= k) %= p;}cout << ans << endl;return 0;}```## P5788【模板】单调栈```cpp#include <bits/stdc++.h>using namespace std;2 行未改动int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n;for (int i = 1; i <= n; ++i) {cin >> a[i];}stack<int> st;for (int i = 1; i <= n; ++i) {while (!st.empty() && a[st.top()] < a[i]) {ans[st.top()] = i;st.pop();}st.push(i);}for (int i = 1; i <= n; ++i) {cout << ans[i] << ' ';}cout << '\n';return 0;211 行未改动全部展开```cpp#include <bits/stdc++.h>using namespace std;inline int read() {int ret = 0, f = 0;char ch = getchar_unlocked();while (!isdigit(ch)) {f |= ch == '-', ch = getchar_unlocked();}while (isdigit(ch)) {ret = ret * 10 + ch - '0', ch = getchar_unlocked();}return f ? -ret : ret;}int main() {89 行未改动## P13825【模板】线段树1.5```cpp#include <bits/stdc++.h>using namespace std;#define int unsigned long longconst int N = 2e5 + 10;int n, m, tot = 0, rt = 0;struct Node {int ls, rs, v, lazy;} tr[N << 5];int calc(int l, int r) {return (l + r) * (r - l + 1) / 2;}void pushup(int p) {tr[p].v = tr[tr[p].ls].v + tr[tr[p].rs].v;return;}void pushdown(int p, int l, int r) {if (tr[p].lazy == 0) {return;}int &ls = tr[p].ls, &rs = tr[p].rs, mid = l + r >> 1;if (!ls) ls = ++tot;if (!rs) rs = ++tot;tr[ls].v += 1ll * (mid - l + 1) * tr[p].lazy;tr[ls].lazy += tr[p].lazy;tr[rs].v += 1ll * (r - mid) * tr[p].lazy;tr[rs].lazy += tr[p].lazy;tr[p].lazy = 0;return;}int ask(int l, int r, int x, int y, int p) {if (l >= x && r <= y) {return tr[p].v;}if (r < x || l > y) {return 0;}pushdown(p, l, r);int mid = (l + r) / 2;return ask(l, mid, x, y, tr[p].ls) + ask(mid + 1, r, x, y, tr[p].rs);}void add(int l, int r, int x, int y, int v, int &p) {if (p == 0) {p = ++tot;}if (r < x || l > y) {return;}if (l >= x && r <= y) {tr[p].v += 1ll * (r - l + 1) * v;tr[p].lazy += v;return;}pushdown(p, l, r);int mid = (l + r) >> 1;add(l, mid, x, y, v, tr[p].ls);add(mid + 1, r, x, y, v, tr[p].rs);pushup(p);}signed main() {cin >> n >> m;while (m--) {int opt, x, y, z;cin >> opt;if (opt == 1) {cin >> x >> y >> z;add(1, n, x, y, z, rt);}else {cin >> x >> y;cout << ask(1, n, x, y, rt) + calc(x, y) << '\n';}}return 0;}```