正文
增加 524 行删除 276 行
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636467 行未改动1321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751769 行未改动1861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133149 行未改动32432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645713 行未改动4714724734744754764774784799 行未改动48949049149249349449549649749849950050150250350450550650750827 行未改动5365375385395405415425435445455465475485495505515525535545555568 行未改动56556656756856957057157257368 行未改动642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714363 行未改动全部展开10781079108010811082108310841085132 行未改动全部展开1218121912201221## 0. 火车头```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> pii;typedef pair<ll, ll> pll;const int M = 1000005;const int I = 0x3f3f3f3f;const ll F = 0x3f3f3f3f3f3f3f3fLL;const int G = 20;```---## 1. 存储结构### 1.1 邻接表存图```cppint n;vector<vector<int>> g(M + 1);vector<vector<pair<int, int>>> wg(M + 1);// 无权:g[x].push_back(y)// 带权:wg[x].push_back({y, w})```### 1.2 链式前向星```cppint hd[M];int to[2 * M];int wt[2 * M];int nx[2 * M];int ct = 0;void init_cfs(int n) {for (int i = 1; i <= n; i++) hd[i] = -1;ct = 0;}void add_edge(int u, int v, int w) {to[++ct] = v;wt[ct] = w;nx[ct] = hd[u];hd[u] = ct;}void add_unedge(int u, int v, int w) {add_edge(u, v, w);add_edge(v, u, w);}```### 1.3 LCT```cppconst int LN = 3e5+5;struct node {int fa, ch[2], sum, val, lazy;}t[N];#define lc t[x].ch[0]#define rc t[x].ch[1]bool isroot(int x) {67 行未改动---## 2. 图论算法### 2.1 DFS```cppbool vs[M];void dfs(int u) {if (vs[u]) return;vs[u] = true;for (int v : g[u]) {if (!vs[v]) dfs(v);}}```### 2.2 BFS```cppbool vs[M];void bfs(int s) {queue<int> q;q.push(s);vs[s] = true;while (!q.empty()) {int u = q.front();q.pop();for (int v : g[u]) {if (!vs[v]) {vs[v] = true;q.push(v);}}}}```### 2.3 拓扑排序```cppvector<int> id(M + 1, 0);void topo() {queue<int> q;for (int i = 1; i <= n; i++) {9 行未改动}}```### 2.4 堆优化 Dijkstra```cppint ds[M];bool vs[M];void dijkstra(int s) {fill(ds, ds + n + 1, I);fill(vs, vs + n + 1, false);priority_queue<pair<int, int>,vector<pair<int, int>>,greater<pair<int, int>>> pq;ds[s] = 0;pq.push({0, s});while (!pq.empty()) {auto [d, u] = pq.top();pq.pop();if (d != ds[u]) continue;if (vs[u]) continue;vs[u] = true;for (auto [v, w] : wg[u]) {if (!vs[v] && ds[v] > ds[u] + w) {ds[v] = ds[u] + w;pq.push({ds[v], v});}}}}```### 2.5 并查集 DSU```cppint pa[M], rk[M];void init_dsu(int n) {for (int i = 1; i <= n; i++) {pa[i] = i;rk[i] = 0;}}int find(int x) {return pa[x] == x ? x : pa[x] = find(pa[x]);}bool unite(int x, int y) {int rx = find(x), ry = find(y);if (rx == ry) return false;if (rk[rx] < rk[ry]) swap(rx, ry);pa[ry] = rx;if (rk[rx] == rk[ry]) rk[rx]++;return true;}```### 2.6 Kruskal 最小生成树```cppstruct E {int u, v, w;bool operator<(const E& o) const {return w < o.w;}};ll kruskal(vector<E>& es) {sort(es.begin(), es.end());init_dsu(n);ll mw = 0;int ec = 0;for (const E& e : es) {if (unite(e.u, e.v)) {mw += e.w;if (++ec == n - 1) return mw;}}return -1;}```### 2.7 SPFA 判负环```cppbool iq[M];int cs[M];// true 无负环,false 有负环bool spfa(int s) {fill(ds, ds + n + 1, I);fill(iq, iq + n + 1, false);fill(cs, cs + n + 1, 0);queue<int> q;ds[s] = 0;q.push(s);iq[s] = true;while (!q.empty()) {int u = q.front();q.pop();iq[u] = false;for (auto [v, w] : wg[u]) {if (ds[v] > ds[u] + w) {ds[v] = ds[u] + w;cs[v] = cs[u] + 1;if (cs[v] >= n) return false;if (!iq[v]) {q.push(v);iq[v] = true;}}}}return true;}```### 2.8 Floyd 全源最短路```cppconst int FN = 505;int d[FN][FN];void init_floyd(int n) {for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)d[i][j] = (i == j) ? 0 : I;9 行未改动}}```### 2.9 Johnson 全源最短路```cppvector<vector<int>> johnson() {vector<int> h(n + 1, 0), ct(n + 1, 0);vector<bool> iq(n + 1, false);queue<int> q;for (int i = 1; i <= n; i++) {q.push(i);iq[i] = true;}while (!q.empty()) {int u = q.front();q.pop();iq[u] = false;for (auto [v, w] : wg[u]) {if (h[v] > h[u] + w) {h[v] = h[u] + w;ct[v] = ct[u] + 1;if (ct[v] >= n) return {};if (!iq[v]) {q.push(v);iq[v] = true;}}}}vector<vector<pair<int, int>>> ng(n + 1);for (int u = 1; u <= n; u++)for (auto [v, w] : wg[u])ng[u].push_back({v, w + h[u] - h[v]});vector<vector<int>> rs(n + 1, vector<int>(n + 1, I));swap(wg, ng);for (int s = 1; s <= n; s++) {dijkstra(s);for (int i = 1; i <= n; i++)if (ds[i] < I) rs[s][i] = ds[i] - h[s] + h[i];}swap(wg, ng);return rs;}```### 2.10 Trie```cppstruct Trie {static const int ALPHA = 26;vector<array<int, ALPHA>> nxt;vector<int> pc, ec;Trie(int rn = 0) { init(rn); }void init(int rn = 0) {nxt.clear();pc.clear();ec.clear();nxt.reserve(rn + 1);pc.reserve(rn + 1);ec.reserve(rn + 1);nxt.push_back({});pc.push_back(0);ec.push_back(0);}int new_node() {nxt.push_back({});pc.push_back(0);ec.push_back(0);return (int)nxt.size() - 1;}static int id(char ch) {return ch - 'a';}void insert(const string& s) {int u = 0;pc[u]++;for (char ch : s) {int c = id(ch);if (!nxt[u][c]) nxt[u][c] = new_node();u = nxt[u][c];pc[u]++;}ec[u]++;}int count(const string& s) const {int u = 0;for (char ch : s) {int c = id(ch);if (!nxt[u][c]) return 0;u = nxt[u][c];}return ec[u];}int count_prefix(const string& s) const {int u = 0;for (char ch : s) {int c = id(ch);if (!nxt[u][c]) return 0;u = nxt[u][c];}return pc[u];}void erase(const string& s) {int u = 0;pc[u]--;for (char ch : s) {int c = id(ch);u = nxt[u][c];pc[u]--;}ec[u]--;}int size() const {return (int)nxt.size();}};```### 2.11 网络最大流```cppconst int NV = 250;ll graph[NV][NV], pre[NV], flow[NV];ll bfs_flow(int s, int t) {memset(pre, -1, sizeof(pre));flow[s] = F;pre[s] = 0;13 行未改动}if (pre[t] == -1) return -1;return flow[t];}ll maxflow(int s, int t) {ll mf = 0;while (1) {ll f = bfs_flow(s, t);9 行未改动}return mf;}```---## 3. 树### 3.1 倍增法 LCA```cppvector<int> aj[M];int dep[M];int up[M][G];int lca(int a, int b) {if (dep[a] < dep[b]) swap(a, b);int df = dep[a] - dep[b];for (int j = 0; j < G; ++j)27 行未改动```---## 4. 区间查询### 4.1 ST 表 RMQ```cppint st[M][G];int lg[M];void init_st(int a[], int n) {lg[1] = 0;for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;for (int i = 1; i <= n; i++) st[i][0] = a[i];for (int k = 1; k < G; k++)for (int i = 1; i + (1 << k) - 1 <= n; i++)st[i][k] = max(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);// 查 min 把 max 改成 min}8 行未改动```cppint a[M];int tr[4 * M];int lz[4 * M];void build(int nd, int l, int r) {if (l == r) {tr[nd] = a[l];return;68 行未改动```cppint a[M];int as[M];int sk[M], tp;void ngr(int n) {tp = 0;for (int i = n; i >= 1; i--) {while (tp > 0 && a[sk[tp]] <= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void nsr(int n) {tp = 0;for (int i = n; i >= 1; i--) {while (tp > 0 && a[sk[tp]] >= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void ngl(int n) {tp = 0;for (int i = 1; i <= n; i++) {while (tp > 0 && a[sk[tp]] <= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void nsl(int n) {tp = 0;for (int i = 1; i <= n; i++) {while (tp > 0 && a[sk[tp]] >= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}```---## 5. 序列和排序### 5.1 归并排序求逆序对```cppll merge(vector<int>& a, vector<int>& tp, int l, int m, int r) {int i = l, j = m + 1, k = l;ll c = 0;while (i <= m && j <= r) {if (a[i] <= a[j]) {tp[k++] = a[i++];} else {c += m - i + 1;tp[k++] = a[j++];}}while (i <= m) tp[k++] = a[i++];while (j <= r) tp[k++] = a[j++];for (int p = l; p <= r; p++) a[p] = tp[p];return c;}ll mergeSort(vector<int>& a, vector<int>& tp, int l, int r) {if (l >= r) return 0;int m = l + (r - l) / 2;ll c = mergeSort(a, tp, l, m) + mergeSort(a, tp, m + 1, r);c += merge(a, tp, l, m, r);return c;}```### 5.2 贪心版 LIS363 行未改动全部展开}return ans;}```## 8. 计算几何### 8.1 三维凸包132 行未改动全部展开return fabs(res) / 6.0;}};```1234567891011121314151617181920212223242526272829303132333467 行未改动1021031041051061071081091101111129 行未改动1221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412429 行未改动25225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330413 行未改动3183193203213223233243259 行未改动33533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637727 行未改动4054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785798 行未改动58858959059159259359459568 行未改动664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766363 行未改动全部展开1130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172132 行未改动全部展开130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469## 1. 数据结构### 1.1 邻接表存图```cppint n;vector<vector<int>> g(M + 1);vector<vector<pair<int, int>>> g(M + 1);// 无权:g[x].push_back(y)// 带权:g[x].push_back({y, w})```### 1.2 链式前向星```cppint head[N], cnt = 0;struct Edge {int to, next}edge[N];void init() {for (int i=0; i < N << 1; i++) edge[i].next = -1, head[i] = -1;cnt = 0;}void addedge(int u, int v) {edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}```### 1.3 LCT```cppconst int N = 3e5+5;struct node {int fa, ch[2], sum, val, lazy;}t[N];#define lc t[x].ch[0]#define rc t[x].ch[1]bool isroot(int x) {67 行未改动---## 2. 图论算法### 2.1 拓扑排序```cppvector<int> id(M + 1, 0);void topo() {queue<int> q;for (int i = 1; i <= n; i++) {9 行未改动}}```### 2.2 堆优化 Dijkstra```cppconst int INF = 0x3f3f3f3f;int dist[MAXN];bool vis[MAXN];void dijkstra(int s, int n) {memset(dist, 0x3f, sizeof(int) * (n + 1));memset(vis, 0, sizeof(bool) * (n + 1));priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;dist[s] = 0;pq.push({0, s});while (!pq.empty()) {auto [d, u] = pq.top(); pq.pop();if (vis[u]) continue;vis[u] = true;for (auto [v, w] : g[u]) {if (!vis[v] && dist[v] > dist[u] + w) {dist[v] = dist[u] + w;pq.push({dist[v], v});}}}}```### 2.3 并查集 DSU```cppint fa[MAXN], rk[MAXN];void init(int n) {for (int i = 1; i <= n; i++) {fa[i] = i;rk[i] = 0;}}int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}bool unite(int x, int y) {int rx = find(x), ry = find(y);if (rx == ry) return false;if (rk[rx] < rk[ry]) swap(rx, ry);fa[ry] = rx;if (rk[rx] == rk[ry]) rk[rx]++;return true;}```### 2.4 Kruskal 最小生成树```cppstruct Edge {int u, v, w;bool operator<(const Edge& other) const {return w < other.w;}};vector<Edge> edges;long long kruskal(int n) {sort(edges.begin(), edges.end());init(n);long long mst = 0;int cnt = 0;for (const Edge& e : edges) {if (unite(e.u, e.v)) {mst += e.w;cnt++;if (cnt == n - 1) return mst;}}return -1;}```### 2.5 SPFA 判负环```cppbool inq[N];int dis[N], cnt[N];bool spfa(int s) {fill(dis, dis + n + 1, INF);fill(inq, inq + n + 1, false);fill(cnt, cnt + n + 1, 0);queue<int> q;dis[s] = 0;q.push(s);inq[s] = true;while (!q.empty()) {int u = q.front();q.pop();inq[u] = false;for (auto [v, w] : g[u]) {if (dis[v] > dis[u] + w) {dis[v] = dis[u] + w;cnt[v] = cnt[u] + 1;if (cnt[v] >= n) return false;if (!inq[v]) {q.push(v);inq[v] = true;}}}}return true;}```### 2.6 Floyd 全源最短路```cppconst int N = 505;int d[N][N];void init_floyd(int n) {for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)d[i][j] = (i == j) ? 0 : I;9 行未改动}}```### 2.7 Johnson 全源最短路```cppvector<vector<int>> johnson() {vector<int> h(n + 1, 0), cnt(n + 1, 0);vector<bool> inq(n + 1, false);queue<int> q;for (int i = 1; i <= n; i++) {q.push(i);inq[i] = true;}while (!q.empty()) {int u = q.front();q.pop();inq[u] = false;for (auto [v, w] : g[u]) {if (h[v] > h[u] + w) {h[v] = h[u] + w;cnt[v] = cnt[u] + 1;if (cnt[v] >= n) return {};if (!inq[v]) {q.push(v);inq[v] = true;}}}}vector<vector<pair<int, int>>> ng(n + 1);for (int u = 1; u <= n; u++) for (auto [v, w] : g[u]) ng[u].push_back({v, w + h[u] - h[v]});vector<vector<int>> rs(n + 1, vector<int>(n + 1, INF));swap(g, ng);for (int s = 1; s <= n; s++) {dijkstra(s, n);for (int i = 1; i <= n; i++) if (dist[i] < INF) rs[s][i] = dist[i] - h[s] + h[i];}swap(g, ng);return rs;}```### 2.8 网络最大流```cppconst int N = 250;ll graph[N][N], pre[N], flow[N];ll bfs_flow(int s, int t) {memset(pre, -1, sizeof(pre));flow[s] = F;pre[s] = 0;13 行未改动}if (pre[t] == -1) return -1;return flow[t];}ll maxflow(int s, int t) {ll mf = 0;while (1) {ll f = bfs_flow(s, t);9 行未改动}return mf;}```----### 2.9 割点```cppconst int N = 2e4+5;int low[N], num[N], dfn;bool iscut[N];vector<int> G[N];void dfs(int u, int fa) {low[u] = num[u] = ++dfn;int child = 0;for (int i = 0; i < G[u].size(); i++) {int v = G[u][i];if (!num[v]) {child++;dfs(v, u);low[u] = min(low[u], low[v]);if (low[v] >= num[u] && fa != 0) iscut[u] = 1;}else if (num[v] < num[u] && v != fa) low[u] = min(low[u], num[v]);}if (fa == 0 && child >= 2) iscut[u] = 1;}```---## 3. 树### 3.1 倍增法 LCA```cppvector<int> g[N];int dep[N];int up[N][20];int lca(int a, int b) {if (dep[a] < dep[b]) swap(a, b);int df = dep[a] - dep[b];for (int j = 0; j < G; ++j)27 行未改动```---### 3.2 树链剖分```cppstruct Edge {int to, next;}edge[N << 1];int head[N << 1], cnt, w[N], w_new[N], tree[N << 2], tag[N << 2], n, m, r, mod, son[N], id[N], fa[N], deep[N], siz[N], top[N], num;void init() {for (int i=0; i < N << 1; i++) edge[i].next = -1, head[i] = -1;cnt = 0;}void addedge(int u, int v) {edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}int ls(int x) {return x << 1;}int rs(int x) {return x << 1 | 1;}void addtag(int p, int pl, int pr ,int d) {tag[p] += d; tree[p] += (d * (pr - pl + 1)) % mod;}void pushup(int p) {tree[p] = 1LL * tree[ls(p)] + tree[rs(p)] % mod;}void pushdown(int p, int pl, int pr) {if (tag[p]) {int mid = (pl + pr) >> 1;addtag(ls(p), pl, mid, tag[p]);addtag(rs(p), mid+1, pr, tag[p]);tag[p] = 0;}}void build(int p, int pl, int pr) {tag[p] = 0;if (pl == pr) {tree[p] = w_new[pl] % mod; return;}int mid = (pl + pr) >> 1;build(ls(p), pl, mid);build(rs(p), mid+1, pr);pushup(p);}void update(int L, int R, int p, int pl, int pr, int d) {if (L <= pl && pr <= R) {addtag(p, pl, pr, d); return;}pushdown(p, pl, pr);int mid = (pl + pr) >> 1;if (L <= mid) update(L, R, ls(p), pl, mid, d);if (R > mid) update(L, R, rs(p), mid+1, pr, d);pushup(p);}int query(int L, int R, int p, int pl, int pr) {if (pl >= L && R >= pr) return tree[p] % mod;pushdown(p, pl, pr);int res = 0, mid = (pl + pr) >> 1;if (L <= mid) res += query(L, R, ls(p), pl, mid) % mod;if (R > mid) res += query(L, R, rs(p), mid+1, pr) % mod;return res % mod;}void dfs1(int x, int fa) {deep[x] = deep[fa] + 1;::fa[x] = fa;siz[x] = 1;for (int i = head[x]; ~i; i = edge[i].next) {int y = edge[i].to;if (y != fa) {::fa[y] = x;dfs1(y, x);siz[x] += siz[y];if (!son[x] || siz[son[x]] < siz[y]) son[x] = y;}}}void dfs2(int x, int topx) {id[x] = ++num;w_new[num] = w[x];top[x] = topx;if (!son[x]) return;dfs2(son[x], topx);for (int i = head[x]; ~i; i = edge[i].next) {int y = edge[i].to;if (y != fa[x] && y != son[x]) dfs2(y, y);}}void update_range(int x, int y, int z) {while (top[x] != top[y]) {if (deep[top[x]] < deep[top[y]]) swap(x, y);update(id[top[x]], id[x], 1, 1, n, z);x = fa[top[x]];}if (deep[x] > deep[y]) swap(x, y);update(id[x], id[y], 1, 1, n, z);}int query_range(int x, int y) {int ans = 0;while (top[x] != top[y]) {if (deep[top[x]] < deep[top[y]]) swap(x, y);ans += query(id[top[x]], id[x], 1, 1, n);ans %= mod;x = fa[top[x]];}if (deep[x] > deep[y]) swap(x, y);ans += query(id[x], id[y], 1, 1, n);return ans % mod;}void update_tree(int x, int k) {update(id[x], id[x] + siz[x] - 1, 1, 1, n, k);}int query_tree(int x) {return query(id[x], id[x] + siz[x] - 1, 1, 1, n);}```### 3.3 文艺平衡树```cppconst int N = 1e5+5;int cnt, root;struct Node {int ls, rs, num, pri, siz, lzy;}t[N];void newnode(int x) {t[++cnt].siz = 1, t[cnt].ls = t[cnt].rs = 0, t[cnt].pri = rand(), t[cnt].num = x;}void update(int u) {t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;}void pushdown(int u) {if (t[u].lzy) {swap(t[u].ls, t[u].rs);t[t[u].ls].lzy ^= 1, t[t[u].rs].lzy ^= 1;t[u].lzy = 0;}}void Split(int u, int x, int& L, int& R) {if (u == 0) {L = R = 0; return;}pushdown(u);if (t[t[u].ls].siz + 1 <= x) {L = u; Split(t[u].rs, x-t[t[u].ls].siz-1, t[u].rs, R);} else {R = u; Split(t[u].ls, x, L, t[u].ls);}update(u);}int Merge(int L, int R) {if (L == 0 || R == 0) return L+R;if (t[L].pri > t[R].pri) {pushdown(L);t[L].rs = Merge(t[L].rs, R); update(L); return L;} else {pushdown(R);t[R].ls = Merge(L, t[R].ls); update(R); return R;}}void show(int u) {if (!u) return;pushdown(u);show(t[u].ls); printf("%d ", t[u].num); show(t[u].rs);}/*int x, y; scanf("%d%d", &x, &y);int L, R, p;Split(root, y, L, R);Split(L, x-1, L, p);t[p].lzy ^= 1;root = Merge(Merge(L, p), R);*/```----## 4. 区间查询### 4.1 ST 表 RMQ```cppint st[N][20];int lg[N];int a[N];void init_st(int n) {lg[1] = 0;for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;for (int i = 1; i <= n; i++) st[i][0] = a[i];for (int k = 1; k < 20; k++)for (int i = 1; i + (1 << k) - 1 <= n; i++)st[i][k] = max(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);// 查 min 把 max 改成 min}8 行未改动```cppint a[M];int tr[4 * M];int lz[4 * M];void build(int nd, int l, int r) {if (l == r) {tr[nd] = a[l];return;68 行未改动```cppint a[M];int as[M];int sk[M], tp;// 记得memset(as, 0, sizeof(as));void ngr(int n) { // 右边第一个比他大的tp = 0;for (int i = n; i >= 1; i--) {while (tp > 0 && a[sk[tp]] <= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void nsr(int n) { // 右边第一个比他小的tp = 0;for (int i = n; i >= 1; i--) {while (tp > 0 && a[sk[tp]] >= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void ngl(int n) { // 左边第一个比他大的tp = 0;for (int i = 1; i <= n; i++) {while (tp > 0 && a[sk[tp]] <= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}void nsl(int n) { // 左边第一个比他小的tp = 0;for (int i = 1; i <= n; i++) {while (tp > 0 && a[sk[tp]] >= a[i]) tp--;as[i] = (tp > 0) ? sk[tp] : 0;sk[++tp] = i;}}```### 4.5 主席树```cppconst int N = 2e5 + 5;int cnt = 0;int a[N], b[N], root[N];struct {int L, R, sum;}tree[N << 5];int build(int pl, int pr) {int rt = ++cnt;tree[rt].sum = 0;int mid = (pl + pr) >> 1;if (pl < pr) tree[rt].L = build(pl, mid), tree[rt].R = build(mid + 1, pr);return rt;}int update(int pre, int pl, int pr, int x) {int rt = ++cnt;tree[rt].L = tree[pre].L;tree[rt].R = tree[pre].R;tree[rt].sum = tree[pre].sum + 1;int mid = (pl + pr) >> 1;if (pl < pr) {if (x <= mid) tree[rt].L = update(tree[pre].L, pl, mid, x);else tree[rt].R = update(tree[pre].R, mid+1, pr, x);}return rt;}int query(int u, int v, int pl, int pr, int k) {if (pl == pr) return pl;int x = tree[tree[v].L].sum - tree[tree[u].L].sum;int mid = (pl + pr) >> 1;if (k <= x) return query(tree[u].L, tree[v].L, pl, mid, k);else return query(tree[u].R, tree[v].R, mid+1, pr, k - x);}// how to use: query(root[x - 1], root[y], 1, size, k);// print(b[t])```---## 5. 序列和排序### 5.1 归并排序求逆序对```cppconst int N = 300005;int a[N], tmp[N];ll mergesort(int l, int r) {if (l >= r) return 0;int m = l + (r - l) / 2;ll c = mergesort(l, m) + mergesort(m + 1, r);int i = l, j = m + 1, k = l;while (i <= m && j <= r) {if (a[i] <= a[j]) tmp[k++] = a[i++];else { c += m - i + 1; tmp[k++] = a[j++]; }}while (i <= m) tmp[k++] = a[i++];while (j <= r) tmp[k++] = a[j++];for (int p = l; p <= r; p++) a[p] = tmp[p];return c;}```### 5.2 贪心版 LIS363 行未改动全部展开}return ans;}```### 7.10 矩阵快速幂```cppconst int N = 100, mod = 1e9 + 7;struct Matrix {int a[N + 5][N + 5];Matrix(){memset(a, 0, sizeof(a));}Matrix operator * (const Matrix &b) {Matrix c;for(int i = 1; i <= N; i++) {for(int k = 1; k <= N; k++) {if (a[i][k] == 0) continue;for(int j = 1; j <= N; j++) {c.a[i][j] = (c.a[i][j] + 1LL * a[i][k] * b.a[k][j]) % mod;}}}return c;}};Matrix qpow(Matrix a, long long exp, int n) {Matrix res;for (int i = 1; i <= n; i++)res.a[i][i] = 1;while (exp > 0) {if (exp & 1) {res = res * a;}a = a * a;exp >>= 1;}return res;}```## 8. 计算几何### 8.1 三维凸包132 行未改动全部展开return fabs(res) / 6.0;}};```### 7.2 最小球覆盖```cppstruct Point {double x, y, z;Point() {}Point(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}} p[MAXN];double dist2(Point a, Point b) {return ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z));}double solve() {double ans = INF;Point cur = {0, 0, 0};double T = start;while (T > eps) {int f = 1;rep(1, n, 1) if (dist2(p[i], cur) > dist2(p[f], cur)) f = i;ans = min(ans, sqrt(dist2(cur, p[f])));cur.x += (p[f].x - cur.x) * (T / start);cur.y += (p[f].y - cur.y) * (T / start);cur.z += (p[f].z - cur.z) * (T / start);T *= rate;}return ans;}```### 7.3 最小圆覆盖```cppconst double eps = 1e-12;const int N = 1e5+5;#define double long doubleint sgn(double x) {if (fabsl(x) < eps) return 0;else return x < 0 ? -1 : 1;}struct Point {double x, y;}p[N];double Distance(Point A, Point B) {return hypotl(A.x-B.x, A.y-B.y);}Point circle_center(const Point a, const Point b, const Point c) {Point center;double a1 = b.x - a.x, b1 = b.y - a.y, c1 = (a1 * a1 + b1 * b1) / 2;double a2 = c.x - a.x, b2 = c.y - a.y, c2 = (a2 * a2 + b2 * b2) / 2;double d = a1 * b2 - a2 * b1;center.x = a.x + (c1 * b2 - c2 * b1) / d;center.y = a.y + (a1 * c2 - a2 * c1) / d;return center;}void min_cover_circle(Point* p, int n, Point& c, double& r) {random_shuffle(p, p+n);c = p[0], r = 0;for (int i = 0; i < n; i++) if (sgn(Distance(p[i], c) - r) > 0) {c = p[i], r = 0;for (int j = 0; j < i; j++) if (sgn(Distance(p[j], c) - r) > 0) {c.x = (p[i].x + p[j].x) / 2;c.y = (p[i].y + p[j].y) / 2;r = Distance(p[j], c);for (int k = 0; k < j; k++) if (sgn(Distance(p[k], c) - r) > 0) {c = circle_center(p[i], p[j], p[k]);r = max({Distance(p[i], c), Distance(p[j], c), Distance(p[k], c)});}}}}```## 8. 字符串### 8.1 Trie```cppstruct Trie {static const int N = 1e5 + 5, A = 26;int son[N][A], pc[N], ec[N], tot = 0;Trie() { init(); }void init() {memset(son, 0, sizeof son);memset(pc, 0, sizeof pc);memset(ec, 0, sizeof ec);tot = 0;}static int id(char ch) { return ch - 'a'; }void insert(const string& s) {int u = 0; pc[u]++;for (char ch : s) {int c = id(ch);if (!son[u][c]) son[u][c] = ++tot;u = son[u][c]; pc[u]++;}ec[u]++;}int count(const string& s) const {int u = 0;for (char ch : s) {int c = id(ch);if (!son[u][c]) return 0;u = son[u][c];}return ec[u];}int prefix(const string& s) const {int u = 0;for (char ch : s) {int c = id(ch);if (!son[u][c]) return 0;u = son[u][c];}return pc[u];}void erase(const string& s) {int u = 0; pc[u]--;for (char ch : s) { u = son[u][id(ch)]; pc[u]--; }ec[u]--;}int size() const { return tot + 1; }};```### 8.2 AC自动机(简单版)```cppconst int N = 1e6+5;struct node {int son[26], end, fail;}t[N];int cnt;void Insert(char* s) {int now = 0;for (int i = 0; s[i]; i++) {int ch = s[i] - 'a';if (t[now].son[ch] == 0) t[now].son[ch] = ++cnt;now = t[now].son[ch];}t[now].end++;}void getFail () {queue<int> q;for (int i = 0; i < 26; i++) if (t[0].son[i]) q.push(t[0].son[i]);while (!q.empty()) {int now = q.front(); q.pop();for (int i = 0; i < 26; i++) {if (t[now].son[i]) {t[t[now].son[i]].fail = t[t[now].fail].son[i]; q.push(t[now].son[i]);}else t[now].son[i] = t[t[now].fail].son[i];}}}int query(char* s) { // 不是模式串int ans = 0, now = 0;for (int i = 0; s[i]; i++) {int ch = s[i] - 'a';now = t[now].son[ch];int tmp = now;while (tmp && t[tmp].end != -1) {ans += t[tmp].end;t[tmp].end = -1;tmp = t[tmp].fail;}}return ans;}```