正文
增加 416 行删除 79 行
1234109 行未改动全部展开11411511611711811912012156 行未改动178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297297 行未改动全部展开59559659759859960060160278 行未改动::::success[LCA]:::success[VirtualTree]```cpptemplate<int N>109 行未改动全部展开返回 node 的 dd 级祖先。*/```:::::::success[BCC]```cpptemplate<int N>struct BCC {56 行未改动N:图的大小。用法说明:vector<vector<int>> solve(int fn, vector<pair<int, int>> fedge[]);计算大小为 fn 的图 fedge 的所有边双,其中 pair<int, int> 表示“走到的点”“无向边在原图中的编号”每个边双内部按点编号从小到大排序,所有边双按字典序排序*/```::::::success[HLD]```cpptemplate<int N>struct HLD {private:int n, fat[N + 1], root;vector<int> edge[N + 1];int dep[N + 1], siz[N + 1], hs[N + 1];int cnt, l[N + 1], r[N + 1], w[N + 1], top[N + 1];inline void dfs(int node) {siz[node] = 1;hs[node] = 0;for (int i : edge[node]) {dep[i] = dep[node] + 1;dfs(i);siz[node] += siz[i];if (siz[hs[node]] < siz[i])hs[node] = i;}}inline void solve(int node, int from) {l[node] = ++cnt;w[cnt] = node;top[node] = from;if (hs[node]) {solve(hs[node], from);for (int i : edge[node]) {if (i == hs[node])continue;solve(i, i);}}r[node] = cnt;}public:inline void init(int fn, int fa[]) {n = fn;for (int i = 1; i <= n; i++)edge[i].clear();for (int i = 1; i <= n; i++) {fat[i] = fa[i];if (fat[i] == 0)root = i;elseedge[fat[i]].push_back(i);}dep[root] = 0;dfs(root);cnt = 0;solve(root, root);}int dfn(int node) {return l[node];}int arcdfn(int dfn) {return w[dfn];}pair<int, int> subtree(int node) {return make_pair(l[node], r[node]);}vector<pair<pair<int, int>, bool>> path(int u, int v) {vector<pair<int, int>> vu, vv;while (top[u] != top[v]) {if (dep[top[u]] > dep[top[v]]) {vu.emplace_back(l[top[u]], l[u]);u = fat[top[u]];} else {vv.emplace_back(l[top[v]], l[v]);v = fat[top[v]];}}int to = dep[u] < dep[v] ? u : v;if (u != to)vu.emplace_back(l[to], l[u]);elsevv.emplace_back(l[to], l[v]);vector<pair<pair<int, int>, bool>> ret;for (auto i : vu)ret.emplace_back(i, true);reverse(vv.begin(), vv.end());for (auto i : vv)ret.emplace_back(i, false);return ret;}};/*模板含义:N:树的大小。用法说明:void init(int fn, int fa[]);预处理,树大小为 fn,每个点 i 的父亲节点为 fa[i],fa[root] = 0。int dfn(int node);返回重儿子优先遍历时 node 的 dfs 序。int arcdfn(int dfn);返回重儿子优先遍历时 dfn 的 dfs 序对应的点。pair<int, int> subtree(int node);返回重儿子优先遍历时 node 所在子树对应的 dfn 区间。vector<pair<pair<int, int>, bool>> path(int u, int v);返回重儿子优先遍历时 u 到 v 路径中对应 dfn 的 log 个连续区间们,若对应值为 true 则需要首尾反转。*/```::::::success[Segtree]297 行未改动全部展开返回以 node 为根的树中 L ~ R 的超立方体中的所有点的信息和。*/```::::::success[NTT]```cpptemplate<int D, int P, int G>struct NTT {78 行未改动123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115109 行未改动全部展开22522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630756 行未改动364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559297 行未改动全部展开85785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893978 行未改动:::success[HLD]```cpptemplate<int N>struct HLD {private:int n, fat[N + 1], root;vector<int> edge[N + 1];int dep[N + 1], siz[N + 1], hs[N + 1];int cnt, l[N + 1], r[N + 1], w[N + 1], top[N + 1];inline void dfs(int node) {siz[node] = 1;hs[node] = 0;for (int i : edge[node]) {dep[i] = dep[node] + 1;dfs(i);siz[node] += siz[i];if (siz[hs[node]] < siz[i])hs[node] = i;}}inline void solve(int node, int from) {l[node] = ++cnt;w[cnt] = node;top[node] = from;if (hs[node]) {solve(hs[node], from);for (int i : edge[node]) {if (i == hs[node])continue;solve(i, i);}}r[node] = cnt;}public:inline void init(int fn, int fa[]) {n = fn;for (int i = 1; i <= n; i++)edge[i].clear();for (int i = 1; i <= n; i++) {fat[i] = fa[i];if (fat[i] == 0)root = i;elseedge[fat[i]].push_back(i);}dep[root] = 0;dfs(root);cnt = 0;solve(root, root);}int dfn(int node) {return l[node];}int arcdfn(int dfn) {return w[dfn];}pair<int, int> subtree(int node) {return make_pair(l[node], r[node]);}vector<pair<pair<int, int>, bool>> path(int u, int v) {vector<pair<int, int>> vu, vv;while (top[u] != top[v]) {if (dep[top[u]] > dep[top[v]]) {vu.emplace_back(l[top[u]], l[u]);u = fat[top[u]];} else {vv.emplace_back(l[top[v]], l[v]);v = fat[top[v]];}}int to = dep[u] < dep[v] ? u : v;if (u != to)vu.emplace_back(l[to], l[u]);elsevv.emplace_back(l[to], l[v]);vector<pair<pair<int, int>, bool>> ret;for (auto i : vu)ret.emplace_back(i, true);reverse(vv.begin(), vv.end());for (auto i : vv)ret.emplace_back(i, false);return ret;}};/*模板含义:N:树的大小。用法说明:void init(int fn, int fa[]);预处理,树大小为 fn,每个点 i 的父亲节点为 fa[i],fa[root] = 0。int dfn(int node);返回重儿子优先遍历时 node 的 dfs 序。int arcdfn(int dfn);返回重儿子优先遍历时 dfn 的 dfs 序对应的点。pair<int, int> subtree(int node);返回重儿子优先遍历时 node 所在子树对应的 dfn 区间。vector<pair<pair<int, int>, bool>> path(int u, int v);返回重儿子优先遍历时 u 到 v 路径中对应 dfn 的 log 个连续区间们,若对应值为 true 则需要首尾反转。*/```:::::::success[LCA]:::success[VirtualTree]```cpptemplate<int N>109 行未改动全部展开返回 node 的 dd 级祖先。*/```:::::::success[DCC]```cpptemplate<int N>struct DCC {private:int n;vector<int> edge[N + 1];int cnt, dfn[N + 1], low[N + 1];bool cut[N + 1];stack<int> stk;vector<vector<int>> ans;inline void Tarjan(int node, int fa) {dfn[node] = low[node] = ++cnt;stk.push(node);int ss = 0;for (int i : edge[node]) {if (dfn[i] == -1) {ss++;Tarjan(i, node);low[node] = min(low[node], low[i]);if (low[i] >= dfn[node]) {cut[node] = true;ans.push_back(vector<int>{});while (!stk.empty()) {int tp = stk.top();ans.back().push_back(tp);stk.pop();if (tp == i)break;}ans.back().push_back(node);sort(ans.back().begin(), ans.back().end());}} else if (i != fa)low[node] = min(low[node], dfn[i]);}if (fa == -1 && ss <= 1)cut[node] = false;if (fa == -1 && ss == 0)ans.push_back(vector<int>{node});}public:vector<vector<int>> solve(int fn, vector<int> fe[]) {n = fn;for (int i = 1; i <= n; i++)edge[i] = fe[i];cnt = 0;for (int i = 1; i <= n; i++) {dfn[i] = -1;cut[i] = false;}ans.clear();for (int i = 1; i <= n; i++)if (dfn[i] == -1) {while (!stk.empty())stk.pop();Tarjan(i, -1);}sort(ans.begin(), ans.end());return ans;}};/*模版含义:N:图的大小。用法说明:vector<vector<int>> solve(int fn, vector<int> fe[]);返回该图中所有的点双连通分量,每个点双内部按点编号从小到大排序,所有点双按字典序排序。*/```::::::success[BCC]```cpptemplate<int N>struct BCC {56 行未改动N:图的大小。用法说明:vector<vector<int>> solve(int fn, vector<pair<int, int>> fedge[]);计算大小为 fn 的图 fedge 的所有边双,其中 pair<int, int> 表示“走到的点”“无向边在原图中的编号”,每个边双内部按点编号从小到大排序,所有边双按字典序排序。*/```::::::success[MaxFlow]```cpptemplate<int N, int M>struct MaxFlow {private:int n;vector<pair<int, int>> edge[N + 1];long long flow[M * 2];int cnt_edge, dis[N + 1], cur[N + 1], s, t;bool bfs(int s, int t) {for (int i = 1; i <= n; i++)dis[i] = -1;dis[s] = 0;queue<int> que;que.push(s);while (!que.empty()) {int node = que.front();if (node == t)return true;que.pop();for (auto [i, eid] : edge[node])if (dis[i] == -1 && flow[eid]) {dis[i] = dis[node] + 1;que.push(i);}}return false;}long long dfs(int node, long long water) {if (node == t || !water)return water;long long ret = 0;while (cur[node] < edge[node].size()) {auto [to, eid] = edge[node][cur[node]];if (dis[to] == dis[node] + 1 && flow[eid]) {int out = dfs(to, min(water, flow[eid]));water -= out;ret += out;flow[eid] -= out;flow[eid ^ 1] += out;if (water == 0)break;}cur[node]++;}if (ret == 0)dis[node] = -1;return ret;}public:inline void init(int fn) {n = fn;for (int i = 1; i <= n; i++)edge[i].clear();cnt_edge = -1;}inline void add_edge(int u, int v, long long w) {edge[u].emplace_back(v, ++cnt_edge);flow[cnt_edge] = w;edge[v].emplace_back(u, ++cnt_edge);flow[cnt_edge] = 0;}long long max_flow(int fs, int ft) {s = fs;t = ft;long long ret = 0;while (bfs(s, t)) {for (int i = 1; i <= n; i++)cur[i] = 0;ret += dfs(fs, LLONG_MAX);}return ret;}};/*模板含义:N:图的节点数;M:图的最大可能边数。用法说明:inline void init(int fn);进行格式化,并开启新图(大小为 fn)。inline void add_edge(int u, int v, long long w);加入边,从 u 到 v 权值为 w。long long max_flow(int fs, int ft);查询 fs 到 ft 的最大流(最小割),此后该图无效。*/```::::::success[RMQ]```cpptemplate<typename Info, int N>struct RMQ {private:int n, b, d, s;int bel[N + 1], l[N + 1], r[N + 1], pos[N + 1];Info st[__lg(N) + 1][N + 1];Info a[N + 1], premax[N + 1], sufmax[N + 1], _oo;unsigned long long zt[N + 1];public:inline void init(int fn, Info fa[]) {n = fn;for (int i = 1; i <= n; i++)a[i] = fa[i];_oo = fa[0];b = max(int(__lg(n) * 1.5), 1);s = (n + b - 1) / b;d = __lg(s);for (int i = 1; i <= s; i++) {l[i] = INT_MAX;r[i] = INT_MIN;st[0][i] = _oo;}for (int i = 1; i <= n; i++) {bel[i] = (i + b - 1) / b;l[bel[i]] = min(l[bel[i]], i);r[bel[i]] = max(r[bel[i]], i);st[0][bel[i]] = max(st[0][bel[i]], a[i]);}for (int i = 1; i <= n; i++) {if (i == 1 || bel[i] != bel[i - 1])pos[i] = 0;elsepos[i] = pos[i - 1] + 1;}for (int i = 1; i <= d; i++)for (int j = 1; j + (1 << i) - 1 <= s; j++)st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);for (int i = 1; i <= s; i++) {premax[l[i]] = a[l[i]];for (int j = l[i] + 1; j <= r[i]; j++)premax[j] = max(premax[j - 1], a[j]);sufmax[r[i]] = a[r[i]];for (int j = r[i] - 1; j >= l[i]; j--)sufmax[j] = max(sufmax[j + 1], a[j]);}for (int i = 1; i <= s; i++) {stack<int> stk;unsigned long long now = 0;for (int j = l[i]; j <= r[i]; j++) {while (!stk.empty() && a[stk.top()] < a[j]) {now ^= 1LL << pos[stk.top()];stk.pop();}now ^= 1LL << pos[j];zt[j] = now;stk.push(j);}}}Info get_max(int l, int r) {if (bel[l] == bel[r])return a[l + __builtin_ctzll(zt[r] >> pos[l])];else {Info ans = max(sufmax[l], premax[r]);int bl = bel[l] + 1, br = bel[r] - 1;if (bl <= br) {int lg = __lg(br - bl + 1);Info mx = max(st[lg][bl], st[lg][br - (1 << lg) + 1]);ans = max(ans, mx);}return ans;}}};/*模版含义:Info:int、unsigned int、long long、unsigned long long,值域;N:元素个数。用法说明:inline void init(int fn, Info fa[]);预处理,共 fn 个数,分别为 fa[i],fa[0] 为值域内最小数,如 INT_MIN。Info get_max(int l, int r);返回 fa[l ~ r] 中的最大值。*/```::::::success[Segtree]297 行未改动全部展开返回以 node 为根的树中 L ~ R 的超立方体中的所有点的信息和。*/```::::::success[SA]```cpptemplate<typename Char, int N>struct SA {private:int n, idx[N + 1], cnt[N + 1], rk_[N + 1];Char str[N + 1];public:inline void solve(int fn, Char fs[], int sa[], int rk[], int ht[]) {n = fn;for (int i = 1; i <= n; i++) {str[i] = fs[i];sa[i] = i;}sort(sa + 1, sa + 1 + n, [&](int u, int v) -> bool {return str[u] < str[v];});int pos = 0;for (int i = 1; i <= n; i++)rk[sa[i]] = pos = pos + (i == 1 || str[sa[i - 1]] != str[sa[i]]);for (int half = 1; half <= n && pos < n; half <<= 1) {int whole = half << 1;int tmp = 0;for (int i = n - half + 1; i <= n; i++)idx[++tmp] = i;for (int i = 1; i <= n; i++)if (sa[i] - half >= 1)idx[++tmp] = sa[i] - half;for (int i = 1; i <= pos; i++)cnt[i] = 0;for (int i = 1; i <= n; i++)cnt[rk[i]]++;for (int i = 2; i <= pos; i++)cnt[i] += cnt[i - 1];for (int i = n; i >= 1; i--)sa[cnt[rk[idx[i]]]--] = idx[i];for (int i = 1; i <= n; i++)rk_[i] = rk[i];pos = 0;pair<int, int> lst = make_pair(-1, -1);for (int i = 1; i <= n; i++) {pair<int, int> now;now.first = rk_[sa[i]];now.second = sa[i] + half <= n ? rk_[sa[i] + half] : 0;rk[sa[i]] = pos = pos + (lst != now);lst = now;}}int now = 0;for (int i = 1; i <= n; i++) {now = max(0, now - 1);if (rk[i] == n) {ht[rk[i]] = now = 0;continue;}int me = i, he = sa[rk[i] + 1];while (me + now <= n && he + now <= n && str[me + now] == str[he + now])now++;ht[rk[i]] = now;}}};/*模板含义:Char:字符集类型,用 char 就行,偶尔会用 int;N:字符串长度。用法说明:void solve(int fn, Char fs[], int sa[], int rk[], int ht[]);对长度为 n 的字符串 fs 求 SA(注意不能用 string 类,要转化成 char 数组),对于每个后缀 fs[i ~ n] 的 i 按照后缀的字典序从小到大排序存入 sa,每个 i 在 sa 中的排名存入 rk,fs[sa[i] ~ n] 与 fs[sa[i + 1] ~ n] 的 LCP 存入 ht。*/```::::::success[NTT]```cpptemplate<int D, int P, int G>struct NTT {78 行未改动