正文
增加 1258 行删除 3 行
1234440 行未改动全部展开445446447448449450451452453101 行未改动全部展开555556557558559560561562563217 行未改动全部展开78178278378478578678778878939 行未改动82983083183283383483583683790 行未改动92892993093193293393493570 行未改动1006100710081009# 各种模版:[一些模版题](https://blog.csdn.net/u011815404/article/details/79324028)440 行未改动全部展开}```### (2)迭代fft```cpp#include<bits/stdc++.h>#define ll long long//十年oi一场空,不开ll……101 行未改动全部展开}return 0;}```## 四:数学类### 1:[模意义下的乘法逆元](https://www.luogu.com.cn/problem/P3811)217 行未改动全部展开from yanyu289;}```#### (2)[有理数取余](https://www.luogu.com.cn/problem/P2613)```cpp#include<bits/stdc++.h>#define int long long#define ll long long39 行未改动from yanyu289;}```### 4[扩展欧拉定理](https://www.luogu.com.cn/problem/P5091)```cpp#include<bits/stdc++.h>#define int long long90 行未改动from yanyu289;}```## 五:线段树### 1:[区间加减区间查,线段树1](https://www.luogu.com.cn/problem/P3372)70 行未改动}from yanyu289;}```1234567891011121314151617181920212223242526272829303132333435440 行未改动全部展开476477478479480481482483484101 行未改动全部展开586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654217 行未改动全部展开87287387487587687787887988039 行未改动92092192292392492592692792890 行未改动1019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144370 行未改动1514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264## 普通模板```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define ull unsigned long long#define double long double#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1#define pii pair<int, int>using namespace std;const int maxn = 2e5 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int n, m;int a[maxn];signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);from yanyu289;}```# 各种模版:[一些模版题](https://blog.csdn.net/u011815404/article/details/79324028)440 行未改动全部展开}```### 2:迭代fft```cpp#include<bits/stdc++.h>#define ll long long//十年oi一场空,不开ll……101 行未改动全部展开}return 0;}```### 2:背包#### (1)[采药1(有限个数)](https://www.luogu.com.cn/problem/P1048)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1000 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int n, m;int v[maxn], w[maxn], f[maxn][maxn];signed main(){cin >> n >> m;//n=T,m=Mfor(int i = 1;i <= m;i ++){cin >> w[i] >> v[i];}for(int i = 1;i <= m;i ++){for(int j = 1;j <= n;j ++){if(j >= w[i]) f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);else f[i][j] = f[i - 1][j];}}cout << f[m][n];from yanyu289;}```#### (2) [采药2(无限个数)](https://www.luogu.com.cn/problem/P1616)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e4 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int t, m, v[maxn], w[10000005];int f[10000005];signed main(){cin >> t >> m;for(int i = 1;i <= m;i ++){cin >> w[i] >> v[i];}for(int i = 1;i <= m;i ++){for(int j = 1;j <= t;j ++){if(j >= w[i]) f[j] = max(f[j], f[j - w[i]] + v[i]);}}cout << f[t];from yanyu289;}```## 四:数学类### 1:[模意义下的乘法逆元](https://www.luogu.com.cn/problem/P3811)217 行未改动全部展开from yanyu289;}```#### (2)[有理数取余](https://www.luogu.com.cn/problem/P2613)```cpp#include<bits/stdc++.h>#define int long long#define ll long long39 行未改动from yanyu289;}```### 4:[扩展欧拉定理](https://www.luogu.com.cn/problem/P5091)```cpp#include<bits/stdc++.h>#define int long long90 行未改动from yanyu289;}```### 6(1):[高斯消元](https://www.luogu.com.cn/problem/P3389)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1005, mod = 998244353, inf = 1e9;const double pi = acos(-1);double a[maxn][maxn];double ans[maxn];double eps = 1e-7;int n;bool gauss(){for(int i = 1,r = 1;i <= n;i ++){r = i;for(int j = i + 1;j <= n;j ++)if(fabs(a[r][i]) < fabs(a[j][i])) r = j;if(fabs(a[r][i]) < eps){return false;}if(i != r)swap(a[i],a[r]);double t = a[i][i];for(int j = i;j <= n + 1;j ++)a[i][j] /= t;for(int j = i + 1;j <= n;j ++){t = a[j][i];for(int k = i;k <= n + 1;k ++) a[j][k] -= a[i][k] * t;}}ans[n] = a[n][n + 1];for(int i = n - 1;i >= 1;i --){ans[i] = a[i][n + 1];for(int j = i + 1;j <= n;j ++)ans[i] -= (a[i][j] * ans[j]);}return true;}signed main(){cin >> n;for(int i = 1;i <= n;i ++)for(int j = 1;j <= n + 1;j ++)cin >> a[i][j];if(!gauss()) puts("No Solution");else{for(int i = 1;i <= n;i ++)cout << fixed << setprecision(2) << ans[i] << endl;}from yanyu289;}```### 6(2):[线性方程组](https://www.luogu.com.cn/problem/P2455)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0using namespace std;const int maxn = 1005;const double eps = 1e-8;int n;double a[maxn][maxn];int gauss(){int c,r;for (c = 0, r = 0;c < n;c ++){int t = r;for (int i = r; i < n; i++)if (fabs(a[i][c]) > fabs(a[t][c]))t = i;if (fabs(a[t][c]) < eps) continue;for (int i = c; i < n + 1; i++) swap(a[t][i], a[r][i]);for (int i = n; i >= c; i--) a[r][i] /= a[r][c];for (int i = r + 1; i < n; i++)if (fabs(a[i][c]) > eps)for (int j = n; j >= c; j--)a[i][j] -= a[r][j] * a[i][c];r ++;}if (r < n){for (int i = r; i < n; i++)if (fabs(a[i][n]) > eps)return 2;return 1;}for (int i = n - 1; i >= 0; i--)for (int j = i + 1; j < n; j++)a[i][n] -= a[j][n] * a[i][j];return 0;}signed main(){cin >> n;for (int i = 0;i < n;i ++)for (int j = 0; j < n + 1; j++)cin >> a[i][j];int flag = gauss();if (flag == 0){for (int i = 0; i < n; i++){cout << "x" << i + 1 << "=";cout << fixed << setprecision(2) << a[i][n] << endl;}}else if (flag == 1) puts("0");else puts("-1");from yanyu289;}```### 6(3):[线性基(高斯消元法求)](https://www.luogu.com.cn/problem/P3812)```#include<bits/stdc++.h>#define int long long#define ll long long#define in(x) scanf("%lld", &x)#define out(x) printf("%lld ", x)#define outs(x) printf("%lld\n", x)#define from return#define yanyu289 0using namespace std;const int maxn = 1e5 + 55, mod = 1e9 + 7;int T;int n, k = 1, a[maxn], ans;int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void gauss_linebas(){//高斯消元法做线形基://可保证基最高位的1不重复,排列从大到小//用于求解异或和相关问题(eg:最大/小第k大/小)//注:线形基:三维空间立体的基向量在线性空间中的推广for(int i = 62;i >= 0;i --){//高位1的数换上去for(int j = k;j <= n;j ++){if((a[j] >> i) & 1){swap(a[j], a[k]);break;}}if(((a[k] >> i) & 1) == 0) continue;//第i位所有向量都为0for(int j = 1;j <= n;j ++){if(j != k && ((a[j] >> i) & 1)){a[j] ^= a[k];//其他数的i位消为0}}k ++;//基的个数if(k == n + 1) break;}}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];}gauss_linebas();for(int i = 1;i <= k;i ++){ans ^= a[i];}cout << ans;from yanyu289;}```### 7:[矩阵快速幂](https://www.luogu.com.cn/problem/P3390)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e6 + 55, mod = 1e9 + 7, inf = 1e9;const double pi = acos(-1);int T;ll n, k;struct matrix{ll c[105][105];matrix(){memset(c, 0, sizeof c);}}a, res;matrix operator * (matrix &x, matrix &y){matrix t;for(int qw = 1; qw <= n; qw ++){for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){t.c[i][j] = (t.c[i][j] % mod + x.c[i][qw] % mod * y.c[qw][j] % mod) % mod;t.c[i][j] %= mod;}}}from t;}void qpow(ll k){for(int i = 1; i <= n; i ++){res.c[i][i] = 1;}while(k){if(k & 1)res = res * a;a = a * a;k >>= 1;}}signed main(){cin >> n >> k;for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){cin >> a.c[i][j];a.c[i][j] %= mod;}}qpow(k);for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){cout << res.c[i][j] % mod << " ";}cout << endl;}from yanyu289;}```### 8:欧拉函数```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 2e5 + 55, inf = 1e18, mod = 1e9 + 7;const double pi = acos(-1);int T;int n, m;int phi[maxn];int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void init(int n){for(int i = 0;i <= n;i ++){phi[i] = i;}for(int i = 2;i <= n;i ++){if(phi[i] == i){for(int j = i;j <= n;j += i){phi[j] = phi[j] / i * (i - 1);}}}}int get_phi(int x) {int res = x;for (int i = 2; i * i <= x; i++) {if (x % i == 0) {res = res / i * (i - 1);while (x % i == 0) x /= i;}}if (x > 1) res = res / x * (x - 1);return res;}int query(int n){int res = 0;for (int d = 1; d * d <= n;d ++) {if(n % d == 0){res += d * get_phi(n / d);if (d * d != n){res += (n / d) * get_phi(d);}}}return res;}signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);from yanyu289;}```### 9:[线性筛](https://www.luogu.com.cn/problem/P3383)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int prime[maxn];bool used[maxn];int cnt, n, q, k;void query(){memset(used, 0 ,sizeof(used));used[1] = 1;for(int i = 2;i <= n;i ++){if(!used[i]) prime[++ cnt] = i;for(int j = 1;j <= cnt && i * prime[j] <= n;j ++){used[i * prime[j]] = 1;if(i % prime[j] == 0) break;}}}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> q;query();while(q --){cin >> k;cout << prime[k] << endl;}from yanyu289;}```### 10:组合数预处理模板```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0using namespace std;const int maxn = 2e6 + 55, mod = 998244353;const double pie = acos(-1);int fra[maxn], inv[maxn];int n, simple;int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void init() {fra[0] = 1;for(int i = 1; i <= n;i ++){fra[i] = fra[i - 1] * i % mod;}inv[n - 1] = qpow(fra[n - 1], mod - 2);for (int i = n - 2; i >= 0;i --) {inv[i] = inv[i + 1] * (i + 1) % mod;}}int c(int n, int k) {if (k < 0 || k > n) return 0;if(k == n) return 1;return fra[n] * inv[k] % mod * inv[n - k] % mod;}signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);init();from yanyu289;}```### 11:莫比乌斯函数u预处理```#include<bits/stdc++.h>#define ll long long#define int long longusing namespace std;const int maxn = 50005;int a,b,c,d,k,miu[maxn],cnt,ans;signed used[maxn];int prime[maxn],sum[maxn];void found_miu(){miu[1] = 1;for(int i = 2;i < maxn - 3;i ++){if(!used[i])prime[++cnt] = i,miu[i] = -1;for(int j = 1; prime[j] * i <= maxn - 3;j ++){used[prime[j] * i] = 1;if(i % prime[j] == 0){break;}else miu[prime[j] * i] = - miu[i];}}for(int i = 1;i <= maxn - 3;i ++){sum[i] = sum[i - 1] + miu[i];}}signed main() {found_miu();return 0;}```## 五:线段树### 1:[区间加减区间查,线段树1](https://www.luogu.com.cn/problem/P3372)70 行未改动}from yanyu289;}```### 2:[区间加/乘,线段树2](https://www.luogu.com.cn/problem/P3373)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) (x << 1) + 1using namespace std;const int maxn = 1e5 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);struct tree{int l, r;int c;}tr[maxn << 2];int n, q, m, arr[maxn], lazytagjia[maxn << 2], lazytagch[maxn << 2];void pushup(int u){tr[u].c = (tr[ls(u)].c + tr[rs(u)].c) % m;}void change(int u){int fa = u >> 1;tr[u].c = (tr[u].c * lazytagch[fa] % m + lazytagjia[fa] * (tr[u].r - tr[u].l + 1) % m) % m;lazytagch[u] = lazytagch[fa] * lazytagch[u] % m;lazytagjia[u] = (lazytagjia[u] * lazytagch[fa] % m + lazytagjia[fa]) % m;}void pushdown(int u){change(ls(u));change(rs(u));lazytagjia[u] = 0;lazytagch[u] = 1;}void build(int u, int l, int r){lazytagch[u] = 1;tr[u].l = l;tr[u].r = r;if(l == r){tr[u].c = arr[l];return;}int mid = (l + r) >> 1;build(ls(u), l, mid);build(rs(u), mid + 1, r);pushup(u);}void update(int u, int l, int r, int op, int k){if(l <= tr[u].l && tr[u].r <= r){if(op == 1){tr[u].c = tr[u].c * k % m;lazytagch[u] = lazytagch[u] * k % m;lazytagjia[u] = lazytagjia[u] * k % m;}else{tr[u].c = (tr[u].c + k * (tr[u].r - tr[u].l + 1) % m) % m;lazytagjia[u] = (lazytagjia[u] + k) % m;}return;}pushdown(u);if(tr[ls(u)].r >= l) update(ls(u), l, r, op, k);if(tr[rs(u)].l <= r) update(rs(u), l, r, op, k);pushup(u);}int query(int u, int l, int r){if(l <= tr[u].l && tr[u].r <= r) return tr[u].c;pushdown(u);int ans = 0;if(tr[ls(u)].r >= l) ans = (ans + query(ls(u), l, r)) % m;if(tr[rs(u)].l <= r) ans = (ans + query(rs(u), l, r)) % m;return ans;}signed main(){cin >> n >> q >> m;for(int i = 1; i <= n; i ++) cin >> arr[i];build(1, 1, n);for(int i = 1; i <= q; i ++){int op, a, b, k;cin >> op >> a >> b;if(op <= 2){cin >> k;update(1, a, b, op, k);}else cout << query(1, a, b) << endl;}from yanyu289;}```### 3:[单点修改,维护区间个数,字符类,线段树](https://www.luogu.com.cn/problem/AT_abc157_e)```cpp#include<bits/stdc++.h>#define ll long long#define ls(x) x << 1#define rs(x) x << 1 | 1#define mid (l + r >> 1)#define endl "\n"using namespace std;const int maxn = 5e5 + 55, mod = 998244353;const double pi = acos(-1);int T;int n, m, q;char s[maxn];bool t[30];struct tree{int l, r;char c;int cnt[30];}tr[maxn << 2];void pushup(int u){for(int i = 0;i < 26;i ++){tr[u].cnt[i] = tr[ls(u)].cnt[i] + tr[rs(u)].cnt[i];}}void build(int u, int l, int r){tr[u].l = l, tr[u].r = r;if(l == r){tr[u].c = s[l];tr[u].cnt[s[l] - 'a'] ++;return;}build(ls(u), l, mid);build(rs(u), mid + 1, r);pushup(u);}void update(int u, int x, int l, int r, char ch){if(l == r){tr[u].cnt[ch - 'a'] ++;tr[u].cnt[tr[u].c - 'a'] --;tr[u].c = ch;return;}if(x <= mid) update(ls(u), x, l, mid, ch);else update(rs(u), x, mid + 1, r, ch);pushup(u);}int query(int u, int l, int r, int lx, int rx){int ans = 0;if (tr[u].r < lx || tr[u].l > rx) return 0;if(tr[u].l >= lx && tr[u].r <= rx){for (int i = 0;i < 26;i ++){if(tr[u].cnt[i] && !t[i]){t[i] = 1;ans ++;}}return ans;}ans += query(ls(u), l, mid, lx, rx);ans += query(rs(u), mid + 1, r, lx, rx);return ans;}int main(){cin >> n;cin >> s + 1;cin >> q;build(1, 1, n);while(q --){memset(t, 0, sizeof t);int op, l, r, u;char c;cin >> op;if(op == 1){cin >> u >> c;update(1, u, 1, n, c);}else{cin >> l >> r;cout << query(1, 1, n, l, r) << endl;}}return 0;}```### 4:[小综合-模板完整体](https://www.luogu.com.cn/problem/P1471)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define double long double#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1using namespace std;const int maxn = 2e5 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;struct tree{int l, r;double add_sum, squ_sum;}tr[maxn << 2];int n, q;int op, a, b;double k, arr[maxn << 2], lazytag[maxn << 2];void pushup(int u){tr[u].squ_sum = tr[ls(u)].squ_sum + tr[rs(u)].squ_sum;tr[u].add_sum = tr[ls(u)].add_sum + tr[rs(u)].add_sum;}void pushdown(int u, int l, int r){if (!lazytag[u]) return;lazytag[ls(u)] += lazytag[u];lazytag[rs(u)] += lazytag[u];double k = lazytag[(u)];int mid = (l + r) >> 1;tr[ls(u)].squ_sum += 2.0 * k * tr[ls(u)].add_sum + (double)(tr[ls(u)].r - tr[ls(u)].l + 1) * k * k;tr[rs(u)].squ_sum += 2.0 * k * tr[rs(u)].add_sum + (double)(tr[rs(u)].r - tr[rs(u)].l + 1) * k * k;k = lazytag[(u)];tr[ls(u)].add_sum += (double)(tr[ls(u)].r - tr[ls(u)].l + 1) * k;tr[rs(u)].add_sum += (double)(tr[rs(u)].r - tr[rs(u)].l + 1) * k;}void build(int u, int l, int r){tr[u].l = l;tr[u].r = r;lazytag[u] = 0.0;if(l == r){tr[u].squ_sum = arr[l] * arr[l];tr[u].add_sum = arr[l];return;}int mid = (l + r) >> 1;build(ls(u), l, mid);build(rs(u), mid + 1, r);pushup(u);}void update(int u, int l, int r, int lc, int rc, double k){if(l > rc || r < lc) return;if(l >= lc && r <= rc){lazytag[u] += k;tr[u].squ_sum += 2.0 * k * tr[u].add_sum + (double)(r - l + 1) * k * k;tr[u].add_sum += (double)(r - l + 1) * k;return;}pushdown(u, l, r);int mid = (l + r) >> 1;if(tr[ls(u)].r >= l) update(ls(u), l, mid, lc, rc, k);if(tr[rs(u)].l <= r) update(rs(u), mid + 1, r, lc, rc, k);pushup(u);}double query1(int u, int l, int r, int lc, int rc){//均if(r < lc || l > rc) return 0;if(l >= lc && r <= rc) return tr[u].add_sum;pushdown(u, l, r);double ans = 0;int mid = (l + r) >> 1;if(lc <= mid) ans += query1(ls(u), l, mid, lc, rc);if(rc > mid) ans += query1(rs(u), mid + 1, r, lc, rc);pushup(u);return ans;}double query2(int u, int l, int r, int lc, int rc){//方差if(r < lc || l > rc) return 0;if(l >= lc && r <= rc) return tr[u].squ_sum;pushdown(u, l, r);double ans = 0;int mid = (l + r) >> 1;if(lc <= mid) ans += query2(ls(u), l, mid, lc, rc);if(rc > mid) ans += query2(rs(u), mid + 1, r, lc, rc);pushup(u);return ans;}signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n;cin >> q;for(int i = 1; i <= n; i ++) cin >> arr[i];build(1, 1, n);for(int i = 1; i <= q; i ++){cin >> op >> a >> b;if(op == 1){cin >> k;update(1, 1, n, a, b, k);}double aver = query1(1, 1, n, a, b) / (b - a + 1.0);if(op == 2){cout << fixed << setprecision(4) << aver << endl;}if(op == 3){cout << fixed << setprecision(4) << (query2(1, 1, n, a, b)) / (b - a + 1.0)- aver * aver << endl;}}from yanyu289;}```## 六:字符串类### 1:[字典树Trie](https://www.luogu.com.cn/problem/P8306)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 3e6 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;char s[maxn];int t,n,m,q;int ch[maxn][64], cnt[maxn], pos[maxn], idx;int ctoi(char c){int x;if(c >= 'A'&&c <= 'Z')x = c - 'A';if(c >= 'a'&&c <= 'z')x = c - 'a' + 26;if(c >= '0'&&c <= '9')x = c - '0' + 52;return x;}void insert(char *s){int u = 0;for(int i = 0;s[i];i ++) {int j = ctoi(s[i]);if(!ch[u][j]) ch[u][j] = ++idx;u = ch[u][j];cnt[u]++;}}int query(char *s){int u = 0;for(int i = 0;s[i];i ++) {int j = ctoi(s[i]);if(!ch[u][j]) return 0;u = ch[u][j];}return cnt[u];}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> t;for(int i = 1;i <= t;i ++){cin >> n >> m;for(int j = 0;j <= idx;j ++){cnt[j] = 0;for(int k = 0;k <= 100;k ++){ch[j][k] = 0;}}idx = 0;for(int j = 1;j <= n;j ++){cin >> s;insert(s);}for(int j = 1;j<= m;j ++){cin >> s;cout << query(s) << endl;}}from yanyu289;}```### 2:[KMP](https://www.luogu.com.cn/problem/P3375)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;char s1[maxn], s2[maxn];int n, m, nex[maxn], cnt;void kmp(){cnt = 0;for(int i = 2;i <= m;i ++){while(cnt != 0 && s2[i] != s2[cnt + 1]){cnt = nex[cnt];}if(s2[i] == s2[cnt + 1])cnt ++;nex[i] = cnt;}}void rekmp(){cnt = 0;for(int i = 1;i <= n;i ++){while(cnt && s1[i] != s2[cnt + 1]){cnt = nex[cnt];}if(s1[i] == s2[cnt + 1])cnt ++;if(cnt == m){cout << i - m + 1 << endl;cnt = nex[cnt];}}}signed main(){cin >> s1 + 1 >> s2 + 1;n = strlen(s1 + 1);m = strlen(s2 + 1);kmp();rekmp();for(int i = 1;i <= m;i ++){cout << nex[i] << " ";}from yanyu289;}```## 七:Tarjan### 1:[缩点](https://www.luogu.com.cn/problem/P3387)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 10005, mod = 998244353, inf = 1e9;const double pi = acos(-1);int n, m, x, y;vector<int> e[maxn], ne[maxn];int dfn[maxn], low[maxn], scc[maxn], ans, cnt;int st[maxn], inst[maxn], top, val[maxn];int nv[maxn], dp[maxn], vis[maxn];priority_queue<pair<int, int> > q;void tar(int u) {dfn[u] = low[u] = ++cnt;st[++top] = u;inst[u] = 1;for(auto v : e[u]){if(!dfn[v]){tar(v);low[u] = min(low[u], low[v]);}else if(inst[v] == 1){low[u] = min(low[u], dfn[v]);}}if(dfn[u] == low[u]){int y;++ ans;do {y = st[top--];inst[y] = 0;scc[y] = ans;} while(u != y);}}signed main(){cin >> n >> m;for(int i = 1; i <= n; i++){cin >> val[i];}for(int i = 1; i <= m; i++){cin >> x >> y;e[x].push_back(y);}for(int i = 1; i <= n; i++){if(!dfn[i]) {tar(i);}}for(int i = 1; i <= n; i++){nv[scc[i]] += val[i];for(int y : e[i]) {int a = scc[i], b = scc[y];if(a != b) {ne[a].push_back(b);}}}for(int i = ans;i; i--){if(dp[i] == 0) {dp[i] = nv[i];}for(int x : ne[i]) {dp[x] = max(dp[x], dp[i] + nv[x]);}}cnt = -1;for(int i = 0; i <= ans; i++) {cnt = max(cnt, dp[i]);}cout << cnt;from yanyu289;}```### 2:## 其他### 1:[快速幂](https://www.luogu.com.cn/problem/P1226)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 10005, mod = 998244353, inf = 1e9;const double pi = acos(-1);int a, b, p;int qpow(int a, int b, int p){int res = 1;while(b){if(b & 1) res = res * a % p;a = a * a % p;b >>= 1;}return res;}signed main(){cin >> a >> b >> p;cout << a << "^" << b << " mod " << p << "=" << qpow(a, b, p);from yanyu289;}```### 2:[并查集](https://www.luogu.com.cn/problem/P3367)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int n, m;int x, y, z;int f[maxn];int found(int o){if(f[o] == o){return f[o];}return f[o] = found(f[o]);}signed main(){cin >> n >> m;for(int i = 1; i <= n; i ++){f[i] = i;}for(int i = 1; i <= m; i ++){cin >> z >> x >> y;int a = found(x), b = found(y);if(z == 1){f[a] = b;} else if(z == 2){if(a == b){cout << "Y" << endl;} else {cout << "N" << endl;}}}from yanyu289;}```### 3:[快读快输](https://www.luogu.com.cn/problem/P10815)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int n, ans, a[maxn];int read(){int x = 0, f = 1;char ch = getchar_unlocked();while (!isdigit(ch)) {if (ch == '-') f *= -1;ch = getchar_unlocked();}while (isdigit(ch)) {x = (x << 3) + (x << 1) + (ch ^ 48);ch = getchar_unlocked();// 一般就写getchar()就可以了}return f * x;}void write(int x){if(x < 0) putchar('-'), x = -x;if(x < 10) putchar(x + '0');else write(x / 10), putchar(x % 10 + '0');}signed main(){n = read();for(int i = 1;i <= n;i ++){a[i] = read();ans += a[i];}write(ans);from yanyu289;}```### 4:[线性筛质数](https://www.luogu.com.cn/problem/P3383)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int prime[maxn];bool used[maxn];int cnt, n, q, k;void query(){memset(used, 0 ,sizeof(used));used[1] = 1;for(int i = 2;i <= n;i ++){if(!used[i]) prime[++ cnt] = i;for(int j = 1;j <= cnt && i * prime[j] <= n;j ++){used[i * prime[j]] = 1;if(i % prime[j] == 0) break;}}}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> q;query();while(q --){cin >> k;cout << prime[k] << endl;}from yanyu289;}```### 5:压位读入(高精度的优化)```cppstruct edge{int a[maxn], n; // a数字,n块数} arr, tem, t, two, five, ans, pow5[150], pow2[150], f[150][150];inline void clearout(edge &x) {memset(x.a, 0, sizeof(x.a));x.n = 1;}inline void cut0(edge &x) {while(x.n > 1 && !x.a[x.n]) --x.n;}inline void inread(edge &x) {//压位读入memset(s, 0, sizeof(s));clearout(x);int cnt = 0;char c = getchar();while(!isdigit(c)) c = getchar();while(isdigit(c)) s[++ cnt] = c ^ '0', c = getchar();for(int i = 1; i <= (cnt >>1 );i ++) swap(s[i], s[cnt -i + 1]);// 反转数组,压入压位后的数组,以8位为一块x.n = 0;for(int i = 1; i <= cnt; i += 8) +{++ x.n;x.a[x.n] += (i + 7 <= cnt) ? s[i + 7] * 10000000 : 0;x.a[x.n] += (i + 6 <= cnt) ? s[i + 6] * 1000000 : 0;x.a[x.n] += (i + 5 <= cnt) ? s[i +5 ] * 100000 : 0;x.a[x.n] += (i + 4 <= cnt) ? s[i + 4] * 10000 : 0;x.a[x.n] += (i + 3 <= cnt) ? s[i + 3] * 1000 : 0;x.a[x.n] += (i + 2 <= cnt) ? s[i + 2] * 100 : 0;x.a[x.n] += (i + 1 <= cnt) ? s[i + 1] * 10 : 0;x.a[x.n] += (i + 0 <= cnt) ? s[i +0 ] * 1 : 0;}cut0(x);}inline void outwrite(const edge &x){printf("%lld", x.a[x.n]);for(int i = x.n - 1; i >= 1;i --) printf("%08lld", x.a[i]);puts("");}```### 6:[单调队列 / 滑动窗口](https://www.luogu.com.cn/problem/P1886)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1using namespace std;const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int n, k, a[maxn];int ans1[maxn], ans2[maxn];int q[maxn], head, tail;void query(int res[]){head = 1, tail = 0;for(int i = 1;i <= n;i ++){while(head <= tail && i - q[head] + 1 > k) q[head ++] = 0;while(head <= tail && a[q[tail]] > a[i]) q[tail --] = 0;q[++ tail] = i;if(i >= k) res[i - k + 1] = q[head];}}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];query(ans1);for(int i = 1;i <= n - k + 1;i ++) cout << a[ans1[i]] << " ";cout << endl;for(int i = 1;i <= n;i++) a[i] = -a[i];query(ans2);for(int i = 1;i <= n - k + 1;i ++)cout << -a[ans2[i]] << " ";from yanyu289;}```