版本比较
「做题记录」分治选做
正文
增加 34 行删除 34 行
1234536 行未改动424344454647484950515243 行未改动9697989910010110210310499 行未改动20420520620720820921021121289 行未改动302303304305306307308309310101 行未改动全部展开412413414415416417418419420116 行未改动全部展开537538539540541542543544545145 行未改动全部展开69169269369469569669769869943 行未改动74374474574674774874975075154 行未改动806807808809810811812813814147 行未改动全部展开962963964965966967968969970154 行未改动全部展开112511261127112811291130113111321133108 行未改动全部展开124212431244124512461247124812491250149 行未改动全部展开14001401140214031404140514061407140865 行未改动14741475147614771478147914801481148289 行未改动157215731574157515761577157815791580128 行未改动全部展开17091710171117121713171417151716171771 行未改动17891790179117921793179417951796179783 行未改动18811882188318841885188618871888188967 行未改动195719581959196019611962196319641965113 行未改动全部展开207920802081208220832084208520862087140 行未改动全部展开222822292230223122322233223422352236158 行未改动全部展开23952396239723982399240024012402240377 行未改动248124822483248424852486248724882489119 行未改动全部展开260926102611261226132614261526162617130 行未改动全部展开27482749275027512752275327542755275698 行未改动28552856285728582859286028612862286384 行未改动294829492950295129522953295429552956156 行未改动全部展开31133114311531163117311831193120312165 行未改动31873188318931903191319231933194319565 行未改动326132623263326432653266326732683269144 行未改动全部展开34143415341634173418341934203421342270 行未改动34933494349534963497349834993500350183 行未改动省选爆炸后开始严肃补知识点。先写一些分治,然后做题。## 二区间合并考虑如下问题:36 行未改动**最值分治**是一种特殊的二区间合并。其处理的问题常常与最值结构相关,因此我们将二区间合并中的 $m$ 改成区间 $[l, r]$ 中最值出现的位置,然后统计最值到两侧的贡献。[笛卡尔树](https://www.luogu.com.cn/problem/P5854)是一种典型的最值分治结构。它是一棵二叉树,每个节点有权值二元组 $(i,a_i)$,要求下标 $i$ 满足二叉搜索树性质,权值 $a_i$ 满足堆性质。笛卡尔树可在 $O(n)$ 时间内构建。顺序插入每个 $a_i$,可知新节点一定位于当前根的右链上。自底向上比较右链节点的权值,若满足偏序关系,则将新节点挂为右子,原右子树变为其左子树。可用单调栈维护右链。参考代码:```cppint n, a[N], l[N], r[N];void build() {stack<int> st;for (int i = 1; i <= n; i++) {43 行未改动更一般地,设网格图的面积为 $S$,询问数量为 $q$,则时间复杂度为 $O\left( (S+q) \sqrt{S} \log S\right)$。而如果最短路部分可以加速到线性,复杂度可以优化到 $O\left((S+q) \sqrt{S}\right)$。:::::info[P3350 代码]```cpp line-numbersconst int N = 1e5 + 5, inf = 1e9;int n, m, q, ans[N];using query = tuple<int, int, int, int, int>; // {x1, y1, x2, y2, id}99 行未改动对于本题,我们还要做 $q$ 次询问,总复杂度是 $O(nq \log n)$ 的。由于点分治的 $O(n \log n)$ 常数很大,我们应该把所有询问一起处理,而不是每次询问点分一次。:::::info[P3806 代码]```cpp line-numbersconst int N = 1e4 + 5, K = 1e7 + 5;int n, q, cur, ctr, k[N], sz[N], vis[N], ans[N];vector<pair<int, int>> e[N];89 行未改动可以发现上述算法的时间复杂度为 $O(n \log n \log k)$。:::::info[P5787 代码]```cppconst int N = 1e5 + 5;int n, m, k;struct dsu {int fa[N << 1], sz[N << 1];101 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 4e4 + 5, M = 205, Q = 2e5 + 5;using node = tuple<int, int, int, int>; // {l, r, V, id}int n, q, v[N], w[N], ans[Q];116 行未改动全部展开懒得半群优化了,视 $n,m$ 同阶,复杂度 $O(n \log^2 n+q \log n)$ 带 $8$ 倍常数。::::::::::info[代码]```cpp line-numbersconst int N = 5e4 + 5;const i64 inf = 1e15;using query = tuple<int, int, int, int, int>;145 行未改动全部展开::::::::::info[代码]```cpp line-numbersint n, a[N], rt, l[N], r[N], sz[N], f[N];void build() {stack<int> st;for (int i = 1; i <= n; i++) {43 行未改动::::::::::info[代码]```cpp line-numbers#define int long longconst int N = 2e5 + 5;using node = pair<int, int>;54 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 505, Q = 6e5 + 5;using query = tuple<int, int, int, int, int>; // {x1, y1, x2, y2, id}int n, m, q;147 行未改动全部展开我们可以用简单的 DP 跑出中线向两侧的最短路,复杂度就是 $O((n+q)\sqrt{n})$ 的。::::::::::info[代码]```cpp line-numbersconst int N = 6e5 + 5;const int64 inf = 1e18;int n, k, q;154 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 2e5 + 5;const int64 inf = 1e18;int n, k, q;108 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 1.5e5 + 5;const i64 inf = 1e18;using node = array<i64, 3>;using point = tuple<i64, i64, i64, i64, int>; // {x, y, f, g, type}149 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 4e4 + 5;int n, k, cur, ctr, ans, sz[N], vis[N];vector<pair<int, int>> e[N];65 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5;int n, X, Y, cur, ctr, sz[N], vis[N];i64 ans, tr[N];void add(int x, i64 c) {89 行未改动:::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5;using query = tuple<int, int, int, int>; // {l, r, x, id}using point = tuple<int, int, int>; // {x, y, col}128 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 1005;int n, p[N], sz[N], vis[N];vector<int> e[N];bool ask(int x, int y, int z) {71 行未改动::::::::::info[代码]```cppconst int N = 3e5 + 5;int n, q, k;struct unionfind {int fa[N], sz[N];83 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5, M = 1e3 + 5;const mint base = 10000019;int n, k, q, cnt, cur;tuple<int, int, int, int> e[N];67 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 6e5 + 5;int n, q, last[N], ins[N];vector<int> e[N];113 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 7e4 + 5, inf = 1e9;using modify = pair<int, int>; // {id, w}using edge = tuple<int, int, int>; // {u, v, id}using wedge = tuple<int, int, int, bool, int>; // {w, u, v, in E1, id}140 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 2e5 + 5, inf = 1e9;using node = pair<int, int>; // {val, id}int n, k, q, fa[N];158 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5;int n, q, a[N], pos[N], t[N];i64 cnt[N];77 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 8e5 + 5;int n, m, q;vector<int> vec;struct node {119 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 5e4 + 5;int n, m, d[N];using info = pair<int, double>;130 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 1e6 + 5, inf = 1e9;int n, a[N], b[N];using node = pair<int, mint>;node operator+ (const node& a, const node& b) {98 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 1e6 + 5, inf = 1e9;struct node {int x, y, id, type;} a[N], b[N];84 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5, M = 1.4e6 + 5;using query = tuple<int, int, int>; // {x, l, r}struct info {int x, y, id, v;156 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5;int n, w, a[N], cnt;mincost_flow<N, N> G;65 行未改动经过卡常的代码在洛谷 3s 时限下直接获得了 [100 分](https://www.luogu.com.cn/record/267036229)。在 QOJ 上获得了 [75 分](https://qoj.ac/submission/2130501)。::::info[代码]```cpp line-numbersconst int N = 5e4 + 5;const i64 inf = 1e18;static int n, q, L, R;65 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 5e4 + 5, B = 64;const i64 inf = 1e18;static int n, q, L, R, len[N];144 行未改动全部展开::::::::::info[代码]```cpp line-numbersconst int N = 2005;long long n, k;mint x, c[N][N];70 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 1e5 + 5;using edge = tuple<int, int, int>;i64 cur, ans[N];83 行未改动1234536 行未改动424344454647484950515243 行未改动9697989910010110210310499 行未改动20420520620720820921021121289 行未改动302303304305306307308309310101 行未改动全部展开412413414415416417418419420116 行未改动全部展开537538539540541542543544545145 行未改动全部展开69169269369469569669769869943 行未改动74374474574674774874975075154 行未改动806807808809810811812813814147 行未改动全部展开962963964965966967968969970154 行未改动全部展开112511261127112811291130113111321133108 行未改动全部展开124212431244124512461247124812491250149 行未改动全部展开14001401140214031404140514061407140865 行未改动14741475147614771478147914801481148289 行未改动157215731574157515761577157815791580128 行未改动全部展开17091710171117121713171417151716171771 行未改动17891790179117921793179417951796179783 行未改动18811882188318841885188618871888188967 行未改动195719581959196019611962196319641965113 行未改动全部展开207920802081208220832084208520862087140 行未改动全部展开222822292230223122322233223422352236158 行未改动全部展开23952396239723982399240024012402240377 行未改动248124822483248424852486248724882489119 行未改动全部展开260926102611261226132614261526162617130 行未改动全部展开27482749275027512752275327542755275698 行未改动28552856285728582859286028612862286384 行未改动294829492950295129522953295429552956156 行未改动全部展开31133114311531163117311831193120312165 行未改动31873188318931903191319231933194319565 行未改动326132623263326432653266326732683269144 行未改动全部展开34143415341634173418341934203421342270 行未改动34933494349534963497349834993500350183 行未改动先写一些分治,然后做题。## 二区间合并考虑如下问题:36 行未改动**最值分治**是一种特殊的二区间合并。其处理的问题常常与最值结构相关,因此我们将二区间合并中的 $m$ 改成区间 $[l, r]$ 中最值出现的位置,然后统计最值到两侧的贡献。[笛卡尔树](https://www.luogu.com.cn/problem/P5854)是一种典型的最值分治结构。它是一棵二叉树,每个节点有权值二元组 $(i,a_i)$,要求下标 $i$ 满足二叉搜索树性质,权值 $a_i$ 满足堆性质。笛卡尔树可在 $O(n)$ 时间内构建。顺序插入每个 $a_i$,可知新节点一定位于当前根的右链上。自底向上比较右链节点的权值,若满足偏序关系,则将新节点挂为右子,原右子树变为其左子树。用单调栈维护右链。```cpp line-numbersint n, a[N], l[N], r[N];void build() {stack<int> st;for (int i = 1; i <= n; i++) {43 行未改动更一般地,设网格图的面积为 $S$,询问数量为 $q$,则时间复杂度为 $O\left( (S+q) \sqrt{S} \log S\right)$。而如果最短路部分可以加速到线性,复杂度可以优化到 $O\left((S+q) \sqrt{S}\right)$。:::::info[P3350 代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5, inf = 1e9;int n, m, q, ans[N];using query = tuple<int, int, int, int, int>; // {x1, y1, x2, y2, id}99 行未改动对于本题,我们还要做 $q$ 次询问,总复杂度是 $O(nq \log n)$ 的。由于点分治的 $O(n \log n)$ 常数很大,我们应该把所有询问一起处理,而不是每次询问点分一次。:::::info[P3806 代码]```cpp line-numbers line-numbersconst int N = 1e4 + 5, K = 1e7 + 5;int n, q, cur, ctr, k[N], sz[N], vis[N], ans[N];vector<pair<int, int>> e[N];89 行未改动可以发现上述算法的时间复杂度为 $O(n \log n \log k)$。:::::info[P5787 代码]```cpp line-numbersconst int N = 1e5 + 5;int n, m, k;struct dsu {int fa[N << 1], sz[N << 1];101 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 4e4 + 5, M = 205, Q = 2e5 + 5;using node = tuple<int, int, int, int>; // {l, r, V, id}int n, q, v[N], w[N], ans[Q];116 行未改动全部展开懒得半群优化了,视 $n,m$ 同阶,复杂度 $O(n \log^2 n+q \log n)$ 带 $8$ 倍常数。::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 5e4 + 5;const i64 inf = 1e15;using query = tuple<int, int, int, int, int>;145 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersint n, a[N], rt, l[N], r[N], sz[N], f[N];void build() {stack<int> st;for (int i = 1; i <= n; i++) {43 行未改动::::::::::info[代码]```cpp line-numbers line-numbers#define int long longconst int N = 2e5 + 5;using node = pair<int, int>;54 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 505, Q = 6e5 + 5;using query = tuple<int, int, int, int, int>; // {x1, y1, x2, y2, id}int n, m, q;147 行未改动全部展开我们可以用简单的 DP 跑出中线向两侧的最短路,复杂度就是 $O((n+q)\sqrt{n})$ 的。::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 6e5 + 5;const int64 inf = 1e18;int n, k, q;154 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 2e5 + 5;const int64 inf = 1e18;int n, k, q;108 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1.5e5 + 5;const i64 inf = 1e18;using node = array<i64, 3>;using point = tuple<i64, i64, i64, i64, int>; // {x, y, f, g, type}149 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 4e4 + 5;int n, k, cur, ctr, ans, sz[N], vis[N];vector<pair<int, int>> e[N];65 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5;int n, X, Y, cur, ctr, sz[N], vis[N];i64 ans, tr[N];void add(int x, i64 c) {89 行未改动:::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5;using query = tuple<int, int, int, int>; // {l, r, x, id}using point = tuple<int, int, int>; // {x, y, col}128 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1005;int n, p[N], sz[N], vis[N];vector<int> e[N];bool ask(int x, int y, int z) {71 行未改动::::::::::info[代码]```cpp line-numbersconst int N = 3e5 + 5;int n, q, k;struct unionfind {int fa[N], sz[N];83 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5, M = 1e3 + 5;const mint base = 10000019;int n, k, q, cnt, cur;tuple<int, int, int, int> e[N];67 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 6e5 + 5;int n, q, last[N], ins[N];vector<int> e[N];113 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 7e4 + 5, inf = 1e9;using modify = pair<int, int>; // {id, w}using edge = tuple<int, int, int>; // {u, v, id}using wedge = tuple<int, int, int, bool, int>; // {w, u, v, in E1, id}140 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 2e5 + 5, inf = 1e9;using node = pair<int, int>; // {val, id}int n, k, q, fa[N];158 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5;int n, q, a[N], pos[N], t[N];i64 cnt[N];77 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 8e5 + 5;int n, m, q;vector<int> vec;struct node {119 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 5e4 + 5;int n, m, d[N];using info = pair<int, double>;130 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e6 + 5, inf = 1e9;int n, a[N], b[N];using node = pair<int, mint>;node operator+ (const node& a, const node& b) {98 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e6 + 5, inf = 1e9;struct node {int x, y, id, type;} a[N], b[N];84 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5, M = 1.4e6 + 5;using query = tuple<int, int, int>; // {x, l, r}struct info {int x, y, id, v;156 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5;int n, w, a[N], cnt;mincost_flow<N, N> G;65 行未改动经过卡常的代码在洛谷 3s 时限下直接获得了 [100 分](https://www.luogu.com.cn/record/267036229)。在 QOJ 上获得了 [75 分](https://qoj.ac/submission/2130501)。::::info[代码]```cpp line-numbers line-numbersconst int N = 5e4 + 5;const i64 inf = 1e18;static int n, q, L, R;65 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 5e4 + 5, B = 64;const i64 inf = 1e18;static int n, q, L, R, len[N];144 行未改动全部展开::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 2005;long long n, k;mint x, c[N][N];70 行未改动::::::::::info[代码]```cpp line-numbers line-numbersconst int N = 1e5 + 5;using edge = tuple<int, int, int>;i64 cur, ans[N];83 行未改动