标题
增加 1 行删除 1 行
1模板(更新中)1关于 BSGS
正文
增加 4 行删除 1977 行
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112424 行未改动124712481249125012511252125312541255125612571258125924 行未改动1284128512861287128812891290129112921293129412951296129712981299130024 行未改动1325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066## 树状数组### 单点修改 + 单点查询:::success[code]```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010], a[500010];int lowbit(int x){return x & (-x);}void add(int l, int a){for (int i = l; i <= n; i += lowbit(i))tree[i] += a;for (int i = l + 1; i <= n; i += lowbit(i))tree[i] -= a;}//差分,将第l个数加上avoid sum(int x){int s = 0;for (int i = x; i > 0; i -= lowbit(i))s += tree[i];//前缀和cout << s + a[x] << endl;}//查询数组第x个数并输出int main(){cin >> n >> m;for (int i = 1; i <= n; i ++)cin >> a[i];for (int i = 1; i <= m && cin >> now >> x; i ++){if (now == 1){cin >> y;add(x, y);}else sum(x);}return 0;}```:::### 单点修改 + 区间查询:::success[code]```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010];int lowbit(int x){return x & (-x);}void add(int l, int a){for (int i = l; i <= n; i += lowbit(i))tree[i] += a;}//将第l个数加上avoid sum(int l, int r){int s = 0;for (int i = r; i > 0; i -= lowbit(i))s += tree[i];for (int i = l - 1; i > 0; i -= lowbit(i))s -= tree[i];cout << s << endl;}//查询数组从l到r的区间和并输出int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> x; i ++)add(i, x);for (int i = 1; i <= m && cin >> now >> x >> y; i ++){if (now == 1)add(x, y);else sum(x, y);}return 0;}```:::### 区间修改 + 单点查询:::success[code]```cpp#include <iostream>using namespace std;int n, m, now, x, y, z, a[500010], c[500010];int lowbit(int x){return x & -x;}void add(int l, int r, int sum){for (int i = l; i <= n; i += lowbit(i))c[i] += sum;for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;//差分}//差分数组,从l到r全部加上sumvoid sum(int x){int s = 0;for (int i = x; i > 0; i -= lowbit(i))s += c[i];//前缀和统计cout << a[x] + s << endl;}//查询第x个数的值并输出int main(){cin >> n >> m;for (int i = 1; i <= n; i ++)cin >> a[i];for (int i = 1; i <= m && cin >> now >> x; i ++){if (now == 1){cin >> y >> z;add(x, y, z);}else sum(x);}return 0;}```:::### 区间修改 + 区间查询:::success[code]```cpp#include <iostream>using namespace std;long long n, m, now, x, y, z, a1, a2, b[100010], c[100010];long long lowbit(long long x){return x & (-x);}void add(long long l, long long r, long long sum){for (int i = l; i <= n; i += lowbit(i))c[i] += sum;for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;for (int i = l; i <= n; i += lowbit(i))b[i] += sum * (l - 1);for (int i = r + 1; i <= n; i += lowbit(i))b[i] -= sum * r;}//差分,从l到r每个数加上sum,b[i] = c[i] * (i - 1)void sum(long long l, long long r){long long sr = 0, sl = 0;for (int i = r; i > 0; i -= lowbit(i))sr += c[i];for (int i = l - 1; i > 0; i -= lowbit(i))sl += c[i];sr = sr * r - sl * (l - 1);for (int i = r; i > 0; i -= lowbit(i))sr -= b[i];for (int i = l - 1; i > 0; i -= lowbit(i))sr += b[i];cout << sr << endl;}//前缀和相加,从l到r的和//从l到r的和为c数组从1到r的和减从1到l - 1的和//化简为c数组从1到r的和乘以r减去b数组从1到r的和int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> a1; i ++)add(i, n, a1 - a2), a2 = a1;//求差分for (int i = 1; i <= m && cin >> now >> x >> y; i ++){if (now == 1){cin >> z;add(x, y, z);}else sum(x, y);}return 0;}```:::## 线段树### 单点修改 + 单点查询:::success[code]```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){if (l == r){tree[now] += sum;return;}long long mid = l + r >> 1;if (h <= mid)add(l, mid, now << 1, h, sum);else add(mid + 1, r, now << 1 | 1, h, sum);tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sumlong long sum_tree(long long l, long long r, long long now, long long x){if (l == r)return tree[now];long long mid = l + r >> 1;if (x <= mid)return sum_tree(l, mid, now << 1, x);else return sum_tree(mid + 1, r, now << 1 | 1, x);}//在数组中查询点xint main(){cin >> n >> w;for (int i = 1; i <= n && cin >> a; i ++)add(1, n, 1, i, a);//建树for (int i = 1; i <= w && cin >> a >> b; i ++){if (a == 1){cin >> c;add(1, n, 1, b, c);//单点修改}else cout << sum_tree(1, n, 1, b) << endl;//单点查询}return 0;}```:::### 单点修改 + 区间查询:::success[code]```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){if (l == r){tree[now] += sum;return;}long long mid = l + r >> 1;if (h <= mid)add(l, mid, now << 1, h, sum);else add(mid + 1, r, now << 1 | 1, h, sum);tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sumlong long sum_tree(long long l, long long r, long long now, long long x, long long y){if (x <= l && r <= y)return tree[now];long long mid = l + r >> 1, ans = 0;if (x <= mid)ans += sum_tree(l, mid, now << 1, x, y);if (mid < y)ans += sum_tree(mid + 1, r, now << 1 | 1, x, y);return ans;}//在区间[x, y]中求和int main(){cin >> n >> w;for (int i = 1; i <= n && cin >> a; i ++) add(1, n, 1, i, a);//建树for (int i = 1; i <= w && cin >> a >> b >> c; i ++){if (a == 1)add(1, n, 1, b, c);//单点修改else cout << sum_tree(1, n, 1, b, c) << endl;//区间查询}return 0;}```:::### 区间修改 + 区间查询(乘 + 加):::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, q, mod, a[100010], tree[400010], mul[400010], add[400010], opt, x, y, z;void down(int now, int l, int r){//懒标记下传int mid = l + r >> 1;mul[now << 1] = mul[now << 1] * 1LL * mul[now] % mod, mul[now << 1 | 1] = mul[now << 1 | 1] * 1LL * mul[now] % mod;add[now << 1] = add[now << 1] * 1LL * mul[now] % mod, add[now << 1 | 1] = add[now << 1 | 1] * 1LL * mul[now] % mod;tree[now << 1] = tree[now << 1] * 1LL * mul[now] % mod, tree[now << 1 | 1] = tree[now << 1 | 1] * 1LL * mul[now] % mod;(add[now << 1] += add[now]) %= mod, (add[now << 1 | 1] += add[now]) %= mod;(tree[now << 1] += add[now] * 1LL * (mid - l + 1) % mod) %= mod, (tree[now << 1 | 1] += add[now] * 1LL * (r - mid) % mod) %= mod;add[now] = 0, mul[now] = 1;}void init(int now, int l, int r){//初始化mul[now] = 1;//乘法初始化为 1if (l == r){tree[now] = a[l];return;}int mid = l + r >> 1;init(now << 1, l, mid), init(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void multion(int now, int l, int r){//乘法if (x <= l && r <= y){mul[now] = (1LL * mul[now] * z) % mod, add[now] = (1LL * add[now] * z) % mod, tree[now] = (1LL * tree[now] * z) % mod;return;}int mid = l + r >> 1;down(now, l, r);if (x <= mid)multion(now << 1, l, mid);if (y > mid)multion(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void plusion(int now, int l, int r){//加法if (x <= l && r <= y){(add[now] += z) %= mod, (tree[now] += z * 1LL * (r - l + 1)) % mod;return;}int mid = l + r >> 1;down(now, l, r);if (x <= mid)plusion(now << 1, l, mid);if (y > mid)plusion(now << 1 | 1, mid + 1, r);tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}int query(int now, int l, int r){//查询if (x <= l && r <= y)return tree[now];int ans = 0, mid = l + r >> 1;down(now, l, r);if (x <= mid)(ans += query(now << 1, l, mid)) %= mod;if (y > mid)(ans += query(now << 1 | 1, mid + 1, r)) %= mod;return ans;}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n >> q >> mod;for (int i = 1; i <= n; i ++)cin >> a[i];init(1, 1, n);while (q -- && cin >> opt >> x >> y){if (opt == 1){cin >> z;multion(1, 1, n);}else if (opt == 2){cin >> z;plusion(1, 1, n);}else cout << query(1, 1, n) << '\n';}return 0;}```:::## 快读### 基本版(int):::success[code]```cppvoid read(int &x){char ch = getchar_unlocked();bool f = 0;x = 0;while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar_unlocked();while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar_unlocked();if (f)x = -x;}```:::### 基本版(double):::success[code]```cppvoid read(double &x){char ch = getchar();double now = 0.1;bool f = 0;x = 0;while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar();while (ch >= '0' && ch <= '9')x = x * 10 + (ch ^ 48), ch = getchar();if (ch != '.'){x = (f ? -x : x);return;}ch = getchar();while (ch >= '0' && ch <= '9')x += now * (ch ^ 48), now *= 0.1, ch = getchar();x = (f ? -x : x);}```:::### fread 优化版(int):::success[code]```cpp//使用方法:IO::cin,IO::cout//代码末尾加 IO::cout.Flush()namespace IO{const int BUFSIZE = 1 << 20;char ibuf[BUFSIZE], *is = ibuf, *it = ibuf, tmp[BUFSIZE];int cnt = 0;inline char getch(){if (is == it)it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);return is == it ? EOF : *is ++;}int readInt(){int x = 0, y = 0;char c = 0;while (!isdigit(c))y |= c == '-', c = getch();while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getch();return !y ? x : -x;}void flush(){fwrite(tmp, 1, cnt, stdout);cnt = 0;}void putch(char c){tmp[cnt ++] = c;if (cnt == BUFSIZE)flush();}void putint(int x){char Q[10];int tp = 0;if (x < 0)putch('-'), x = -x;if (!x)putch('0');while (x)Q[tp ++] = (x % 10 + 48), x /= 10;while (tp --)putch(Q[tp]);}struct Basic{Basic & operator >> (int &A){A = IO::readInt();return (*this);}Basic & operator << (const int &A){putint(A);return (*this);}void Flush(){flush();}}cin, cout;}```:::## 单源最短路### Dijkstra(堆优化):::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];pair<int, int> now;vector<pair<int, int> > v[100010];priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;int main(){cin >> n >> m >> s;//由s至全图的最短路for (int i = 1; i <= 1e5 + 1; i ++)ans[i] = 1e10;ans[s] = 0, q.push({0, s});for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});while (q.size()){now = q.top(), q.pop();if (vis[now.second])continue;vis[now.second] = 1;for (auto u : v[now.second]){if (ans[u.first] > ans[now.second] + u.second){ans[u.first] = ans[now.second] + u.second;if (!vis[u.first])q.push({ans[u.first], u.first});}}}for (int i = 1; i <= n; i ++)cout << ans[i] << " ";return 0;}```:::### SPFA:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];vector<pair<int, int> > v[100010];bool spfa(){queue<int> q;q.push(s), vis[s] = 1, ans[s] = 0;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (ans[u.first] > ans[now] + u.second){ans[u.first] = ans[now] + u.second;if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}signed main(){cin >> n >> m >> s;//由s至全图的最短路for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});for (int i = 1; i <= n; i ++)ans[i] = 2e9;spfa();for (int i = 1; i <= n; i ++)cout << (ans[i] == 2e9 ? -1 : ans[i]) << " ";return 0;}```:::## 多源最短路### Floyd:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, q, x, y, z, ans[110][110];signed main(){cin >> n >> m >> q;for (int i = 1; i <= n; i ++){for (int j = 1; j <= n; j ++)ans[i][j] = (i == j ? 0 : 1e15);}for (int i = 1; i <= m && cin >> x >> y >> z; i ++)ans[x][y] = min(ans[x][y], z);for (int k = 1; k <= n; k ++){for (int i = 1; i <= n; i ++){for (int j = 1; j <= n; j ++)ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);}}while (q -- && cin >> x >> y)cout << (ans[x][y] == 1e15 ? -1 : ans[x][y]) << endl;return 0;}```:::## 割点### Tarjan:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, num, sum, root, low[20010], dfn[20010];bool ans[20010];vector<int> v[20010];void dfs(int now){low[now] = dfn[now] = ++ num;int child = 0;for (auto u : v[now]){if (!dfn[u])child ++, dfs(u), low[now] = min(low[u], low[now]), sum += (!ans[now] && now != root && low[u] >= dfn[now]), ans[now] |= (now != root && low[u] >= dfn[now]);//判定1else low[now] = min(low[now], dfn[u]);}if (now == root && child >= 2)sum += !ans[now], ans[now] = 1;//判定2}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);for (int i = 1; i <= n; i ++){if(!dfn[i])root = i, dfs(i);}cout << sum << endl;//数量for (int i = 1; i <= n; i ++){if (ans[i])cout << i << " ";}return 0;}```:::## 割边(桥)### Tarjan:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, a, b, low[200], dfn[200], num;vector<int> v[200];priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;//按照由小至大顺序输出void dfs(int now, int fa){low[now] = dfn[now] = ++ num;for (auto u : v[now]){if (fa == u)continue;if (!dfn[u]){dfs(u, now), low[now] = min(low[now], low[u]);if (low[u] > dfn[now])q.push({min(now, u), max(now, u)});//满足判定条件}else low[now] = min(low[now], dfn[u]);}}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> a >> b; i ++)v[a].push_back(b), v[b].push_back(a);//无向图存边dfs(1, 0);while (q.size()){cout << q.top().first << " " << q.top().second << endl;q.pop();}return 0;}```:::## 拓扑排序:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, d[1010];vector<int> v[1010];queue<int> q;queue<int> ans;int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), d[y] ++;for (int i = 1; i <= n; i ++){if (!d[i])q.push(i);//统计入度为0的点}while (q.size()){int now = q.front();q.pop(), ans.push(now);for (auto u : v[now]){d[u] --;//删边if (!d[u])q.push(u);}}if (ans.size() < n){cout << -1;return 0;}while (ans.size()){cout << ans.front() << " ";ans.pop();}return 0;}```:::## 最小生成树### Kruskal:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;struct node{int x, y, z;}a[400010];int n, m, ans, sum, fa[5010];bool cmp(node b, node c){return b.z < c.z;//对权值进行排序}int find(int now){if (fa[now] == now)return now;return fa[now] = find(fa[now]);}signed main(){cin >> n >> m;for (int i = 1; i <= m && cin >> a[i].x >> a[i].y >> a[i].z; i ++)a[i + m] = node{a[i].y, a[i].x, a[i].z};//无向图存边sort(a + 1, a + 2 * m + 1, cmp);for (int i = 1; i <= n; i ++)fa[i] = i;for (int i = 1; i <= 2 * m; i ++){if (find(a[i].x) != find(a[i].y))fa[find(a[i].y)] = fa[find(a[i].x)], ans += a[i].z, sum ++;if (sum >= n - 1){//已添完所有边cout << ans;return 0;}}cout << "orz";//不连通return 0;}```:::### Prim:::success[code]```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, sum, cnt, d[5010];bool vis[5010];vector<pair<long long, long long> > v[5010];int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});for (int i = 0; i <= n; i ++)d[i] = 1e9;d[1] = 0;for (int i = 1; i <= n; i ++){long long id = 0;for (int j = 1; j <= n; j ++){if (!vis[j] && d[j] < d[id])id = j;}if (!id){cout << "orz";return 0;}vis[id] = 1, sum += d[id];for (auto u : v[id]){if (!vis[u.first])d[u.first] = min(d[u.first], 1LL * u.second);}}cout << sum;return 0;}```:::## 点双连通分量### Tarjan:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, root, sum = 1;vector<int> v[500010], ans[500010];stack<int> sta;void dfs(int now, int fa){low[now] = dfn[now] = ++ num, sta.push(now);int child = 0;for (auto u : v[now]){if (u == fa)continue;if (!dfn[u]){child ++, dfs(u, now), low[now] = min(low[now], low[u]);if (low[u] >= dfn[now]){while (sta.top() != u)ans[sum].push_back(sta.top()), sta.pop();ans[sum].push_back(u), ans[sum ++].push_back(now), sta.pop();//找到割点,记录}}else low[now] = min(low[now], dfn[u]);}if (now == root && !child)ans[sum ++].push_back(now);//特判只有一个点}int main(){ios::sync_with_stdio(0);cin.tie();cout.tie();cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);for (int i = 1; i <= n; i ++){if (!dfn[i])root = i, dfs(i, 0);//图不保证连通}cout << -- sum << endl;//压行的奇怪手段for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```:::## 边双连通分量### Tarjan:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, sum;vector<int> ans[500010];stack<int> sta;vector<pair<int, int> > v[500010];void tarjan(int now, int fa){low[now] = dfn[now] = ++ num, sta.push(now);for (auto u : v[now]){if (u.second == (fa ^ 1))continue;//这里的fa表示边的编号if (!dfn[u.first]){tarjan(u.first, u.second), low[now] = min(low[now], low[u.first]);}else low[now] = min(low[now], dfn[u.first]);}if (low[now] == dfn[now]){//为割边(桥),记录ans[++ sum].push_back(now);while (sta.top() != now)ans[sum].push_back(sta.top()), sta.pop();sta.pop();}}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back({y, i << 1}), v[y].push_back({x, i << 1 | 1});//为每条边编号for (int i = 1; i <= n; i ++){if (!dfn[i])tarjan(i, 0);//图不保证连通}cout << sum << endl;for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```:::## 强连通分量### Tarjan:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, dfn[10010], low[10010], num, sum;bool vis[10010];vector<int> v[10010], ans[10010];stack<int> sta;void dfs(int now){dfn[now] = low[now] = ++ num, sta.push(now), vis[now] = true;for (auto u : v[now]){if (!dfn[u])dfs(u), low[now] = min(low[now], low[u]);else if (vis[u])low[now] = min(low[now], dfn[u]);}if (low[now] == dfn[now]){//割边ans[++ sum].push_back(now), vis[now] = false;while (sta.top() != now)ans[sum].push_back(sta.top()), vis[sta.top()] = false, sta.pop();sta.pop();}}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y);for (int i = 1; i <= n; i ++){if (!dfn[i])dfs(i);}cout << sum << endl;for (int i = 1; i <= sum; i ++){cout << ans[i].size() << " ";for (auto u : ans[i])cout << u << " ";cout << endl;}return 0;}```:::## 负(权)环### SPFA:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int T, n, m, x, y, z, ans[2010], sum[2010];bool vis[2010];vector<pair<int, int> > v[2010];bool spfa(int now){for (int i = 1; i <= n; i ++)vis[i] = 0, sum[i] = 0, ans[i] = 1e9;//初始化queue<int> q;q.push(now), ans[now] = 0, vis[now] = 1;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (ans[u.first] > ans[now] + u.second){ans[u.first] = ans[now] + u.second, sum[u.first] = sum[now] + 1;if (sum[u.first] >= n)return true;//若经过的点超过整张图的点数则存在负(权)环if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}int main(){cin >> n >> m;bool flag = false;for (int i = 1; i <= 2001; i ++)v[i].clear();for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z});for (int i = 1; i <= n; i ++){//图不保证连通,需要每个子图都遍历一次if (!vis[i]){if (spfa(i)){cout << "YES" << endl;flag = true;break;}}}if (!flag)cout << "NO" << endl;return 0;}```:::## KMP### KMP:::success[code]```cpp#include <bits/stdc++.h>using namespace std;string s1, s2;int l1, l2, n[1000010];int main(){cin >> s1 >> s2;l1 = s1.size(), l2 = s2.size();for (int i = 1, j = 0; i < l2; i ++){while (j && s2[i] != s2[j])j = n[j - 1];n[i] = (j += (s2[i] == s2[j]));}for (int i = 0, j = 0; i < l1; i ++){while (j && s1[i] != s2[j])j = n[j - 1];if (s1[i] == s2[j])j ++;if (j == l2){cout << i - l2 + 2 << '\n';j = n[j - 1];}}return 0;}```:::### exKMP(Z 函数):::success[code]```cpp#include <bits/stdc++.h>using namespace std;string a, b;int n, z[40000010];//z[i] -> [i, n]'s LCP's lengthint main(){ios::sync_with_stdio(0), cin.tie(0),cin >> a >> b, z[0] = n = (b += a).size();for (int i = 1, l = 0, r = 0; i < n; i ++){z[i] = i > r ? 0 : min(z[i - l], r - i);//在最长覆盖之内while (i + z[i] < n && b[z[i]] == b[i + z[i]])z[i] ++;//暴力拓展if (i + z[i] > r)l = i, r = i + z[i];//更新最长覆盖}long long ansz = 0, ansp = 0;for (int i = 0; i < n - a.size(); i ++)ansz ^= (i + 1LL) * min(1ULL + z[i], 1ULL + n - a.size() - i);for (int i = n - a.size(); i < n; i ++)ansp ^= (i - n + a.size() + 1LL) * (z[i] + 1LL);return cout << ansz << '\n' << ansp, 0;}```:::## 哈希### 进制哈希:::success[code]```cpp#include <bits/stdc++.h>#define int unsigned long longusing namespace std;int n, ans, now, l = 132135461;//随意一个数(最好是质数)string s;map<int, bool> m;int Hash(string x){int sum = 0, len = x.size();for (int i = 0; i < len; i ++)sum = (sum * l) + x[i];//模拟进制return sum;}signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for (int i = 1; i <= n && cin >> s; i ++)now = Hash(s), ans += !m[now], m[now] = 1;cout << ans;return 0;}```:::## ST表:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, l, r, maxn[100010][18], k;//从第i个数开始长度为2^j的序列最大值int main(){cin >> n >> m;for (int i = 1; i <= n && cin >> maxn[i][0]; i ++);k = log2(n);for (int i = 1; i <= k; i ++){for (int j = 1; j + (1 << i) - 1 <= n; j ++)maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << i - 1)][i - 1]);//O(nlogn)更新ST表}while (m -- && cin >> l >> r){k = log2(r - l + 1);cout << max(maxn[l][k], maxn[r - (1 << k) + 1][k]) << '\n';//找最大能覆盖的区间}return 0;}```:::## 树的重心:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, son[50010], w[50010];vector<int> v[50010];priority_queue<int, vector<int>, greater<int> > ans;void dfs(int fa, int now){son[now] = 1, w[now] = 0;for (auto u : v[now]){if (u != fa)dfs(now, u), son[now] += son[u], w[now] = max(w[now], son[u]);}w[now] = max(w[now], n - son[now]);if (w[now] <= n / 2)ans.push(now);}int main(){cin >> n;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(0, 1);while (ans.size()){cout << ans.top() << " ";ans.pop();}return 0;}```:::## 树的直径### 树形 dp:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, x, y, z, f[500010], ans;vector<pair<int, int> > v[500010];void dfs(int fa, int now){for (auto u : v[now])if (u.first != fa)dfs(now, u.first), ans = max(ans, f[now] + f[u.first] +u.second), f[now] = max(f[now], f[u.first] + u.second);}signed main(){ios::sync_with_stdio(0);cin.tie(0);cin >> n;for (int i = 1; i < n && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z});dfs(0, 1);cout << ans;return 0;}```### dfs:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, d[100010], s, sum;vector<int> v[100010];void dfs(int fa, int now){for (auto u : v[now]){if (u != fa)d[u] = d[now] + 1, s = (d[u] > d[s] ? u : s), dfs(now, u);}}int main(){cin >> n;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(0, 1), d[s] = 0, dfs(0, s);cout << d[s];return 0;}```:::## LCA### 倍增:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, k, fa[500010][20], d[500010];vector<int> v[500010];void dfs(int now, int f){d[now] = d[f] + 1, fa[now][0] = f;for (int i = 1; (1 << i) <= d[now]; i ++)fa[now][i] = fa[fa[now][i - 1]][i - 1];//类似ST表预处理祖先节点for (auto u : v[now]){if (u != f)dfs(u, now);}}int solve(int a, int b){if (d[b] > d[a])swap(a, b);//始终让a在b后面(深度较大)for (int i = 19; i >= 0; i --){if (d[a] - (1 << i) >= d[b])a = fa[a][i];//把a的深度跳至b}if (a == b)return a;for (int i = 19; i >= 0; i --){if (fa[a][i] != fa[b][i])a = fa[a][i], b = fa[b][i];//a和b同时跳}return fa[a][0];}int main(){cin >> n >> m >> s;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs(s, 0);for (int i = 1; i <= m && cin >> x >> y; i ++)cout << solve(x, y) << '\n';return 0;}```:::### 树链剖分:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;int n, m, r, x, y, d[N], fa[N], siz[N], son[N], top[N];vector<int> v[N];void dfs1(int now, int f){//找重儿子d[now] = d[f] + 1, fa[now] = f, siz[now] = 1;for (auto u : v[now])if (u != f)dfs1(u, now), siz[now] += siz[u], son[now] =!son[now] || siz[son[now]] < siz[u] ? u : son[now];}void dfs2(int now, int tnow){top[now] = tnow;if (!son[now])return;//叶子结点直接返回dfs2(son[now], tnow);//先递归重链for (auto u : v[now])if (u != fa[now] && u != son[now])dfs2(u, u);//以轻儿子为链头的重链}int LCA(){while (top[x] != top[y]){//不在同一重链上if (d[top[x]] < d[top[y]])swap(x, y);//让 x 所在重链的链头更深x = fa[top[x]];//跳链}return d[x] < d[y] ? x : y;}int main(){ios::sync_with_stdio(0), cin.tie(0);cin >> n >> m >> r;for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x);dfs1(r, 0), dfs2(r, r);while (m -- && cin >> x >> y)cout << LCA() << '\n';return 0;}```:::## 拓展欧几里得:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int x, y;void exgcd(int a, int b){//求特殊解if (!b){x = 1, y = 0;//ax + by = a 时的解return;}exgcd(b, a % b);//辗转相除int now = x;x = y, y = now - a / b * y;}signed main(){int a, b;cin >> a >> b;exgcd(a, b);cout << (x % b + b) % b;//最小解return 0;}```:::## 斜率优化(下凸包格式):::success[code]```cpp#include <bits/stdc++.h>using namespace std;long long n, nx, bk = 1, fr = 1, f[1000010];long double X(int x){return ();}long double Y(int x){return ();}long double slope(int x, int y){return (Y(x) - Y(y)) / (X(x) - X(y));//求斜率}int main(){cin >> n;()//输入for (int i = 1; i <= n; i ++){//单调队列维护下凸壳while (fr < bk && slope(q[fr + 1], q[fr]) < ())fr ++;f[i] = ();while (fr < bk && slope(q[bk], q[bk - 1]) > slope(i, q[bk]))bk --;q[++ bk] = i;}cout << f[n];return 0;}```:::## 差分约束:::success[code]```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, d[5010], sum[5010];bool vis[5010];vector<pair<int, int> > v[5010];queue<int> q;bool spfa(){//跑最短路q.push(0), vis[0] = 1, sum[0] = 1;while (q.size()){int now = q.front();q.pop(), vis[now] = 0;for (auto u : v[now]){if (d[u.first] > d[now] + u.second){d[u.first] = d[now] + u.second, sum[u.first] = sum[now] + 1;if (sum[u.first] > n + 1)return true;//注意多了一个点if (!vis[u.first])vis[u.first] = 1, q.push(u.first);}}}return false;}int main(){cin >> n >> m;for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[y].push_back({x, z});//存边for (int i = 1; i <= n; i ++)d[i] = 1e9, v[0].push_back({i, 0});//建一个虚点if (spfa()){cout << "NO";return 0;}for (int i = 1; i <= n; i ++)cout << d[i] << " ";return 0;}```:::## 字典树:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int T, n, q, cnt[3000010], t[3000010][65], num;string s;int g(char x){return (x >= 'A' && x <= 'Z' ? x - 'A' : (x >= 'a' && x <= 'z' ? x - 'a' + 26 : x - '0' + 52));}int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> T;while (T -- && cin >> n >> q){for (int i = 1, np = 0; i <= n && cin >> s; np = 0, i ++){for (int j = 0, l = s.size(), c = g(s[j]); j < l; np = t[np][c], cnt[np] ++, c = g(s[++ j])){if (!t[np][c])t[np][c] = ++ num;//建树}}for (int i = 1, ans = 0; i <= q && cin >> s; ans = 0, i ++){for (int j = 0, l = s.size(), np = 0, c = g(s[j]); j < l; np = t[np][c], ans = cnt[np], c = g(s[++ j])){if (!t[np][c]){//查询ans = 0;break;}}cout << ans << '\n';}for (int i = 0; i <= num; cnt[i ++] = 0){for (int j = 0; j <= 64; j ++)t[i][j] = 0;}num = 0;//多测清空}return 0;}```:::## Manacher(马拉车):::success[code]```cpp#include <bits/stdc++.h>using namespace std;int d[22000010], l, r, ans;string s;char now[22000010];int main(){ios::sync_with_stdio(0);cin.tie(0);cin >> s;now[0] = '#';for (int i = 0, l = s.size(); i < l; ++ i)now[i << 1 | 1] = s[i], now[(i << 1) + 2] = '#';//加间隔符for (int i = 0, n = (s.size() << 1) + 1; i < n; ans = max(ans, d[i ++])){int k = i > r ? 1 : min(d[l + r - i], r - i + 1);//找到当前最长的回文串长度(总长的一半)while (0 <= i - k && i + k < n && now[i - k] == now[i + k])k ++;//不断拓展d[i] = k --;if (i + k > r)l = i - k, r = i + k;//更新最大右端点}cout << ans - 1;return 0;}```:::## AC 自动机:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 200010;string t;vector<int> v[N];int n, num, ans, vis[N], vp[N];struct node{int son[30], fail, cnt, flag;}a[N];void insert(string now, int sp){int id = 0, l = now.size();for (int i = 0; i < l; i ++)id = a[id].son[now[i] - 97] ? a[id].son[now[i] - 97] : a[id].son[now[i] - 97] = ++ num;vp[sp] = id;}void getfail(){queue<int> q;for (int i = 0; i < 26; i ++)if (a[0].son[i])a[a[0].son[i]].fail = 0, q.push(a[0].son[i]);while (!q.empty()){int now = q.front(), f = a[now].fail;q.pop();for (int i = 0; i < 26; i ++)if (a[now].son[i])a[a[now].son[i]].fail = a[f].son[i], q.push(a[now].son[i]);else a[now].son[i] = a[f].son[i];}for (int i = 1; i <= num; i ++)v[a[i].fail].push_back(i);//将 fail 边建成树}void query(){//查询文本串int id = 0, l = t.size();for (int i = 0; i < l; i ++)a[id = a[id].son[t[i] - 97]].cnt ++;//进行树上的修改}void dfs(int u){for (auto e : v[u])dfs(e), a[u].cnt += a[e].cnt;}//树上前缀和int main(){ios::sync_with_stdio(0), cin.tie(0), cin >> n;for (int i = 1; i <= n; i ++)cin >> t, insert(t, i);cin >> t, getfail(), query(), dfs(0);for (int i = 1; i <= n; i ++)cout << a[vp[i]].cnt << '\n';return 0;}```:::## BSGS### BSGS(Baby Step,Gaint Step)算法给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。4 行未改动于是把所有 $b \times a ^ j \bmod p$ 放入一个哈希表,再遍历 $i$ 查找 $a ^ {i \times t} \bmod p$ 即可做到 $\sqrt p$ 实现啦!注意特判若 $a = 0$ 且 $b \ne 0$ 时无解。:::success[code]```cpp#include <bits/stdc++.h>using namespace std;struct custom_hash{//哈希static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x=(x ^ (x >> 27)) * 0x94d049bb133111eb;24 行未改动return BSGS(b, n, p), 0;}```:::### exBSGS 算法若 $a$ 与 $p$ 不互质,则需要不断将 $a$、$b$ 和 $p$ 除去 $\gcd(a, \ p)$,其中若出现 $b$ 有余数的情况则必然无解。记每次的 $\gcd(a_i, \ p_i) = d_i$,则得到:$$\prod \limits _ {i = 1} ^ k \frac{a}{d_i} · a ^ {x - k} \equiv \frac{b}{\prod \limits _ {i = 1} ^ k d_i} \pmod p$$用 BSGS 求解即可。:::success[code]```cpp#include <bits/stdc++.h>#define int long long24 行未改动return 0;}```:::## 平衡树### 替罪羊树:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;const double alpha = 0.75;//不平衡率struct node{int ls, rs, v, tot, siz, del;void init(){ls = rs = 0, siz = tot = del = 1;}//初始化节点}t[N];int order[N], cnt;//用于存拍平后的数int bin[N], top;//回收空间int root;//记录根节点void inorder(int u){//拍平以 u 为根的子树,此处采用中序遍历使得存储的值升序排列if (!u)return;inorder(t[u].ls);if (t[u].del)order[++ cnt] = u;else bin[++ top] = u;//判断是否为空节点inorder(t[u].rs);}void pushup(int u){//上传子树信息t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1,t[u].tot = t[t[u].ls].tot + t[t[u].rs].tot + 1;}void build(int l, int r, int &u){//建树int mid = l + r >> 1;u = order[mid];//新根设为中点使树尽量平衡if (l == r)return t[u].init();if (l < mid)build(l, mid - 1, t[u].ls);if (l == mid)t[u].ls = 0;//无左儿子(mid 的取值向下取整,故需判断左边界)build(mid + 1, r, t[u].rs), pushup(u);}void rebuild(int &u){//拍平并重建树cnt = 0, inorder(u);//先拍平if (cnt)build(1, cnt, u);else u = 0;//有节点则重建,否则设为空}bool balance(int u){//以 u 为根的子树是否不平衡return t[u].siz * alpha <= 1.0 * max(t[t[u].ls].siz, t[t[u].rs].siz);}void insert(int &u, int v){//插入数字 vif (!u){//到达空节点则直接插入u = bin[top --], t[u].v = v;return t[u].init();}t[u].siz ++, t[u].tot ++;if (t[u].v >= v)insert(t[u].ls, v);else insert(t[u].rs, v);//按二叉查找树权值插入if (balance(u))rebuild(u);//不平衡则重建}int rk(int u, int v){//小于 v 的数的个数if (!u)return 0;if (v <= t[u].v)return rk(t[u].ls, v);//在左子树上return t[t[u].ls].siz + t[u].del + rk(t[u].rs, v);//在右子树上}int kth(int k){//第 k 小的数int u = root;//从根节点开始找while (u){//只要非空节点if (t[u].del && t[t[u].ls].siz + 1 == k)return t[u].v;else if (t[t[u].ls].siz >= k)u = t[u].ls;else k -= t[t[u].ls].siz + t[u].del, u = t[u].rs;}return 0;}void delk(int &u, int k){//删除排名为 k 的数字t[u].siz --;if (t[u].del && t[t[u].ls].siz + 1 == k){t[u].del = 0;return;}//在根节点if (t[t[u].ls].siz + t[u].del >= k)delk(t[u].ls, k);//在左子树上else delk(t[u].rs, k - t[t[u].ls].siz - t[u].del);//在右子树上}void remove(int v){//删除数字 vdelk(root, rk(root, v) + 1);if (t[root].tot * alpha >= t[root].siz)rebuild(root);//不平衡则重建}int main(){for (int i = N - 1; i >= 1; i --)bin[++ top] = i;int n, opt, x;ios::sync_with_stdio(0), cin.tie(0), cin >> n;while (n -- && cin >> opt >> x)if (opt == 1)insert(root, x);else if (opt == 2)remove(x);else if (opt == 3)cout << rk(root, x) + 1 << '\n';else if (opt == 4)cout << kth(x) << '\n';else if (opt == 5)cout << kth(rk(root, x)) << '\n';else cout << kth(rk(root, x + 1) + 1) << '\n';return 0;}```:::### Treap:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int cnt;//最新节点的存储位置struct node{int ls, rs, siz;int v, p;//键值和优先级}t[N];void newnode(int v){//初始化新节点t[cnt].v = v, t[cnt].ls = t[cnt].rs = 0,t[cnt].siz = 1, t[cnt].p = rand();}void pushup(int u){t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;}//上传信息void rotate(int &r, int d){//旋转以 r 为根的子树int u;if (!d)u = t[r].rs, t[r].rs = t[u].ls, t[u].ls = r;//左旋else u = t[r].ls, t[r].ls = t[u].rs, t[u].rs = r;t[u].siz = t[r].siz, pushup(r), r = u;}void insert(int &u, int v){//插入数字 vif (!u){u = ++ cnt;return newnode(v);}//空节点直接插入t[u].siz ++;if (v < t[u].v)insert(t[u].ls, v);else insert(t[u].rs, v);if (t[u].ls && t[u].p > t[t[u].ls].p)rotate(u, 1);if (t[u].rs && t[u].p > t[t[u].rs].p)rotate(u, 0);pushup(u);}void del(int &u, int v){//删除数字 vt[u].siz --;if (t[u].v == v){//注意删除时维护优先级小根堆if (!t[u].ls && !t[u].rs){u = 0;return;}if (!t[u].ls || !t[u].rs){u = t[u].ls + t[u].rs;return;}if (t[t[u].ls].p < t[t[u].rs].p){rotate(u, 1), del(t[u].rs, v);return;}else{rotate(u, 0), del(t[u].ls, v);return;}}del(t[u].v < v ? t[u].rs : t[u].ls, v), pushup(u);}int rk(int u, int v){//小于 v 的数的个数if (!u)return 0;if (v <= t[u].v)return rk(t[u].ls, v);//左子树上return t[t[u].ls].siz + 1 + rk(t[u].rs, v);//右子树上}int kth(int u, int k){//第 k 小的数if (k == t[t[u].ls].siz + 1)return t[u].v;if (k <= t[t[u].ls].siz)return kth(t[u].ls, k);//左子树上return kth(t[u].rs, k - t[t[u].ls].siz - 1);//右子树上}int main(){srand(time(0)), ios::sync_with_stdio(0), cin.tie(0);int root = 0, n, opt, x;cin >> n;while (n -- && cin >> opt >> x)if (opt == 1)insert(root, x);else if (opt == 2)del(root, x);else if (opt == 3)cout << rk(root, x) + 1 << '\n';else if (opt == 4)cout << kth(root, x) << '\n';else if (opt == 5)cout << kth(root, rk(root, x)) << '\n';else cout << kth(root, rk(root, x + 1) + 1) << '\n';return 0;}```:::### FHQ Treap:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int cnt, root;struct node{int ls, rs, siz;int v, p;//键值和优先级}t[N];void newnode(int v){//新建节点t[++ cnt].v = v, t[cnt].ls = t[cnt].rs = 0;t[cnt].siz = 1, t[cnt].p = rand();}void pushup(int u){t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;}//上传子节点信息void split(int u, int v, int &L, int &R){//权值分裂,返回两树的根if (!u){L = R = 0;return;}if (t[u].v <= v)//在右子树L = u, split(t[u].rs, v, t[u].rs, R);else R = u, split(t[u].ls, v, L, t[u].ls);//在左子树pushup(u);}int merge(int L, int R){//合并两棵树并返回树根if (!L || !R)return L + R;if (t[L].p < t[R].p)//L 为父节点{t[L].rs = merge(t[L].rs, R), pushup(L);return L;}else{t[R].ls = merge(L, t[R].ls), pushup(R);return R;}}void insert(int v){//插入数字 vint L, R, nr;//一棵树不大于 v,一棵树大于 vsplit(root, v, L, R), newnode(v);nr = merge(L, cnt), root = merge(nr, R);//加入 v 再合并}void del(int v){//删除数字 vint L, R, nr;//nr 存储值为 v 的树split(root, v, L, R), split(L, v - 1, L, nr);//分裂得到 nrnr = merge(t[nr].ls, t[nr].rs);//删去根节点root = merge(merge(L, nr), R);//合并回原树}int rk(int v){//小于 v 的数的个数 + 1(即 v 的排名)int L, R, nr;split(root, v - 1, L, R);//得到小于 v 的树nr = t[L].siz + 1, root = merge(L, R);return nr;}int kth(int u, int k){//第 k 小的数的编号if (k == t[t[u].ls].siz + 1)return u;if (k <= t[t[u].ls].siz)return kth(t[u].ls, k);return kth(t[u].rs, k - t[t[u].ls].siz - 1);}int pre(int v){//数字 v 的前驱int L, R, p;split(root, v - 1, L, R);//得到小于 v 的树p = t[kth(L, t[L].siz)].v, root = merge(L, R);//合并回原树return p;}int nxt(int v){//数字 v 的后继int L, R, p;split(root, v, L, R);//得到大于 v 的树p = t[kth(R, 1)].v, root = merge(L, R);//合并回原树return p;}int main(){srand(time(0)), ios::sync_with_stdio(0), cin.tie(0);int n, opt, x;cin >> n;while (n -- && cin >> opt >> x)if (opt == 1)insert(x);else if (opt == 2)del(x);else if (opt == 3)cout << rk(x) << '\n';else if (opt == 4)cout << t[kth(root, x)].v << '\n';else if (opt == 5)cout << pre(x) << '\n';else cout << nxt(x) << '\n';return 0;}```:::## 重链剖分:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n, m, r, p, op, a, b, c, cnt, w[N], nw[N], d[N], fa[N],siz[N], son[N], top[N], id[N], tree[N << 2], tag[N << 2];vector<int> v[N];void pushup(int u){tree[u] = (tree[u << 1] + tree[u << 1 | 1]) % p;}void add(int u, int l, int r, int v){tag[u] += v, (tree[u] += v * (r - l + 1)) %= p;}void pushdown(int u, int l, int r){if (!tag[u])return;int mid = l + r >> 1;add(u << 1 | 1, mid + 1, r, tag[u]),add(u << 1, l, mid, tag[u]), tag[u] = 0;}void build(int u, int l, int r){if (l == r){tree[u] = nw[l] % p;return;}int mid = l + r >> 1;build(u << 1 | 1, mid + 1, r),build(u << 1, l, mid), pushup(u);}void update(int L, int R, int u, int l, int r, int v){if (L > r || R < l)return;if (L <= l && r <= R)return add(u, l, r, v);pushdown(u, l, r);int mid = l + r >> 1;update(L, R, u << 1 | 1, mid + 1, r, v),update(L, R, u << 1, l, mid, v), pushup(u);}int query(int L, int R, int u, int l, int r){if (L > r || R < l)return 0;if (L <= l && r <= R)return tree[u];pushdown(u, l, r);int mid = l + r >> 1;return (query(L, R, u << 1, l, mid) +query(L, R, u << 1 | 1, mid + 1, r)) % p;}void dfs1(int now, int f){//找重儿子d[now] = d[f] + 1, fa[now] = f, siz[now] = 1;for (auto u : v[now])if (u != f)dfs1(u, now), siz[now] += siz[u], son[now] =!son[now] || siz[son[now]] < siz[u] ? u : son[now];}void dfs2(int now, int tnow){id[now] = ++ cnt, nw[cnt] = w[now], top[now] = tnow;if (!son[now])return;//叶子结点直接返回dfs2(son[now], tnow);//先递归重链for (auto u : v[now])if (u != fa[now] && u != son[now])dfs2(u, u);//以轻儿子为链头的重链}void update_line(int x, int y, int v){while (top[x] != top[y]){//不在同一重链上if (d[top[x]] < d[top[y]])swap(x, y);//让 x 所在重链的链头更深update(id[top[x]], id[x], 1, 1, n, v), x = fa[top[x]];//跳链}if (d[x] > d[y])swap(x, y);update(id[x], id[y], 1, 1, n, v);//同一条链上的修改}int query_line(int x, int y){int ans = 0;while (top[x] != top[y]){if (d[top[x]] < d[top[y]])swap(x, y);ans += query(id[top[x]], id[x], 1, 1, n),ans %= p, x = fa[top[x]];}if (d[x] > d[y])swap(x, y);ans += query(id[x], id[y], 1, 1, n);return ans % p;}void update_tree(int x, int v){update(id[x], id[x] + siz[x] - 1, 1, 1, n, v);}int query_tree(int x){return query(id[x], id[x] + siz[x] - 1, 1, 1, n);}int main(){ios::sync_with_stdio(0), cin.tie(0);cin >> n >> m >> r >> p;for (int i = 1; i <= n; i ++)cin >> w[i];for (int i = 1; i < n && cin >> a >> b; i ++)v[a].push_back(b), v[b].push_back(a);dfs1(r, 0), dfs2(r, r), build(1, 1, n);while (m -- && cin >> op >> a)if (op == 1)cin >> b >> c, update_line(a, b, c);else if (op == 2)cin >> b, cout << query_line(a, b) << '\n';else if (op == 3)cin >> b, update_tree(a, b);else cout << query_tree(a) << '\n';return 0;}```:::## 可持久化线段树(主席树):::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 2e5 + 10;struct tree{int l, r, sum;}t[N << 5];int n, m, a[N], b[N], root[N], cnt;//root -> 第 i 棵树的根节点int update(int pre, int l, int r, int x){int rt = ++ cnt, mid = l + r >> 1;t[rt].l = t[pre].l, t[rt].r = t[pre].r,t[rt].sum = t[pre].sum + 1;//继承上一棵树的位置if (l < r)if (x <= mid)t[rt].l = update(t[pre].l, l, mid, x);else t[rt].r = update(t[pre].r, mid + 1, r, x);return rt;//返回根节点编号}int query(int L, int R, int l, int r, int k){//区间第 k 小if (l == r)return l;int x = t[t[R].l].sum - t[t[L].l].sum, mid = l + r >> 1;//x -> 左子树元素个数,即多少数不大于 lif (x >= k)return query(t[L].l, t[R].l, l, mid, k);else return query(t[L].r, t[R].r, mid + 1, r, k - x);}signed main(){ios::sync_with_stdio(0), cin.tie(0);cin >> n >> m;for (int i = 1; i <= n; i ++)cin >> a[i], b[i] = a[i];sort(b + 1, b + n + 1);//对 b 排序以进行离散化int len = unique(b + 1, b + n + 1) - b - 1, l, r, k;//总值域for (int i = 1; i <= n; i ++)//离散化,动态开点建权值线段树root[i] = update(root[i - 1], 1, len,lower_bound(b + 1, b + len + 1, a[i]) - b);while (m -- && cin >> l >> r >> k)//还原回原数cout << b[query(root[l - 1], root[r], 1, len, k)] << '\n';return 0;}```:::## 可持久化并查集:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 4e6;struct node{int ls, rs, dep, fa;}t[N];int n, m, cnt, root[N];int build(int l, int r){int rt = ++ cnt, mid = l + r >> 1;if (l == r)return t[rt].fa = l, rt;t[rt].ls = build(l, mid), t[rt].rs = build(mid + 1, r);return rt;}int query(int u, int l, int r, int x){if (l == r)return u;int mid = l + r >> 1;return mid >= x ? query(t[u].ls, l,mid, x) : query(t[u].rs, mid + 1, r, x);}int find(int tim, int u){int fa = query(root[tim], 1, n, u);return t[fa].fa == u ? fa : find(tim, t[fa].fa);}int unite(int u, int l, int r, int x, int f){int rt = ++ cnt, mid = l + r >> 1;t[rt] = t[u];if (l == r)return t[rt].fa = f, rt;if (mid >= x)t[rt].ls = unite(t[u].ls, l, mid, x, f);else t[rt].rs = unite(t[u].rs, mid + 1, r, x, f);return rt;}int update(int u, int l, int r, int x){int rt = ++ cnt, mid = l + r >> 1;t[rt] = t[u];if (l == r)return t[rt].dep ++, rt;if (mid >= x)t[rt].ls = update(t[u].ls, l, mid, x);else t[rt].rs = update(t[u].rs, mid + 1, r, x);return rt;}void merge(int tim, int a, int b){root[tim] = root[tim - 1], a = find(tim, a), b = find(tim, b);if (t[a].fa == t[b].fa)return;if (t[a].dep > t[b].dep)swap(a, b);root[tim] = unite(root[tim], 1, n, t[a].fa, t[b].fa);if (t[a].dep == t[b].dep)root[tim] = update(root[tim], 1, n, t[b].fa);}bool check(int tim, int a, int b){return t[find(tim, a)].fa == t[find(tim, b)].fa;}int main(){ios::sync_with_stdio(0), cin.tie(0),cin >> n >> m, root[0] = build(1, n);for (int i = 1, op, a, b; i <= m && cin >> op >> a; i ++)if (op == 1)cin >> b, merge(i, a, b);else if (op == 2)root[i] = root[a];else cin >> b, root[i] = root[i - 1],cout << check(i, a, b) << '\n';return 0;}```:::## 线性基:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, ans, a, p[60];signed main(){cin >> n;for (int i = 1; i <= n && cin >> a; i ++)for (int j = 50; j >= 0; j --)if (a & (1LL << j))if (p[j])a ^= p[j];else{p[j] = a;break;}for (int i = 51; i >= 0; i --)ans = max(ans, ans ^ p[i]);return cout << ans, 0;}```:::## 网络流### 最大流/最小割#### EK(Edmond-Karp):::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 210, inf = 1e17;int n, m, a, b, c, ans, g[N][N], p[N], f[N];int bfs(){memset(p, -1, sizeof(p)), f[1] = inf, p[1] = 0;queue<int> q;q.push(1);while (q.size()){int u = q.front();q.pop();if (u == n)break;for (int i = 1; i <= n; i ++)if (p[i] == -1 && g[u][i] > 0 && i - 1)p[i] = u, q.push(i), f[i] = min(f[u], g[u][i]);}return p[n] == -1 ? -1 : f[n];}void flow(){while (1){int mf = bfs();if (mf == -1)break;int u = n, fa;while (u - 1)g[fa = p[u]][u] -= mf,g[u][fa] += mf, u = fa;ans += mf;}}signed main(){cin >> m >> n;for (int i = 1; i <= m; i ++)cin >> a >> b >> c, g[a][b] += c;flow();return cout << ans, 0;}```:::#### Dinic:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 210, M = 10010, inf = 1e17;struct edge{int to, w, nxt;}e[M];int n, m, s, t, a, b, c, cnt = 1, d[N], cur[N], h[N];void add(){//链式前向星加边e[++ cnt] = {b, c, h[a]}, h[a] = cnt,e[++ cnt] = {a, 0, h[b]}, h[b] = cnt;}bool bfs(){memset(d, 0, sizeof(d));queue<int> q;q.push(s), d[s] = 1, cur[s] = h[s];while (q.size()){int u = q.front();q.pop();for (int i = h[u], v; i; i = e[i].nxt)if (!d[v = e[i].to] && e[i].w > 0){d[v] = d[u] + 1, q.push(v), cur[v] = h[v];if (v == t)return 1;}}return 0;}int dfs(int u, int f){if (u == t)return f;int sum = 0;for (int i = cur[u], v; i && f > 0; i = e[i].nxt){cur[u] = i, v = e[i].to;//当前弧优化if (d[v] == d[u] + 1 && e[i].w > 0){int nf = dfs(v, min(f, e[i].w));e[i].w -= nf, sum += nf,e[i ^ 1].w += nf, f -= nf;}}if (!sum)d[u] = 0;return sum;}int dinic(){int ans = 0;while (bfs())ans += dfs(s, inf);return ans;}signed main(){cin >> n >> m >> s >> t;for (int i = 1; i <= m; i ++)cin >> a >> b >> c, add();return cout << dinic(), 0;}```:::### 最小费用最大流:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 5e3 + 10, M = 1e5 + 10, inf = 1ll << 31 - 1;bool vis[N];struct edge{int to, w, c, nxt;}e[M];int n, m, s, t, a, b, w, c, cnt = 1, ans, d[N], cur[N], h[N], mic;void add(){e[++ cnt] = {b, w, c, h[a]}, h[a] = cnt,e[++ cnt] = {a, 0, -c, h[b]}, h[b] = cnt;}bool spfa(){for (int i = 1; i <= n; i ++)vis[i] = 0, d[i] = inf, cur[i] = h[i];queue<int> q;q.push(s), d[s] = 0, vis[s] = 1;while (q.size()){int u = q.front();q.pop(), vis[u] = 0;for (int i = h[u], v; i; i = e[i].nxt)if (d[v = e[i].to] > d[u] + e[i].c && e[i].w > 0){d[v] = d[u] + e[i].c;if (!vis[v])q.push(v), vis[v] = 1;}}return d[t] != inf;}int dfs(int u, int f){if (u == t)return f;vis[u] = 1;int sum = 0;for (int i = cur[u], v; i && f > 0; i = e[i].nxt){cur[u] = i, v = e[i].to;if (d[v] == d[u] + e[i].c && e[i].w > 0 && !vis[v]){int nf = dfs(v, min(f, e[i].w));e[i].w -= nf, e[i ^ 1].w += nf,f -= nf, sum += nf, mic += nf * e[i].c;}}vis[u] = 0;if (!sum)d[u] = inf;return sum;}signed main(){ios::sync_with_stdio(0), cin.tie(0),cin >> n >> m >> s >> t;for (int i = 1; i <= m; i ++)cin >> a >> b >> w >> c, add();while (spfa())ans += dfs(s, inf);return cout << ans << " " << mic, 0;}```:::## 线性筛欧拉函数:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 4e4 + 10;bool vis[N];int n, cnt, p[N], phi[N], s;int main(){phi[1] = s = 1, cin >> n;for (int i = 2; i < n; i ++){if (!vis[i])vis[i] = 1, p[++ cnt] = i, phi[i] = i - 1;for (int j = 1; j <= cnt; j ++){if (i * p[j] > N)break;vis[i * p[j]] = 1;if (i % p[j] == 0){phi[i * p[j]] = phi[i] * p[j];/*假设 i = p[j] ^ n * b,则 i * p[j] = p[j] ^ {n + 1} * bphi[i] = phi[p[j] ^ n] * phi[b]phi[p[j] ^ n] = p[j] ^ n - p[j] ^ {n - 1}= p[j] * (p[j] ^ {n - 1} - p[j] ^ {n - 2}= p[j] * phi[p[j] ^ {n - 1}]则 phi[i * p[j]] = p[j] * phi[p[j] ^ n] * phi[b]= p[j] * phi[i]*/break;}phi[i * p[j]] = phi[i] * phi[p[j]];}if (i < n)s += phi[i];}return cout << (n == 1 ? 0 : s << 1 | 1), 0;}```:::## 拓展欧拉定理:::success[code]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int a, phi, m, b, mod;bool flag;string s;int fpow(int x, int y){int res = 1;for (; y; y >>= 1, (x *= x) %= mod)if (y & 1)(res *= x) %= mod;return res;}signed main(){ios::sync_with_stdio(0), cin.tie(0);cin >> a >> m >> s;mod = phi = m;for (int i = 2; i * i <= m; i ++)if (m % i == 0){phi = phi * (i - 1) / i;while (m % i == 0)m /= i;}if (m > 1)phi = phi * (m - 1) / m;for (int i = 0, l = s.size(); i < l; i ++)b = ((b << 3) + (b << 1) + (s[i] ^ 48)), flag |= b >= phi, b %= phi;return cout << fpow(a, b % phi + (flag ? phi : 0)), 0;}```:::## 点分治:::success[code]```cpp#include <bits/stdc++.h>using namespace std;const int N = 10010, M = 1e7 + 10;int n, m, rt, sz[N], d[N], l, ms[N], tot;bool vis[N], ans[M];vector<pair<int, int> > v[N];void gets(int u, int f){sz[u] = 1, ms[u] = 0;for (auto [e, k] : v[u])if (e != f && !vis[e])gets(e, u), sz[u] += sz[e], ms[u] = max(ms[u], sz[e]);}void getr(int u, int f){if ((ms[u] = max(ms[u], tot - sz[u])) <= tot / 2)rt = u;for (auto [e, k] : v[u])if (e != f && !vis[e])getr(e, u);}void dfs(int u, int f, int dis){d[++ l] = dis;for (auto [e, k] : v[u])if (e != f && !vis[e])dfs(e, u, dis + k);}void calc(int u){vector<int> all;for (auto [e, k] : v[u])if(!vis[e]){l = 0, dfs(e, u, k);for (int i = 1; i <= l; i ++)for (auto x : all)if (d[i] + x < M)ans[d[i] + x] = 1;for (int i = 1; i <= l; i ++)if (d[i] < M)ans[d[i]] = 1, all.push_back(d[i]);}}void divide(int u){gets(u, -1), tot = sz[u], getr(u, -1), vis[rt] = 1, calc(rt);for (auto [e, k] : v[rt])if (!vis[e])divide(e);}int main(){ios::sync_with_stdio(0), cin.tie(0), cin >> n >> m;for (int i = 1, a, b, c; i < n; i ++)cin >> a >> b >> c,v[a].push_back({b, c}), v[b].push_back({a, c});divide(1);while (m -- && cin >> l)cout << (ans[l] ? "AYE" : "NAY") << '\n';return 0;}```:::## 线段树合并:::success[code]```cpp//永无乡#include <bits/stdc++.h>using namespace std;const int N = 100010, M = N << 5;int n, m, q, tot, v, w[N], fa[N], rt[N];struct segmenT{int ls, rs, cnt;}t[M];void insert(int &u, int l, int r, int x){if (!u)u = ++ tot;t[u].cnt ++;if (l == r)return;int mid = l + r >> 1;(x <= mid ? insert(t[u].ls, l, mid,x) : insert(t[u].rs, mid + 1, r, x));}int merge(int L, int R, int l, int r){if (!L || !R)return L + R;if (l == r)return t[L].cnt += t[R].cnt, L;int mid = l + r >> 1;t[L].ls = merge(t[L].ls, t[R].ls, l, mid),t[L].rs = merge(t[L].rs, t[R].rs, mid + 1, r),t[L].cnt += t[R].cnt;return L;}int query(int u, int l, int r, int k){if (l == r)return l;int mid = l + r >> 1, ns = t[u].ls;return t[ns].cnt >= k ? query(ns, l, mid, k): query(t[u].rs, mid + 1, r, k - t[ns].cnt);}int find(int u){return fa[u] == u ? u : fa[u] = find(fa[u]);}int main(){ios::sync_with_stdio(0), cin.tie(0), cin >> n >> m;for (int i = 1; i <= n && cin >> v; i ++)w[v] = i, fa[i] = i, insert(rt[i], 1, n, v);for (int i = 1, x, y; i <= m; i ++){cin >> x >> y;int fx = find(x), fy = find(y);if (fx != fy)rt[fx] = merge(rt[fx], rt[fy], 1, n), fa[fy] = fx;}cin >> q;char op;int x, y;while (q -- && cin >> op >> x >> y)if (op == 'Q')cout << (t[rt[find(x)]].cnt < y ? -1 : w[query(rt[fa[x]], 1, n, y)]) << '\n';else if (find(x) != find(y))rt[fa[x]] = merge(rt[fa[x]], rt[fa[y]], 1, n), fa[fa[y]] = fa[x];return 0;}```:::12344 行未改动91011121314151617181920212224 行未改动4748495051525354555657585960616263646524 行未改动90919293## BSGS### BSGS(Baby Step,Gaint Step)算法给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。4 行未改动于是把所有 $b \times a ^ j \bmod p$ 放入一个哈希表,再遍历 $i$ 查找 $a ^ {i \times t} \bmod p$ 即可做到 $\sqrt p$ 实现啦!注意特判若 $a = 0$ 且 $b \ne 0$ 时无解。:::success[code]```cpp#include <bits/stdc++.h>using namespace std;struct custom_hash{static uint64_t splitmix64(uint64_t x){x += 0x9e3779b97f4a7c15;x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;x=(x ^ (x >> 27)) * 0x94d049bb133111eb;24 行未改动return BSGS(b, n, p), 0;}```:::### exBSGS 算法若 $a$ 与 $p$ 不互质,则需要不断将 $a$、$b$ 和 $p$ 除去 $\gcd(a, \ p)$,其中若出现 $b$ 有余数的情况则必然无解。记每次的 $\gcd(a_i, \ p_i) = d_i$,则得到:$$\prod \limits _ {i = 1} ^ k \frac{a}{d_i} · a ^ {x - k} \equiv \frac{b}{\prod \limits _ {i = 1} ^ k d_i} \pmod p$$用 BSGS 求解即可。:::success[code]```cpp#include <bits/stdc++.h>#define int long long24 行未改动return 0;}```:::