版本比较
板子
正文
增加 73 行删除 73 行
11 行未改动12131415161718192044 行未改动656667686970717273146 行未改动全部展开22022122222322422522622722854 行未改动28328428528628728828929029133 行未改动32532632732832933033133233343 行未改动37737837938038138238338438538638738838939039139239339439539639739839983 行未改动48348448548648748848949049149249349449524 行未改动52052152252352452552652752852953053153252 行未改动58558658758858959059159259314 行未改动608609610611612613614615616617618619620621622623624625626627628629630631177 行未改动全部展开80981081181281381481581681717 行未改动83583683783883984084184284323 行未改动867868869870871872873874875876114 行未改动全部展开99199299399499599699799899929 行未改动10291030103110321033103410351036103721 行未改动1059106010611062106310641065106610676 行未改动1074107510761077107810791080108110824 行未改动10871088108910901091109210931094109514 行未改动11101111111211131114111511161117111844 行未改动1163116411651166116711681169117011714 行未改动11761177117811791180118111821183118444 行未改动1229123012311232123312341235123612374 行未改动1242124312441245124612471248124912509 行未改动1260126112621263126412651266126712681269127031 行未改动13021303130413051306130713081309131044 行未改动13551356135713581359136013611362136346 行未改动14101411141214131414141514161417141813 行未改动1432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145410 行未改动14651466146714681469147014711472147321 行未改动14951496149714981499150015011502150315041505150615071508150915101511151215136 行未改动15201521152215231524152515261527152829 行未改动1558155915601561156215631564156515661567156815691570157115721573157436 行未改动16111612161316141615161616171618161916201621162216231624162567 行未改动169316941695169616971698169917001701201 行未改动全部展开190319041905190619071908190919101911191219131914191519161917169 行未改动全部展开208720882089209020912092209320942095161 行未改动全部展开2257225822592260226122622263226422652 行未改动2268226922702271227222732274227522767 行未改动228422852286228722882289229022912292153 行未改动全部展开2446244724482449245024512452245324544 行未改动2459246024612462246324642465246624676 行未改动2474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249614 行未改动25112512251325142515251625172518251975 行未改动11 行未改动- `min` 和 `max` 都可以直接 `std::min` `std::max` 吧- 关同步 `#define fastread std::ios_sync_with_stdio (false); std::cin.tie (nullptr);`upd: 更为简洁的写法是 `std::cin.tie(nullptr) -> sync_with_stdio(false);`- 学习 jiangly `using i64 = long long`44 行未改动auto s = std::chrono::steady_clock::now ();system ("test_2.exe < test.in > test_2.out");auto t = std::chrono::steady_clock::now ();auto duration = std::chrono::duration_cast <std::chrono::microseconds> (t - s);double cost = (double) duration.count () / 1000;std::cout << cost << "\n";146 行未改动全部展开if (not spfa (n + 1)) { std::cout << "NO\n"; }else {for (int i = 1; i <= n; i++) { std::cout << dis[i] << " "; }}}```#### Floyd54 行未改动}```> 上面的代码是用 `bitset` 写的, 与传统 $\text{Floyd}$ 略有不同>> 但是其基本思想是类似的### 生成树33 行未改动}```> 其中 `init()` 就是并查集的 `init()`>> 并查集的写法看数据结构部分吧### 欧拉回路43 行未改动upd: 判断- 有向图欧拉路径的判断```cppbool check () {int dcnt1 = 0;int dcnt2 = 0;for (int i = 1; i <= tot; i++) {if (ind[i] - otd[i] == 0) { continue; }if (ind[i] - otd[i] == 1) { dcnt1 ++; continue; }if (ind[i] - otd[i] == -1) { dcnt2 ++; start = i; continue; }return false;}if (dcnt1 > 1 or dcnt2 > 1 or dcnt1 + dcnt2 == 1) { return false; }}```83 行未改动sons ++;if (low[t] >= dfn[now]) { cut[now] = true; }} else { // 此处可以考虑父亲low[now] = std::min (low[now], dfn[t]);}}if (fa == 0 and sons < 2) { cut[now] = false; }}```##### 点双连通分量24 行未改动int last = 0;while (last != t) {int top = stk[spos--];belong[top] = vdcc_cnt;vdcc[vdcc_cnt].push_back (top);last = top;}vdcc[vdcc_cnt].push_back (now);}} else {low[now] = std::min (low[now], dfn[t]);}}52 行未改动} else if (i != (in_edge ^ 1)) {low[now] = std::min (low[now], dfn[t]);}}}int belong[maxn], edcc_cnt;std::vector<int> edcc[maxn];14 行未改动### 网络流> 网络流的加边方式与普通图论问题的加边方式略有不同>> ```cpp> struct Edge {> int u, v;> int pre;> long long flow;> } es[maxm << 1];>> int last[maxn], cur[maxn], cnt = 1;> int S, T;>> inline void _addEdge (int u, int v, long long cap) {> es[++cnt] = Edge { u, v, last[u], cap };> last[u] = cnt;> }>> inline void addEdge (int u, int v, long long cap) {> _addEdge (u, v, cap);> _addEdge (v, u, 0);> }177 行未改动全部展开// 改变树的结构 (加入入基边, 删除出基边)// 实际上就是翻转树的结构 (此处我们直接暴力翻转)// 如果最小边是在lca到u的路径上, 我们实际上要对v->u->delNode的路径进行更新;// 如果最小边是在v到lca的路径上, 我们实际上要对u->v->delNode的路径进行更新// 这里有一个trick// 我们发现, 在第一种情况里的u->delNode和第二种情况里v->delNode的路径中, 我们原有的faEdge[x]都是从我们要跳到的点, 指向当前点x// 如果我们能统一边的方向(从我们要跳到的点指向当前点), 翻转整条链的操作就会变得非常好处理17 行未改动lastEdge ^= 1; // 将上一条边翻转swap (lastEdge, faEdge[now]);fa[now] = lastNode;lastNode = now;}return cost;}23 行未改动}// 最后加上的边反向边 (S -> T) 的flow就是我们的最大流// (因为我们这条边加的意义是使得任意S到T的流都可以与它形成负环, 所以每次推流的时候都会把增加的流量加到它上面去)int maxFlow = es[cnt].flow;int minCost = int ((long long) es[cnt].flow * inf + cost); // 需要加上 es[cnt].flow * inf 抵消掉一开始那条边的费用return MCMF { minCost, maxFlow };}inline void clear () {114 行未改动全部展开for (; p and ns[p].ch[c] == q; p = ns[p].fa) { ns[p].ch[c] = nq; }}}}inline void getParentTree (Graph& tr) {for (int i = 2; i <= tot; i++) { tr.addEdge (ns[i].fa, i); }}29 行未改动inline void init () {cnt = 1, last = 0; // 奇根的前一个是偶根pam[0].len = 0;pam[1].len = -1; // 两边同时加入一个pam[0].fail = 1; // 奇根不会有fail}// decrypt (x) 只是为了满足模板题里面的强制在线而已#define decrypt(x) (s[i] + pam[last].cnt - 97) % 26 + 9721 行未改动last = pam[a].next[s[i] - 'a'];res.push_back(pam[last].cnt);}return res;}}```6 行未改动考虑贡献即可。```cpprep (d, 1, n)for (int t = d; t <= n; t += d) ds[t].push_back(d);```### Miller-Rabin4 行未改动i64 x = p - 1;while (x % 2 == 0) {i64 v = ksm(a, x / 2, p);if (v == p - 1) return true;if (v != 1) return false;x /= 2;}return true;}14 行未改动while (b) {if (b & 1) { (res *= base) %= mod; }(base *= base) %= mod;b >>= 1;}return res;}```44 行未改动```于是, 上面的逆元也可以用exgcd求因为$$a \times inv_a \equiv 1 \pmod b \\4 行未改动```cppinline i64 inv (i64 a, i64 b) {i64 x, y; exgcd (a, b, x, y);return (x + b) % b;}```### 线性筛44 行未改动}}for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + mu[i]; }}```### 中国剩余定理4 行未改动~~orz钟神太强了%%%~~```cppinline bool merge (i64 &m1, i64 &a1, i64 m2, i64 a2) {if (m1 < m2) { std::swap (m1, m2); std::swap (a1, a2); }while (a1 % m2 != a2) { a1 += m1; }m1 = lcm (m1, m2);9 行未改动```cppinline bool merge (i64& m1, i64& a1, i64 m2, i64 a2) {if (m1 < m2) { std::swap (m1, m2); std::swap (a1, a2); }int tot = 0;while (a1 % m2 != a2) {if (++tot > m2 + 1) { return false; }a1 += m1;}m1 = lcm (m1, m2);return true;}31 行未改动for (int i = 0; i < 22; i++) {for (int S = 0; S < (1 << 22); S++) {if (S & (1 << i)) { sum[S] = sum[S] + sum[S ^ (1 << i)]; }}}```### 异或线性基44 行未改动int rnkA = 0; // 系数矩阵的秩rep (i, 1, n) {int t = rnkA + 1;rep (j, rnkA + 1, n) {if (std::fabs(a[j][i]) > std::fabs(a[t][i])) t = j;}if (std::fabs(a[t][i]) < eps) continue;rnkA++;46 行未改动for (int i = 0; i < len; i += h) {for (int j = i; j < i + (h >> 1); j++) {i64 u = a[j] % mod;i64 v = a[j + (h >> 1)] % mod * pw[(maxn / h) * c * (j - i)] % mod;a[j] = (u + v >= mod ? u + v - mod : u + v);a[j + (h >> 1)] = (u - v < 0 ? u - v + mod : u - v);}}}13 行未改动DFT(b, 1);std::vector<i64> c(len);rep (i, 0, len - 1) c[i] = a[i] * b[i] % mod;DFT(c, -1);return c;}}```### Stern–Brocot 树```cppvoid dfs(f64 X, int a, int b, int c, int d) {int x = a + c, y = b + d;if (x > n or y > m) return;decltype(ans) cur = { std::abs(((f64) 1.0 * x / y) - X), { x, y }};nxt = std::min(nxt, cur);if (nxt < ans) std::swap(nxt, ans);if (X < ((f64) 1.0 * x / y)) dfs(X, a, b, x, y);10 行未改动int fa[maxn];inline void solve() {rep (i, 1, n - 2) std::cin >> pi[i];rep (i, 1, n) deg[i] = 1;rep (i, 1, n - 2) deg[pi[i]]++;pi[n - 1] = n;21 行未改动inline void solve() {rep (i, 1, n - 1) std::cin >> fa[i];rep (i, 1, n - 1) deg[i] = 1;rep (i, 1, n) deg[fa[i]]++;int p = 1;rep (i, 1, n - 2) {while (deg[p] > 1) p++;pi[i] = fa[p];while (i < n - 1 and (--deg[pi[i]]) == 1 and pi[i] < p) {pi[i + 1] = fa[pi[i]]; i++;}p++;}i64 res = 0;rep (i, 1, n - 2) res ^= (1ll * i * pi[i]);std::cout << res << "\n";}6 行未改动```cpptemplate <int maxn>struct UnionFindSet {int fa[maxn];inline void init () {for (int i = 1; i <= n; i++) { fa[i] = i; }}29 行未改动};```> 上面只给出了单点加区间求和的版本>> 实际上, 树状数组还可以通过神秘的技术实现区间加单点查和 **区间加区间求和**>> 甚至可以通过跳 `lowbit` 实现 **区间最值查询**>> 然而实现十分复杂, 失去了树状数组本身简洁的优势>> ~~而且复杂度好像也不是很对~~ (曾被hzhl狠狠质疑)>> 所以不如写线段树 👇### 线段树36 行未改动pushUp (now);}void modify (int now, int l, int r, int L, int R, val_t val) {if (L <= l and r <= R) { update (now, l, r, val); return; }pushDown (now, l, r);int mid = (l + r) >> 1;if (L <= mid) { modify (now << 1, l, mid, L, R, val); }if (R > mid) { modify (now << 1 | 1, mid + 1, r, L, R, val); }pushUp (now);}val_t query (int now, int l, int r, int L, int R) {if (L <= l and r <= R) { return tr[now].sum; }67 行未改动int mid = (l + r) >> 1;if (L <= mid) mdf(now << 1, l, mid, L, R, t);if (R > mid) mdf(now << 1 | 1, mid + 1, r, L, R, t);pushup(now);}int qry(int now, int l, int r, int L, int R) {201 行未改动全部展开int rt[maxn];inline void clear () {for (int i = 1; i <= tot; i++) { tr[i][0] = tr[i][1] = 0; siz[i] = 0; }tot = 0;}inline void insert (int now, int old, int val) {for (int i = 31; i >= 0; i--) {bool x = ((val >> i) & 1);tr[now][x] = ++tot; tr[now][x ^ 1] = tr[old][x ^ 1]; // clonenow = tr[now][x]; old = tr[old][x];169 行未改动全部展开y = now; split (tr[now].ls, siz, x, tr[now].ls);pushUp (now);} else {x = now; split (tr[now].rs, siz - tr[tr[now].ls].siz - 1, tr[now].rs, y);pushUp (now);}}int merge (int x, int y) {161 行未改动全部展开void modify (int& now, int l, int r, int pos, int val) {if (not now) { now = ++tot; }tr[now].sum += val;if (l == r) { return; }int mid = (l + r) >> 1;if (pos <= mid) { modify (tr[now].ls, l, mid, pos, val); }2 行未改动int query (int l, int r, int k) {if (l == r) { return l; }int sum = 0;for (auto x : Add) { sum += tr[tr[x].ls].sum; }for (auto x : Sub) { sum -= tr[tr[x].ls].sum; }for (auto& x : Add) { x = (k <= sum) ? tr[x].ls : tr[x].rs; }7 行未改动inline int lowbit (int x) { return x & (-x); }inline void ins (int pos, int val) {for (int i = pos; i <= n; i += lowbit (i)) { seg.modify (seg.root[i], 0, 1e9, val, 1); }}inline void del (int pos, int val) {for (int i = pos; i <= n; i += lowbit (i)) { seg.modify (seg.root[i], 0, 1e9, val, -1); }153 行未改动全部展开inline void connect (int fa, int x, int p) { tr[fa].ch[p] = x; tr[x].fa = fa; }inline void rotate (int x) {int f = tr[x].fa, ff = tr[f].fa, p = chk (x), pp = chk (f);pushDown (f); pushDown (x);if (notRoot (f)) { connect (ff, x, pp); }else { tr[x].fa = ff; }connect (f, tr[x].ch[p ^ 1], p); connect (x, f, p ^ 1);4 行未改动pushAll (x);for (; notRoot (x); rotate (x)) {if (notRoot (tr[x].fa)) { rotate (chk (tr[x].fa) == chk (x) ? tr[x].fa : x); }}pushUp (x);}inline void access (int x) {int pre = 0;6 行未改动inline void makeRoot (int x) { access (x); splay (x); rev (x); }inline void split (int x, int y) { makeRoot (x); access (y); splay (y); }inline int findRoot (int x) {access (x); splay (x);pushDown (x); while (tr[x].ch[0]) { x = tr[x].ch[0]; pushDown (x); }splay (x); return x;}inline void link (int x, int y) {makeRoot (x);if (x == findRoot (y)) { return; }tr[x].fa = y;}inline void cut (int x, int y) {split (x, y);if (tr[y].ch[0] != x or tr[x].ch[0] != tr[x].ch[1]) { return; }tr[y].ch[0] = tr[x].fa = 0;pushUp (y);}```14 行未改动int maxpart[maxn];int siz[maxn];void getRoot (int now, int fa) {siz[now] = 1;maxpart[now] = 0;for (int i = last[now]; i; i = es[i].pre) {int t = es[i].v;if (t == fa) { continue; }75 行未改动11 行未改动12131415161718192044 行未改动656667686970717273146 行未改动全部展开22022122222322422522622722854 行未改动28328428528628728828929029133 行未改动32532632732832933033133233343 行未改动37737837938038138238338438538638738838939039139239339439539639739839983 行未改动48348448548648748848949049149249349449524 行未改动52052152252352452552652752852953053153252 行未改动58558658758858959059159259314 行未改动608609610611612613614615616617618619620621622623624625626627628629630631177 行未改动全部展开80981081181281381481581681717 行未改动83583683783883984084184284323 行未改动867868869870871872873874875876114 行未改动全部展开99199299399499599699799899929 行未改动10291030103110321033103410351036103721 行未改动1059106010611062106310641065106610676 行未改动1074107510761077107810791080108110824 行未改动10871088108910901091109210931094109514 行未改动11101111111211131114111511161117111844 行未改动1163116411651166116711681169117011714 行未改动11761177117811791180118111821183118444 行未改动1229123012311232123312341235123612374 行未改动1242124312441245124612471248124912509 行未改动1260126112621263126412651266126712681269127031 行未改动13021303130413051306130713081309131044 行未改动13551356135713581359136013611362136346 行未改动14101411141214131414141514161417141813 行未改动1432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145410 行未改动14651466146714681469147014711472147321 行未改动14951496149714981499150015011502150315041505150615071508150915101511151215136 行未改动15201521152215231524152515261527152829 行未改动1558155915601561156215631564156515661567156815691570157115721573157436 行未改动16111612161316141615161616171618161916201621162216231624162567 行未改动169316941695169616971698169917001701201 行未改动全部展开190319041905190619071908190919101911191219131914191519161917169 行未改动全部展开208720882089209020912092209320942095161 行未改动全部展开2257225822592260226122622263226422652 行未改动2268226922702271227222732274227522767 行未改动228422852286228722882289229022912292153 行未改动全部展开2446244724482449245024512452245324544 行未改动2459246024612462246324642465246624676 行未改动2474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249614 行未改动25112512251325142515251625172518251975 行未改动11 行未改动- `min` 和 `max` 都可以直接 `std::min` `std::max` 吧- 关同步 `#define fastread std::ios_sync_with_stdio (false); std::cin.tie (nullptr);`upd: 更为简洁的写法是 `std::cin.tie(nullptr) -> sync_with_stdio(false);`- 学习 jiangly `using i64 = long long`44 行未改动auto s = std::chrono::steady_clock::now ();system ("test_2.exe < test.in > test_2.out");auto t = std::chrono::steady_clock::now ();auto duration = std::chrono::duration_cast <std::chrono::microseconds> (t - s);double cost = (double) duration.count () / 1000;std::cout << cost << "\n";146 行未改动全部展开if (not spfa (n + 1)) { std::cout << "NO\n"; }else {for (int i = 1; i <= n; i++) { std::cout << dis[i] << " "; }}}```#### Floyd54 行未改动}```> 上面的代码是用 `bitset` 写的, 与传统 $\text{Floyd}$ 略有不同>> 但是其基本思想是类似的### 生成树33 行未改动}```> 其中 `init()` 就是并查集的 `init()`>> 并查集的写法看数据结构部分吧### 欧拉回路43 行未改动upd: 判断- 有向图欧拉路径的判断```cppbool check () {int dcnt1 = 0;int dcnt2 = 0;for (int i = 1; i <= tot; i++) {if (ind[i] - otd[i] == 0) { continue; }if (ind[i] - otd[i] == 1) { dcnt1 ++; continue; }if (ind[i] - otd[i] == -1) { dcnt2 ++; start = i; continue; }return false;}if (dcnt1 > 1 or dcnt2 > 1 or dcnt1 + dcnt2 == 1) { return false; }}```83 行未改动sons ++;if (low[t] >= dfn[now]) { cut[now] = true; }} else { // 此处可以考虑父亲low[now] = std::min (low[now], dfn[t]);}}if (fa == 0 and sons < 2) { cut[now] = false; }}```##### 点双连通分量24 行未改动int last = 0;while (last != t) {int top = stk[spos--];belong[top] = vdcc_cnt;vdcc[vdcc_cnt].push_back (top);last = top;}vdcc[vdcc_cnt].push_back (now);}} else {low[now] = std::min (low[now], dfn[t]);}}52 行未改动} else if (i != (in_edge ^ 1)) {low[now] = std::min (low[now], dfn[t]);}}}int belong[maxn], edcc_cnt;std::vector<int> edcc[maxn];14 行未改动### 网络流> 网络流的加边方式与普通图论问题的加边方式略有不同>> ```cpp> struct Edge {> int u, v;> int pre;> long long flow;> } es[maxm << 1];>> int last[maxn], cur[maxn], cnt = 1;> int S, T;>> inline void _addEdge (int u, int v, long long cap) {> es[++cnt] = Edge { u, v, last[u], cap };> last[u] = cnt;> }>> inline void addEdge (int u, int v, long long cap) {> _addEdge (u, v, cap);> _addEdge (v, u, 0);> }177 行未改动全部展开// 改变树的结构 (加入入基边, 删除出基边)// 实际上就是翻转树的结构 (此处我们直接暴力翻转)// 如果最小边是在lca到u的路径上, 我们实际上要对v->u->delNode的路径进行更新;// 如果最小边是在v到lca的路径上, 我们实际上要对u->v->delNode的路径进行更新// 这里有一个trick// 我们发现, 在第一种情况里的u->delNode和第二种情况里v->delNode的路径中, 我们原有的faEdge[x]都是从我们要跳到的点, 指向当前点x// 如果我们能统一边的方向(从我们要跳到的点指向当前点), 翻转整条链的操作就会变得非常好处理17 行未改动lastEdge ^= 1; // 将上一条边翻转swap (lastEdge, faEdge[now]);fa[now] = lastNode;lastNode = now;}return cost;}23 行未改动}// 最后加上的边反向边 (S -> T) 的flow就是我们的最大流// (因为我们这条边加的意义是使得任意S到T的流都可以与它形成负环, 所以每次推流的时候都会把增加的流量加到它上面去)int maxFlow = es[cnt].flow;int minCost = int ((long long) es[cnt].flow * inf + cost); // 需要加上 es[cnt].flow * inf 抵消掉一开始那条边的费用return MCMF { minCost, maxFlow };}inline void clear () {114 行未改动全部展开for (; p and ns[p].ch[c] == q; p = ns[p].fa) { ns[p].ch[c] = nq; }}}}inline void getParentTree (Graph& tr) {for (int i = 2; i <= tot; i++) { tr.addEdge (ns[i].fa, i); }}29 行未改动inline void init () {cnt = 1, last = 0; // 奇根的前一个是偶根pam[0].len = 0;pam[1].len = -1; // 两边同时加入一个pam[0].fail = 1; // 奇根不会有fail}// decrypt (x) 只是为了满足模板题里面的强制在线而已#define decrypt(x) (s[i] + pam[last].cnt - 97) % 26 + 9721 行未改动last = pam[a].next[s[i] - 'a'];res.push_back(pam[last].cnt);}return res;}}```6 行未改动考虑贡献即可。```cpprep (d, 1, n)for (int t = d; t <= n; t += d) ds[t].push_back(d);```### Miller-Rabin4 行未改动i64 x = p - 1;while (x % 2 == 0) {i64 v = ksm(a, x / 2, p);if (v == p - 1) return true;if (v != 1) return false;x /= 2;}return true;}14 行未改动while (b) {if (b & 1) { (res *= base) %= mod; }(base *= base) %= mod;b >>= 1;}return res;}```44 行未改动```于是, 上面的逆元也可以用exgcd求因为$$a \times inv_a \equiv 1 \pmod b \\4 行未改动```cppinline i64 inv (i64 a, i64 b) {i64 x, y; exgcd (a, b, x, y);return (x + b) % b;}```### 线性筛44 行未改动}}for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + mu[i]; }}```### 中国剩余定理4 行未改动~~orz钟神太强了%%%~~```cppinline bool merge (i64 &m1, i64 &a1, i64 m2, i64 a2) {if (m1 < m2) { std::swap (m1, m2); std::swap (a1, a2); }while (a1 % m2 != a2) { a1 += m1; }m1 = lcm (m1, m2);9 行未改动```cppinline bool merge (i64& m1, i64& a1, i64 m2, i64 a2) {if (m1 < m2) { std::swap (m1, m2); std::swap (a1, a2); }int tot = 0;while (a1 % m2 != a2) {if (++tot > m2 + 1) { return false; }a1 += m1;}m1 = lcm (m1, m2);return true;}31 行未改动for (int i = 0; i < 22; i++) {for (int S = 0; S < (1 << 22); S++) {if (S & (1 << i)) { sum[S] = sum[S] + sum[S ^ (1 << i)]; }}}```### 异或线性基44 行未改动int rnkA = 0; // 系数矩阵的秩rep (i, 1, n) {int t = rnkA + 1;rep (j, rnkA + 1, n) {if (std::fabs(a[j][i]) > std::fabs(a[t][i])) t = j;}if (std::fabs(a[t][i]) < eps) continue;rnkA++;46 行未改动for (int i = 0; i < len; i += h) {for (int j = i; j < i + (h >> 1); j++) {i64 u = a[j] % mod;i64 v = a[j + (h >> 1)] % mod * pw[(maxn / h) * c * (j - i)] % mod;a[j] = (u + v >= mod ? u + v - mod : u + v);a[j + (h >> 1)] = (u - v < 0 ? u - v + mod : u - v);}}}13 行未改动DFT(b, 1);std::vector<i64> c(len);rep (i, 0, len - 1) c[i] = a[i] * b[i] % mod;DFT(c, -1);return c;}}```### Stern–Brocot 树```cppvoid dfs(f64 X, int a, int b, int c, int d) {int x = a + c, y = b + d;if (x > n or y > m) return;decltype(ans) cur = { std::abs(((f64) 1.0 * x / y) - X), { x, y }};nxt = std::min(nxt, cur);if (nxt < ans) std::swap(nxt, ans);if (X < ((f64) 1.0 * x / y)) dfs(X, a, b, x, y);10 行未改动int fa[maxn];inline void solve() {rep (i, 1, n - 2) std::cin >> pi[i];rep (i, 1, n) deg[i] = 1;rep (i, 1, n - 2) deg[pi[i]]++;pi[n - 1] = n;21 行未改动inline void solve() {rep (i, 1, n - 1) std::cin >> fa[i];rep (i, 1, n - 1) deg[i] = 1;rep (i, 1, n) deg[fa[i]]++;int p = 1;rep (i, 1, n - 2) {while (deg[p] > 1) p++;pi[i] = fa[p];while (i < n - 1 and (--deg[pi[i]]) == 1 and pi[i] < p) {pi[i + 1] = fa[pi[i]]; i++;}p++;}i64 res = 0;rep (i, 1, n - 2) res ^= (1ll * i * pi[i]);std::cout << res << "\n";}6 行未改动```cpptemplate <int maxn>struct UnionFindSet {int fa[maxn];inline void init () {for (int i = 1; i <= n; i++) { fa[i] = i; }}29 行未改动};```> 上面只给出了单点加区间求和的版本>> 实际上, 树状数组还可以通过神秘的技术实现区间加单点查和 **区间加区间求和**>> 甚至可以通过跳 `lowbit` 实现 **区间最值查询**>> 然而实现十分复杂, 失去了树状数组本身简洁的优势>> ~~而且复杂度好像也不是很对~~ (曾被hzhl狠狠质疑)>> 所以不如写线段树 👇### 线段树36 行未改动pushUp (now);}void modify (int now, int l, int r, int L, int R, val_t val) {if (L <= l and r <= R) { update (now, l, r, val); return; }pushDown (now, l, r);int mid = (l + r) >> 1;if (L <= mid) { modify (now << 1, l, mid, L, R, val); }if (R > mid) { modify (now << 1 | 1, mid + 1, r, L, R, val); }pushUp (now);}val_t query (int now, int l, int r, int L, int R) {if (L <= l and r <= R) { return tr[now].sum; }67 行未改动int mid = (l + r) >> 1;if (L <= mid) mdf(now << 1, l, mid, L, R, t);if (R > mid) mdf(now << 1 | 1, mid + 1, r, L, R, t);pushup(now);}int qry(int now, int l, int r, int L, int R) {201 行未改动全部展开int rt[maxn];inline void clear () {for (int i = 1; i <= tot; i++) { tr[i][0] = tr[i][1] = 0; siz[i] = 0; }tot = 0;}inline void insert (int now, int old, int val) {for (int i = 31; i >= 0; i--) {bool x = ((val >> i) & 1);tr[now][x] = ++tot; tr[now][x ^ 1] = tr[old][x ^ 1]; // clonenow = tr[now][x]; old = tr[old][x];169 行未改动全部展开y = now; split (tr[now].ls, siz, x, tr[now].ls);pushUp (now);} else {x = now; split (tr[now].rs, siz - tr[tr[now].ls].siz - 1, tr[now].rs, y);pushUp (now);}}int merge (int x, int y) {161 行未改动全部展开void modify (int& now, int l, int r, int pos, int val) {if (not now) { now = ++tot; }tr[now].sum += val;if (l == r) { return; }int mid = (l + r) >> 1;if (pos <= mid) { modify (tr[now].ls, l, mid, pos, val); }2 行未改动int query (int l, int r, int k) {if (l == r) { return l; }int sum = 0;for (auto x : Add) { sum += tr[tr[x].ls].sum; }for (auto x : Sub) { sum -= tr[tr[x].ls].sum; }for (auto& x : Add) { x = (k <= sum) ? tr[x].ls : tr[x].rs; }7 行未改动inline int lowbit (int x) { return x & (-x); }inline void ins (int pos, int val) {for (int i = pos; i <= n; i += lowbit (i)) { seg.modify (seg.root[i], 0, 1e9, val, 1); }}inline void del (int pos, int val) {for (int i = pos; i <= n; i += lowbit (i)) { seg.modify (seg.root[i], 0, 1e9, val, -1); }153 行未改动全部展开inline void connect (int fa, int x, int p) { tr[fa].ch[p] = x; tr[x].fa = fa; }inline void rotate (int x) {int f = tr[x].fa, ff = tr[f].fa, p = chk (x), pp = chk (f);pushDown (f); pushDown (x);if (notRoot (f)) { connect (ff, x, pp); }else { tr[x].fa = ff; }connect (f, tr[x].ch[p ^ 1], p); connect (x, f, p ^ 1);4 行未改动pushAll (x);for (; notRoot (x); rotate (x)) {if (notRoot (tr[x].fa)) { rotate (chk (tr[x].fa) == chk (x) ? tr[x].fa : x); }}pushUp (x);}inline void access (int x) {int pre = 0;6 行未改动inline void makeRoot (int x) { access (x); splay (x); rev (x); }inline void split (int x, int y) { makeRoot (x); access (y); splay (y); }inline int findRoot (int x) {access (x); splay (x);pushDown (x); while (tr[x].ch[0]) { x = tr[x].ch[0]; pushDown (x); }splay (x); return x;}inline void link (int x, int y) {makeRoot (x);if (x == findRoot (y)) { return; }tr[x].fa = y;}inline void cut (int x, int y) {split (x, y);if (tr[y].ch[0] != x or tr[x].ch[0] != tr[x].ch[1]) { return; }tr[y].ch[0] = tr[x].fa = 0;pushUp (y);}```14 行未改动int maxpart[maxn];int siz[maxn];void getRoot (int now, int fa) {siz[now] = 1;maxpart[now] = 0;for (int i = last[now]; i; i = es[i].pre) {int t = es[i].v;if (t == fa) { continue; }75 行未改动