正文
增加 305 行删除 82 行
25 行未改动262728293031323334155 行未改动全部展开19019119219319419519619719857 行未改动2562572582592602612622632643 行未改动26826927027127227327427527627727827928028128228328428528628728828929029129229341 行未改动335336337338339340341342343139 行未改动全部展开48348448548648748848949049149249349449549649749849950050150250350450584 行未改动590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662363 行未改动全部展开10261027102810291030103110321033133 行未改动全部展开1167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124625 行未改动### 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) {155 行未改动全部展开for (const Edge& e : edges) {if (unite(e.u, e.v)) {mst += e.w;cnt++;if (cnt == n - 1) return mst_weight;}}return -1;}57 行未改动### 2.7 Johnson 全源最短路```cppvector<vector<int>> johnson() {vector<int> dis(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);3 行未改动int u = q.front();q.pop();inq[u] = false;for (auto [v, w] : g[u]) {if (h[v] > h[u] + w) {dis[v] = dis[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, I));swap(g, ng);for (int s = 1; s <= n; s++) {dijkstra(s);for (int i = 1; i <= n; i++) if (ds[i] < INF) rs[s][i] = ds[i] - h[s] + h[i];}swap(g, ng);return rs;}41 行未改动}return mf;}```---## 3. 树139 行未改动全部展开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);}```----## 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}84 行未改动```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 三维凸包133 行未改动全部展开}};```## 8. 字符串### 8.1 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();}};```25 行未改动262728293031323334155 行未改动全部展开19019119219319419519619719857 行未改动2562572582592602612622632643 行未改动26826927027127227327427527627727827928028128228328428528628728828929029129229341 行未改动335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367139 行未改动全部展开50750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857984 行未改动664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766363 行未改动全部展开1130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172133 行未改动全部展开1306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146925 行未改动### 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) {155 行未改动全部展开for (const Edge& e : edges) {if (unite(e.u, e.v)) {mst += e.w;cnt++;if (cnt == n - 1) return mst;}}return -1;}57 行未改动### 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);3 行未改动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;}41 行未改动}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. 树139 行未改动全部展开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}84 行未改动```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 三维凸包133 行未改动全部展开}};```### 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;}```