版本比较
Nothing
正文
增加 391 行删除 177 行
101 行未改动全部展开10210310410510610710810911031 行未改动14214314414514614714814915059 行未改动210211212213214215216217147 行未改动全部展开365366367368369370371372373295 行未改动全部展开669670671672673674675676677678679680681682130 行未改动全部展开81381481581681781881982082182282382482582677 行未改动904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089302 行未改动全部展开139213931394139513961397139813991400260 行未改动全部展开16611662166316641665166616671668193 行未改动全部展开18621863186418651866186718681869187018713 行未改动1875187618771878187918801881188218837 行未改动189118921893189418951896189718981899190017 行未改动191819191920192119221923192419251926162 行未改动全部展开2089209020912092209320942095209620972098249 行未改动全部展开23482349235023512352235323542355134 行未改动全部展开24902491249224932494101 行未改动全部展开* 阶乘逆元的递推,基于 $\frac 1{(n+1)!}\times(n+1)=\frac 1{n!}$,即 $\text{inv}[i]=inv[i+1]\times(i+1)$。```cppint fac[N], inv[N];void get_fac_inv(int n) {fac[0] = 1, inv[1] = 0;for(int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % mod;inv[n] = get_inv(fac[n]);for(int i = n - 1; i > 1; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % mod;}31 行未改动for(int l = 1, r; l <= n; l = r + 1)r = n / (n / l), ans += (gsum[r] - gsum[l - 1]) * f(n / l);for(int l = 1, r; l < n; l = r + 1)r = (n - 1) / ((n - 1) / l), ans += (gsum[r] - gsum[l - 1]) * f(n / l + 1);ans += f(1) * g(n);```---59 行未改动* $(x + y)^n = \sum_{k=0}^{n} \binom{n}{k} x^{n-k} y^k$。---### [Lucas 定理](https://oi-wiki.org/math/number-theory/lucas/#lucas-%E5%AE%9A%E7%90%86)* 对于质数 $p$,$\binom{n}{k} \equiv \binom{\left\lfloor n/p \right\rfloor}{\left\lfloor k/p \right\rfloor} \binom{n \bmod p}{k \bmod p} \pmod{p}$,其中,当 $n<k$ 时,二项式系数 $\binom{n}{k}$ 规定为 0。```cpp147 行未改动全部展开### 分块> “这个东西看上去就不好维护,想想分块打暴力吧”* 优点在于其思想简洁,容易实现,常数小,暴力美学;难点在于其[衍生的算法](https://oi-wiki.org/ds/decompose/)。---### 树状数组295 行未改动全部展开* 另外就是其扩展的 Trie 图(AC 自动机)。--- ## 可持久化数据结构### 可持久化并查集* 启发式(按秩)合并,并记下操作序列。---### 可持久化线段树130 行未改动全部展开```--- ## 进阶数据结构### 哈希表> 这是一个(我们)用处不大但强大且简单(有待商榷)的数据结构* 其主要功能:通过关键码值(key)进行快速数据访问的结构。而手写哈希表主要是为了将离散的下标映射成连续的下标以达到快速、节约地进行**超大下标**的数据操作,比如状压 DP 中会产生大量非法状态的空间浪费,插头 DP 空间浪费更严重。* 手写哈希表主要是“哈希**挂**表”的形式,由于我习惯了链式前向星,所以延续了链式前向星的一些传统。77 行未改动* 树上主席树见 [**Code is Too Long!!!**](https://www.luogu.com.cn/record/212049895)---### 根号分治(更多的是思想层面)> 这一部分也是刚刚[新学的](https://www.cnblogs.com/TKXZ133/p/17772749.html),还没学[深](https://www.luogu.com.cn/article/5gtqzd4a)* 小操作暴力在线做,而大操作的**数量少**,那么对所有大操作的影响可以预处理打表,再在线暴力查表即可。---### [莫队](https://oi-wiki.org/misc/mo-algo-intro/)> 这个学的不[深](https://www.cnblogs.com/MortisLife/p/18962510)---### Splay* 混合标记超级模板。```cpp#include <bits/stdc++.h>using namespace std;const int N = 500100;struct Node {int s[2], fa, v, siz;int rev, same;int ls, rs, ma, sum;void init(int _v, int _fa) {rev = same = s[0] = s[1] = 0;fa = _fa, v = _v, sum = ma = v;ls = rs = max(0, v);siz = 1;}}t[N];int n, m, w[N];int can[N], tot, rt;void push_up(int x) {int lc = t[x].s[0], rc = t[x].s[1];t[x].siz = t[lc].siz + t[rc].siz + 1;t[x].ls = max(t[lc].ls, t[lc].sum + t[rc].ls + t[x].v);t[x].rs = max(t[rc].rs, t[rc].sum + t[lc].rs + t[x].v);t[x].ma = max(max(t[rc].ma, t[lc].ma), t[lc].rs + t[x].v + t[rc].ls);t[x].sum = t[lc].sum + t[rc].sum + t[x].v;}void push_down(int x) {int lc = t[x].s[0], rc = t[x].s[1];if(t[x].same) {t[x].same = t[x].rev = 0;if(lc) {t[lc].same = 1;t[lc].v = t[x].v, t[lc].sum = t[x].v * t[lc].siz;t[lc].ls = t[lc].rs = max(0, t[x].v * t[lc].siz);t[lc].ma = max(t[x].v * t[lc].siz, t[x].v);}if(rc) {t[rc].same = 1;t[rc].v = t[x].v, t[rc].sum = t[x].v * t[rc].siz;t[rc].ls = t[rc].rs = max(0, t[x].v * t[rc].siz);t[rc].ma = max(t[x].v * t[rc].siz, t[x].v);}} else if(t[x].rev) {t[x].rev = 0;t[lc].rev ^= 1, t[rc].rev ^= 1;swap(t[lc].ls, t[lc].rs);swap(t[rc].ls, t[rc].rs);swap(t[lc].s[0], t[lc].s[1]);swap(t[rc].s[0], t[rc].s[1]);}}void rotate(int x) {int y = t[x].fa, z = t[y].fa;bool k = x == t[y].s[1];t[z].s[t[z].s[1] == y] = x, t[x].fa = z;t[y].s[k] = t[x].s[k ^ 1], t[t[x].s[k ^ 1]].fa = y;t[x].s[k ^ 1] = y, t[y].fa = x;push_up(y); push_up(x);}void splay(int x, int a) {while(t[x].fa != a) {int y = t[x].fa, z = t[y].fa;if(z != a)if((t[z].s[1] == y) ^ (t[y].s[1] == x)) rotate(x);else rotate(y);rotate(x);}if(!a) rt = x;}int get_k(int k) {int u = rt;while(u) {push_down(u);if(t[t[u].s[0]].siz >= k) u = t[u].s[0];else if(t[t[u].s[0]].siz + 1 == k) return u;else k -= t[t[u].s[0]].siz + 1, u = t[u].s[1];}return -1;}int build(int l, int r, int fa) {int u = can[tot--];int mid = l + r >> 1;t[u].init(w[mid], fa);if(l < mid) t[u].s[0] = build(l, mid - 1, u);if(r > mid) t[u].s[1] = build(mid + 1, r, u);push_up(u);return u;}void dfs(int x) {if(t[x].s[0]) dfs(t[x].s[0]);if(t[x].s[1]) dfs(t[x].s[1]);can[++tot] = x;}int main() {scanf("%d%d", &n, &m);for(int i = N - 1; i > 0; i--) can[++tot] = i;t[0].ma = w[0] = w[n + 1] = -1e9;for(int i = 1; i <= n; i++) scanf("%d", &w[i]);rt = build(0, n + 1, 0);//建splaywhile(m--) {char mode[20]; scanf("%s", mode);if(mode[0] == 'I') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + 2);posi = get_k(posi + 1);splay(posi, 0); splay(r, posi);for(int i = 0; i < a; i++) scanf("%d", &w[i]);t[r].s[0] = build(0, a - 1, r);push_up(r); push_up(posi);}if(mode[0] == 'D') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);dfs(t[r].s[0]);t[r].s[0] = 0;push_up(r); push_up(posi);}if(mode[2] == 'K') {int posi, a, c; scanf("%d%d%d", &posi, &a, &c);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);int s = t[r].s[0];t[s].same = 1, t[s].v = c, t[s].sum = c * t[s].siz;t[s].ls = t[s].rs = max(0, c * t[s].siz);t[s].ma = max(c * t[s].siz, c);push_up(r); push_up(posi);}if(mode[0] == 'R') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);int s = t[r].s[0];t[s].rev ^= 1;swap(t[s].ls, t[s].rs);swap(t[s].s[0], t[s].s[1]);push_up(r); push_up(posi);}if(mode[0] == 'G') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);printf("%d\n", t[t[r].s[0]].sum);}if(mode[2] == 'X') printf("%d\n", t[rt].ma);}return 0;}```--- 302 行未改动全部展开friend bool operator < (const Node & a, const Node & b) {return a.dis > b.dis;}};int dis[N];bool vis[N];void dji(int s) {memset(vis, 0, sizeof vis);memset(dis, 63, sizeof dis);priority_queue<Node> q;dis[s] = 0, q.push({s, 0});260 行未改动全部展开---### 欧拉(回)路* [无需多言](https://oi-wiki.org/graph/euler/),题目中,注意“整个城市所有的道路都是双向车道,道路的两个方向均需要...”等字眼。---193 行未改动全部展开* 关于网络流的典例那就不得不单独开一块了。* [建模得到更复杂的板子](https://www.acwing.com/activity/content/32/)。* [具体情境中的建模](https://www.luogu.com.cn/problem/list?type=luogu&page=1&tag=332)。--- 3 行未改动## DP 部分### Bases* 首先我们要知道 DP 绝对没有所谓的“模板”。但对于一般性的 DP 问题可以有一个固定的思维方向:状态 $\to$ 阶段 $\to$ 转移。* **状态**:遇到一个陌生情境时一定不能吝啬你给它安排的状态,因为往往没思路的原因就是“状态少了,无法清晰地转移”。**但就算状态多,各个状态也要清晰、简易!(这能简化转移式、提升正确率、加快做题速度)*** 阶段:这一部分要想清楚子问题间的依赖关系,不仅要无后效性,还不能漏掉可行决策。* **转移**:写方程时要细心,或者直接手推小样例检验自己的转移方程,注意转移的先后顺序。7 行未改动* 决策数过多,或是对子问题的重复计算,使算法效率严重下降。* 冗余、**不优**的决策仍被考虑(这些决策本应被剪去不考虑)。所以要限定可转移的范围。一个实用的方法是,先尽可能压缩范围,有漏掉的再加。给一些例子:从“往前**一个**单词处”转移、仅枚举**上一个**区域转移、从“拿这**一个**物品前”转移...* [题目](https://www.cnblogs.com/luckyblock/p/13121167.html)。---17 行未改动* [插头 DP](https://ewig-milan.github.io/2025/06/13/%E6%8F%92%E5%A4%B4DP%20-%20%E5%93%88%E5%AF%86%E9%A1%BF%E5%9B%9E%E8%B7%AF%E8%AE%A1%E6%95%B0/)。* [计数 DP](https://www.luogu.com.cn/problem/P7690)。* [数位 DP](https://www.luogu.com.cn/problem/P10959)。---### 背包问题162 行未改动全部展开### 矩阵乘方优化(加速递推)* 代码易写错的前置知识,[矩阵乘法](https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95/5446029)。* 这个算法需要**观察数据范围**,因为他在一个很尴尬的处境中:数据范围小了,没必要用;数据范围大了,一眼就看得出来要用;数据范围卡得“正好要用”,可能常数太大正解过不了。所以大多数都能一眼看出来。* 这里给出递推的原理,那么“转移矩阵”的构造方法也一目了然了。249 行未改动全部展开--- ## 整体二分* 感觉很灵活,但我目前碰到的都比较板:多询问,可离线,单个询问可二分(所以其实算法本质上还是二分答案)。134 行未改动全部展开###### 下面是水话写完那确实爽了 想整体欣赏一遍!61584 > 60k 字符量,小目标也是达成了好吧~101 行未改动全部展开10210310410510610710810911031 行未改动14214314414514614714814915059 行未改动210211212213214215216217218219220221222223224225147 行未改动全部展开373374375376377378379380381295 行未改动全部展开677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956130 行未改动全部展开108710881089109010911092109310941095109610971098109911001101110211031104110511061107110877 行未改动118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254302 行未改动全部展开155715581559156015611562156315641565260 行未改动全部展开1826182718281829183018311832183318341835183618371838183918401841193 行未改动全部展开2035203620372038203920402041204220432044204520463 行未改动2050205120522053205420552056205720587 行未改动206620672068206920702071207220732074207517 行未改动209320942095209620972098209921002101162 行未改动全部展开226422652266226722682269227022712272249 行未改动全部展开2522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555134 行未改动全部展开2690269126922693269426952696269726982699270027012702270327042705270627072708101 行未改动全部展开* 阶乘逆元的递推,基于 $\frac 1{(n+1)!}\times(n+1)=\frac 1{n!}$,即 $\text{inv}[i]=inv[i+1]\times(i+1)$。```cppint fac[N], inv[N];void get_fac_inv(int n) {fac[0] = 1, inv[1] = 1;for(int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % mod;inv[n] = get_inv(fac[n]);for(int i = n - 1; i > 1; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % mod;}31 行未改动for(int l = 1, r; l <= n; l = r + 1)r = n / (n / l), ans += (gsum[r] - gsum[l - 1]) * f(n / l);for(int l = 1, r; l < n; l = r + 1)r = (n - 1) / ((n - 1) / l), ans += (gsum[r] - gsum[l - 1]) * f((n - 1) / l + 1);ans += f(1) * g(n);```---59 行未改动* $(x + y)^n = \sum_{k=0}^{n} \binom{n}{k} x^{n-k} y^k$。---### 高维前缀和* 对于二维前缀和(类比于高维),我常偏向于“利用容斥去递推”的写法,但那实际上不如“各个维度依次做前缀和”简洁,所以对于高维前缀和的写法,我们要用后者。另外,状态最好压缩成一个数表示:$\sum_{i=1}^kn^{k-i}a_i$,$n$ 为值域。* 考虑高维前缀和主要能帮我们解决什么问题:$sum(a_1,a_2,\cdots,a_k)$ 表达的是“$x_i$ 所有组合的信息并,其中 $x_i\leq a_i$”。这也是它能 $O(2^kk)$ 做子集和的原理。---### [Lucas 定理](https://oi-wiki.org/math/number-theory/lucas/#lucas-%E5%AE%9A%E7%90%86)* 对于质数 $p$,$\binom{n}{k} \equiv \binom{\left\lfloor n/p \right\rfloor}{\left\lfloor k/p \right\rfloor} \binom{n \bmod p}{k \bmod p} \pmod{p}$,其中,当 $n<k$ 时,二项式系数 $\binom{n}{k}$ 规定为 0。```cpp147 行未改动全部展开### 分块> “这个东西看上去就不好维护,想想分块打暴力吧”* 优点在于其思想简洁,容易实现,常数小,暴力美学;难点在于其[衍生的算法](https://oi-wiki.org/ds/decompose/),分块难题全是人类智慧。---### 树状数组295 行未改动全部展开* 另外就是其扩展的 Trie 图(AC 自动机)。---### 平衡树* Splay,[这题](https://www.luogu.com.cn/problem/P2042)的代码做模板。```cpp#include <bits/stdc++.h>using namespace std;const int N = 500100;struct Node {int s[2], fa, v, siz;int rev, same;int ls, rs, ma, sum;void init(int _v, int _fa) {rev = same = s[0] = s[1] = 0;fa = _fa, v = _v, sum = ma = v;ls = rs = max(0, v);siz = 1;}}t[N];int n, m, w[N];int can[N], tot, rt;void push_up(int x) {int lc = t[x].s[0], rc = t[x].s[1];t[x].siz = t[lc].siz + t[rc].siz + 1;t[x].ls = max(t[lc].ls, t[lc].sum + t[rc].ls + t[x].v);t[x].rs = max(t[rc].rs, t[rc].sum + t[lc].rs + t[x].v);t[x].ma = max(max(t[rc].ma, t[lc].ma), t[lc].rs + t[x].v + t[rc].ls);t[x].sum = t[lc].sum + t[rc].sum + t[x].v;}void push_down(int x) {int lc = t[x].s[0], rc = t[x].s[1];if(t[x].same) {t[x].same = t[x].rev = 0;if(lc) {t[lc].same = 1;t[lc].v = t[x].v, t[lc].sum = t[x].v * t[lc].siz;t[lc].ls = t[lc].rs = max(0, t[x].v * t[lc].siz);t[lc].ma = max(t[x].v * t[lc].siz, t[x].v);}if(rc) {t[rc].same = 1;t[rc].v = t[x].v, t[rc].sum = t[x].v * t[rc].siz;t[rc].ls = t[rc].rs = max(0, t[x].v * t[rc].siz);t[rc].ma = max(t[x].v * t[rc].siz, t[x].v);}} else if(t[x].rev) {t[x].rev = 0;t[lc].rev ^= 1, t[rc].rev ^= 1;swap(t[lc].ls, t[lc].rs);swap(t[rc].ls, t[rc].rs);swap(t[lc].s[0], t[lc].s[1]);swap(t[rc].s[0], t[rc].s[1]);}}void rotate(int x) {int y = t[x].fa, z = t[y].fa;bool k = x == t[y].s[1];t[z].s[t[z].s[1] == y] = x, t[x].fa = z;t[y].s[k] = t[x].s[k ^ 1], t[t[x].s[k ^ 1]].fa = y;t[x].s[k ^ 1] = y, t[y].fa = x;push_up(y); push_up(x);}void splay(int x, int a) {while(t[x].fa != a) {int y = t[x].fa, z = t[y].fa;if(z != a)if((t[z].s[1] == y) ^ (t[y].s[1] == x)) rotate(x);else rotate(y);rotate(x);}if(!a) rt = x;}int get_k(int k) {int u = rt;while(u) {push_down(u);if(t[t[u].s[0]].siz >= k) u = t[u].s[0];else if(t[t[u].s[0]].siz + 1 == k) return u;else k -= t[t[u].s[0]].siz + 1, u = t[u].s[1];}return -1;}int build(int l, int r, int fa) {int u = can[tot--];int mid = l + r >> 1;t[u].init(w[mid], fa);if(l < mid) t[u].s[0] = build(l, mid - 1, u);if(r > mid) t[u].s[1] = build(mid + 1, r, u);push_up(u);return u;}void dfs(int x) {if(t[x].s[0]) dfs(t[x].s[0]);if(t[x].s[1]) dfs(t[x].s[1]);can[++tot] = x;}int main() {scanf("%d%d", &n, &m);for(int i = N - 1; i > 0; i--) can[++tot] = i;t[0].ma = w[0] = w[n + 1] = -1e9;for(int i = 1; i <= n; i++) scanf("%d", &w[i]);rt = build(0, n + 1, 0);//建splaywhile(m--) {char mode[20]; scanf("%s", mode);if(mode[0] == 'I') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + 2);posi = get_k(posi + 1);splay(posi, 0); splay(r, posi);for(int i = 0; i < a; i++) scanf("%d", &w[i]);t[r].s[0] = build(0, a - 1, r);push_up(r); push_up(posi);}if(mode[0] == 'D') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);dfs(t[r].s[0]);t[r].s[0] = 0;push_up(r); push_up(posi);}if(mode[2] == 'K') {int posi, a, c; scanf("%d%d%d", &posi, &a, &c);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);int s = t[r].s[0];t[s].same = 1, t[s].v = c, t[s].sum = c * t[s].siz;t[s].ls = t[s].rs = max(0, c * t[s].siz);t[s].ma = max(c * t[s].siz, c);push_up(r); push_up(posi);}if(mode[0] == 'R') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);int s = t[r].s[0];t[s].rev ^= 1;swap(t[s].ls, t[s].rs);swap(t[s].s[0], t[s].s[1]);push_up(r); push_up(posi);}if(mode[0] == 'G') {int posi, a; scanf("%d%d", &posi, &a);int r = get_k(posi + a + 1);posi = get_k(posi);splay(posi, 0); splay(r, posi);printf("%d\n", t[t[r].s[0]].sum);}if(mode[2] == 'X') printf("%d\n", t[rt].ma);}return 0;}```* [FHQ-Treap](https://ewig-milan.github.io/2026/01/07/FHQ-Treap/),一个“强大、简洁、实用”的平衡树,其内核就是**分裂**与**合并**,下面是一些基础函数。```cppint root;struct Node {int l, r, x/*信息*/, key, /*其他东西*/;} t[N];int tot;int make(int x) {t[++tot] = {0, 0, x, rnd(), /*其它东西的初始化*/};return tot;}int build_key() { // 基于 keydeque<int> q;sort(A + 1, A + n + 1); // 对于 val 递增for(int i = 1; i <= n; i++) {int tmp = make(A[i]), last = 0;while(!q.empty() && t[q.back()].key > t[tmp].key) push_up(last = q.back()), q.pop_back();// tmp 插入 q.back() 和 last 之间if(!q.empty()) t[q.back()].r = tmp;t[tmp].l = last, q.push_back(tmp);}int rt = q.front();while(!q.empty()) push_up(q.back()), q.pop_back();return rt;}int build_none_key(int l, int r) { // 舍弃 keyif(l > r) return 0;int mid = l + r >> 1;int tmp = make(A[mid]);t[tmp].l = build(l, mid - 1), t[tmp].r = build(mid + 1, r);push_up(tmp);return tmp;}void split_val(int rt, int k, int &x, int &y) {int &lc = t[rt].l, &rc = t[rt].r;if(!rt) {x = y = 0; return;} // 注意push_down(rt);if(t[rt].x <= k) x = rt, split(rc, k, rc, y);else y = rt, split(lc, k, x, lc);push_up(rt);}void split_kth(int rt, int k, int &x, int &y) {if(!rt) {x = y = 0; return;}push_down(rt);int &lc = t[rt].l, &rc = t[rt].r;if(t[lc].siz + 1 <= k) x = rt, split(rc, k - t[lc].siz - 1, rc, y); // 注意是 k - t[lc].siz - 1else y = rt, split(lc, k, x, lc);push_up(rt);}int merge_key(int x, int y) { // 基于 keyif(!x || !y) return max(x, y); // 注意if(t[x].key <= t[y].key) {push_down(x);t[x].r = merge(t[x].r, y);push_up(x);return x;} else {push_down(y);t[y].l = merge(x, t[y].l);push_up(y);return y;}}int merge_none_key(int x, int y) { // 舍弃 keyif(!x || !y) return max(x, y);if(Rand(t[x].siz + t[y].siz) <= t[x].siz) {push_down(x);t[x].r = merge(t[x].r, y);push_up(x);return x;} else {push_down(y);t[y].l = merge(x, t[y].l);push_up(y);return y;}}int find_kth(int rt, int k) {int &lc = t[rt].l, &rc = t[rt].r;if(t[lc].siz + 1 == k) return rt;push_down(rt);if(t[lc].siz + 1 > k) return find(lc, k);return find(rc, k - t[lc].siz - 1);}int idx;void dfs(int rt) {if(!rt) return;push_down(rt);dfs(t[rt].l);A[++idx] = t[rt].x;dfs(t[rt].r);}void rebuild() {idx = 0, dfs(root);tot = 0, root = build(1, n);}```--- ## 可持久化数据结构### 可持久化并查集* 启发式(按秩)合并,并记下操作序列,~~这是虚假的可持久化~~。---### 可持久化线段树130 行未改动全部展开```---### 可持久化 FHQ-Treap* FHQ-Treap 相较于基于旋转的 Treap,一个很好的性质就是它可以做可持久化。* 思想大差不差。关于 clone 在哪用?**一切会改变这个节点的操作前 clone**。[Code](https://www.luogu.com.cn/record/256859704) is too long to put here。--- ## 进阶数据结构### 哈希表> 这是一个(我们)不常用但强大且简单(“简单”有待商榷)的数据结构* 其主要功能:通过关键码值(key)进行快速数据访问的结构。而手写哈希表主要是为了将离散的下标映射成连续的下标以达到快速、节约地进行**超大下标**的数据操作,比如状压 DP 中会产生大量非法状态的空间浪费,插头 DP 空间浪费更严重。* 手写哈希表主要是“哈希**挂**表”的形式,由于我习惯了链式前向星,所以延续了链式前向星的一些传统。77 行未改动* 树上主席树见 [**Code is Too Long!!!**](https://www.luogu.com.cn/record/212049895)---### 李超线段树* 整个树结构维护这样一件事:保证“若将根到叶子**路径**上所有函数下放到底,可以得到叶子的答案”。如何维护这件事呢?暴力的做法是对每个点做一遍,但我们想**利用线性函数的单调性**,做一个**类二分的剪枝**。那么我们可以为各个区间选一个“代表点”,各个节点维护其“代表点”在其**子树中**的最优函数,于是,下放的函数显然只能向左或向右更新(或不更新)。最后,取“代表点”为区间中点时,复杂度最优。然后你就了解了李超树的基础。* 一点性质:若为**直线**且动态开点,“树上的节点”到“直线”的映射是双射。* **一定要注意**,cmp 关系一定要满足**强序关系**,否则一条线不只存在一次。```cppstruct Line {ll k, b; int l, r;};struct Node {Line L;int lc, rc;}t[N << 2];int tot;int make(Line x) {t[++tot] = {x, 0, 0};return tot;}void add(int &rt, int l, int r, Line x) {if(!rt) {rt = make(x); return;}int mid = l + r >> 1;// cmp(a, b, x): a 优先级更高 ? 1 : 0if(cmp(x, t[rt].L, mid)) swap(t[rt].L, x);if(l == r) return;if(cmp(x, t[rt].L, l)) add(t[rt].lc, l, mid, x);if(cmp(x, t[rt].L, r)) add(t[rt].rc, mid + 1, r, x);}void modify(int &rt, int l, int r, Line x) {if(x.l <= l && x.r >= r) {add(rt, l, r, x);return;}if(!rt) rt = make({0, INF}); // 或 -INFint mid = l + r >> 1;if(x.l <= mid) modify(t[rt].lc, l, mid, x);if(x.r > mid) modify(t[rt].rc, mid + 1, r, x);}ll query(int rt, int l, int r, ll x) {if(!rt) return INF; // 或 -INFint mid = l + r >> 1;ll ans;if(x <= mid) ans = query(t[rt].lc, l, mid, x);else ans = query(t[rt].rc, mid + 1, r, x);return min(ans, t[rt].L.k * (x - O) + t[rt].L.b); // 或 max}```---### 根号分治(更多的是思想层面)> 这一部分也是刚刚[新学的](https://www.cnblogs.com/TKXZ133/p/17772749.html),还没学[深](https://www.luogu.com.cn/article/5gtqzd4a)* 可以找出界限分明的两种算法(其中一种常为部分分给的暴力),一种对于规模小,数量多的问题;一种对于规模大,数量少的问题。而且“**类打表**”的行为很常见。---### [莫队](https://oi-wiki.org/misc/mo-algo-intro/)> 这个学的不[深](https://www.cnblogs.com/MortisLife/p/18962510)--- 302 行未改动全部展开friend bool operator < (const Node & a, const Node & b) {return a.dis > b.dis;}};int dis[N];bool vis[N];void dij(int s) {memset(vis, 0, sizeof vis);memset(dis, 63, sizeof dis);priority_queue<Node> q;dis[s] = 0, q.push({s, 0});260 行未改动全部展开---### 欧拉(回)路* 一定要注意**当前弧优化**。```cppfor(int i = h[x]; ~i; i = h[x]) {h[x] = ne[i];// ......}```* [无需多言](https://oi-wiki.org/graph/euler/),题目中,注意“整个城市所有的道路都是双向车道,道路的两个方向均需要...”等字眼。---193 行未改动全部展开* 关于网络流的典例那就不得不单独开一块了。* [建模得到更复杂的板子](https://www.acwing.com/activity/content/32/)。* [24 题](https://www.luogu.com.cn/problem/list?type=luogu&page=1&tag=332)。* [一些思考 & 总结](https://www.luogu.com.cn/article/2lmd8k67)。--- 3 行未改动## DP 部分### Bases* 首先我们要知道 DP 绝对没有所谓的“模板”。但对于一般性的 DP 问题可以有一个固定的思维方向:状态 $\to$ 阶段 $\to$ 转移。* **状态**:遇到一个陌生情境时一定不能吝啬你给它安排的状态,因为往往没思路的原因就是“状态少了,无法清晰地转移”。**但就算状态多,各个状态也要清晰、简易!(这能简化转移式、提升正确率、加快做题速度,也别忘了去除无用状态)*** 阶段:这一部分要想清楚子问题间的依赖关系,不仅要无后效性,还不能漏掉可行决策。* **转移**:写方程时要细心,或者直接手推小样例检验自己的转移方程,注意转移的先后顺序。7 行未改动* 决策数过多,或是对子问题的重复计算,使算法效率严重下降。* 冗余、**不优**的决策仍被考虑(这些决策本应被剪去不考虑)。针对前两点,要限定可转移的范围。一个实用的方法是,先尽可能压缩范围,有漏掉的再加。给一些例子:从“往前**一个**单词处”转移、仅枚举**上一个**区域转移、从“拿这**一个**物品前”转移...针对第三点,带着“优化决策数”的目的多思考就行。* [题目](https://www.cnblogs.com/luckyblock/p/13121167.html)。---17 行未改动* [插头 DP](https://ewig-milan.github.io/2025/06/13/%E6%8F%92%E5%A4%B4DP%20-%20%E5%93%88%E5%AF%86%E9%A1%BF%E5%9B%9E%E8%B7%AF%E8%AE%A1%E6%95%B0/)。* [计数 DP](https://www.luogu.com.cn/problem/P7690)。* [数位 DP](https://www.luogu.com.cn/paste/m0ojt3g5)。---### 背包问题162 行未改动全部展开### 矩阵乘方优化(加速递推)* 代码易写错的前置知识,[矩阵乘法](https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95/5446029)。* 这个算法需要**观察数据范围**,因为他在一个很尴尬的处境中:数据范围小了,没必要用;数据范围大了,一眼就看得出来要用;数据范围卡得“正好要用”,可能常数太大正解过不了。所以大多数都能一眼看出来。* 这里给出递推的原理,那么“转移矩阵”的构造方法也一目了然了。249 行未改动全部展开--- ## 反悔贪心* 学术界(运筹学领域)对反悔贪心的阐释有两种流行理论:**DP 的凸优化**与**模拟费用流**,它们并非相互独立,两者有很多相同点(比如都与“凸”有关),但一般会选择模拟费用流,因为网络流方面,前人给出了很丰富的性质(而且都是**特别难证**的结论)。* [这题](https://www.luogu.com.cn/problem/P3545)可以试着用 DP 的凸优化裸推。* [这题](https://www.luogu.com.cn/problem/P3045)维度不单一,裸推是极难的,只能顺着前人铺好的路往后走——模拟费用流,利用那些已证的性质。--- ## wqs 二分* wqs 二分的基本运用是解决这样一类问题:设 $g(x)$ 为恰好选择 $x$ 个物品时的最优答案,其中**不限制 $x$ 时有十分优秀的做法**且 $g(x)$ 具有凸性(最大化答案需其上凸,反之亦然),求 $g(k)$。* 做法:在图中引入一条 $y=Cx$ 的“惩罚线”,即每选择一个物品会被惩罚 $C$。对于任意的 $C$ 我们可以轻松找到 $\Delta_{\max}=g(x^\prime)-Cx^\prime$。由凸性,通过二分 $C$ 我们可以有意地取到 $x^\prime=k$ 时的 $\Delta_{\max}$,那么我们就得到了 $g(k)$。* 注意二分写法上的严谨性(比如“共线”是需要想一想的)。--- ## 整体二分* 感觉很灵活,但我目前碰到的都比较板:多询问,可离线,单个询问可二分(所以其实算法本质上还是二分答案)。134 行未改动全部展开###### 下面是水话写完那确实爽了 想整体欣赏一遍!60k 字符量,小目标也是达成了好吧~---2026.1.13 突然想写了,又发现最近正好是“Nothing”半年纪念日(~~大雾~~)。半年时光白驹过隙,这半年,发生了许许多多的事情:第一次集训、第一次在 OI 路上结识好兄弟、第一个 CSP-S/NOIP 一等、第一次**全自主**学习知识点......当然还有这篇《Nothing》中无数的润色与修改,可以说,它不仅见证了我的成长,更陪伴着我踏出前进的每一步,以扎实的基础知识根基,作为我后期学习的坚固后盾。算是应了当初“**我希望这篇文章能在陪伴我 OI 生涯的同时更加丰富、完善**”的愿景。所以,我想说的是:感谢半年前奋斗的我,这份《致未来——Nothing》我们收到了!当然这半年的成长也是不可忽视的,我的内心变得更成熟、更坚强了。其实,也并非万事十全十美,遗憾是常态——CSP-S 的 T2、T3,NOIP 的 T4,偶尔颓废的日子,偶尔低落的日子......但是,瑕不掩瑜嘛,尽力而为,遗憾是“追求完美”,不后悔是“神之自信”!那么,同样的,新的一年,我在 2026.1.13,以当下的自己,诚邀未来的自己——继续奋斗!愿未来的我们,共同收到这份礼物!无限攀登——Ewig/Ever_crest!计划 1.25 前完成第二版(**done on Jan.21**)。###### 我想陪你一起看烟花(“你”是谁呢?也许是“过去和未来的自己”吧)