版本比较
题解:P14312 【模板】K-D Tree
正文
增加 234 行删除 41 行
112345678910111245 行未改动5859606156626364656677 行未改动14414514614714214814915014515114714815215315415515615715815916016116216316416516616716816917017117217317417517617717817918023 行未改动20420520620718218318420820918518618721021121221321421521621719621821919922020120220320420520620720820922122222321322422521521622622722822921823023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441522441641722641841922842042142242342465 行未改动49049149249330249449549649749873 行未改动57257357457538438538657657738738838957857958058151 行未改动633634635636449637451638639640641642210 行未改动全部展开853854855856670857672858859860861862863864865[P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)[P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)。
[查看 OI Wiki 上的 K-D Tree 页面](https://oi-wiki.org/ds/kdt/)。
update:感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021) dsa 的指正,替罪羊树维护 K-D Tree 的插入操作复杂度其实是假的,已在下文修正。
update:你说的对,但是 @[MZMTab](https://www.luogu.com.cn/user/1046687) 成功证明了替罪羊树维护 K-D Tree 的复杂度,让我们拜谢 ta。
## 写在前面
K-D Tree 全称 K-dimensional 树,是一种高效维护 $k$ 维数据的数据结构,可以以非常优秀的时间复杂度实现插入、修改、查询等操作。相较 cdq 分治,其可以在线处理问题;而相较于树套树,其又更节省空间,~~是考场上骗分的一大利器~~。
45 行未改动::::info[`nth_element` 是什么?]
`nth_element` 是 STL 中的函数,用于将第 $k$ 小(或大)的元素放到第 $k$ 个位置,其余元素按顺序排列。具体实现是双指针,时间复杂度 $O(n)$。
`nth_element(l , x , r , cmp)` 表示将 $[l,r)$ 区级的所有元素按 cmp 排序,$x$ 前面的元素严格小于 $x$,$x$ 后面的元素严格大于 $x$。`nth_element(l , x , r , cmp)` 表示将 $[l,r)$ 区间的所有元素按 cmp 排序,$x$ 前面的元素严格小于 $x$,$x$ 后面的元素严格大于 $x$。
::::
## 查询77 行未改动```
## 插入元素
前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 K-D Tree 的平衡性。哪怎么办呢?我们可以每插入一个节点就重构一次,解决方法有很多,这里介绍替罪羊树和二进制分组的做法,~~我是不可能会集训队论文做法的~~。前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 K-D Tree 的平衡性。哪怎么办呢?我们可以每插入一个节点就重构一次,解决方法有很多,比如二进制分组、根号分治等。
### 替罪羊树考虑到 K-D Tree 是不可旋转的,我们可以考虑使用**替罪羊树**维护 K-D Tree 的平衡性。每次 K-D Tree 失衡就拍扁重构。
::::info[替罪羊树是什么]替罪羊树是平衡树的一种,它的基本思想是每次插入一个节点后,检查树是否失衡,如果失衡则拍扁重构。拍扁重构的过程是先对树进行中序遍历,将节点取出,然后重新构建一棵树。::::success[关于复杂度]{open}
:::error[旧的分析]
**请注意此处的分析已经过时。**
由于 K-D Tree 的时间复杂度极度依赖树高,只有在树高严格为 $\log n + O(1)$ 时,才能保证复杂度的正确性,但替罪羊树维护 K-D Tree 的树高为 $O(\log n)$,所以不能严格保证复杂度的正确性。:::
详细的解释读者可以自行阅读 《[基于替罪羊树维护的 K‑D Tree 复杂度证明](https://www.luogu.com.cn/article/shh0v8zb)》。
简单来说 $k=2,\alpha=0.6$ 时,查询的复杂度约为 $O(n^{0.57})$.
更具体的,$\alpha\in(0.5,0.75]$ 时,查询的复杂度为 $O(n^p)$($p\in(0.6,0.68]$),重构的复杂度为 $O(\frac{1-\alpha}{\alpha-0.5}\log N\log_{\alpha^{-1}} N)$,其中 $N$ 为整棵树最多的结点树。
同时,@[MZMTab](https://www.luogu.com.cn/user/1046687) 还证明了 $n=10^5$,插入、查询比例为 $1:1$ 时,$\alpha\approx 0.63$ 最优。
注:文中 $\alpha$ 为替罪羊树的平衡因子。
::::
替罪羊树是平衡树的一种,它的基本思想是每次插入一个节点后,检查树是否失衡,如果失衡则拍扁重构。拍扁重构的过程是先对树进行中序遍历,将节点取出,然后重新构建一棵树。
考虑到 K-D Tree 是不可旋转的,我们想到使用**替罪羊树**维护 K-D Tree 的平衡性,每次 K-D Tree 失衡就拍扁重构。
参考代码:
```cppinline void rebuild(int &u, int dep){23 行未改动 while(pos < 25 && roots[pos]){ pool.clear(); collect(roots[pos]); collect(cur1); if(!pool.empty()){ pool.insert(pool.begin() , 0); cur1 = build(1 , pool.size() - 1 , 0); pool.insert(pool.begin() , 0); cur1 = build(1 , pool.size() - 1 , 0); } else cur1 = 0; cur2 += szs[pos]; roots[pos] = 0; szs[pos] = 0; pos += 1; } roots[pos] = cur1; szs[pos] = cur2;}int m;```
### 替罪羊树 or 二进制分组?
| 维度 | 二进制分组 | 替罪羊树重构 || :--- | :--- | :--- || **复杂度** | 常数较小 | 常数略大 || **单次插入复杂度** | 摊还 $O(\log n)$ | 摊还 $O(\log^2 n)$ || **单次查询复杂度** | $O(\sqrt{n} \log n)$ | $O(\sqrt{n})$ || **删除** | 很难实现删除 | 可以 || **空间复杂度** | 略大,同时维护 $O(\log n)$ 棵树 | 较小,仅维护一棵树 || **重构常数** | 较大。合并时需拍平两棵完整子树并重建。 | 较小。重构局部子树,但在如根节点失衡等极端情况会重构整棵树。 |
## 复杂度分析
| 复杂度维度 | 静态 K-D Tree | 二进制分组 | 替罪羊树重构 || :--- | :--- | :--- | :--- || :--- | :--: | :--: | :--: || **静态建树** | $O(n \log n)$ | 无需建树,初始为空 | 无需建树,动态插入 || **单点插入**(摊还时间复杂度) | 不支持动态插入 | ${O(\log n)}$ | ${O(\log^2 n)}$ || **区间查询** | $O(\sqrt{n})$ | ${O(\sqrt{n} \log n)}$ | ${O(\sqrt{n})}$ || **单点插入**(摊还时间复杂度) | 不支持动态插入 | $O(\log^2 n)$ | < || **区间查询** | $O(n^{1-\frac{1}{k}})$ | $O(n^{1-\frac{1}{k}} \log n)$ | $O(n^p)$ || **重构** | — | < | $O(\frac{1-\alpha}{\alpha-0.5}\log N\log_{\alpha^{-1}} N)$ || **空间复杂度** | $O(n)$ | $O(n)$(常数略大) | $O(n)$ || **重构常数** | — | 较大 | 较小 |
注:复杂度分析中 $K$ 视为常数。
## [P4148 简单题](https://www.luogu.com.cn/problem/P4148)
[P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312) 的弱化版,要求维护一个静态 K-D Tree(20M 的空间卡掉了所有树套树,强制在线卡掉了 CDQ 分治)支持单点修改和区间查询,建议读者通过本题后再挑战 P14312。
::::success[AC code]```cpp#include <bits/stdc++.h>#define N 200010#define K 2using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗struct Node{ LL x[K] , val , sum; LL mn[K] , mx[K]; int lc , rc , sz;}t[N];int tot , cmp_;bool cmp(int a , int b){ return t[a].x[cmp_] < t[b].x[cmp_];}inline void pushup(int u){ Node &p = t[u]; p.sz = 1; p.sum = p.val; if(p.lc) p.sz += t[p.lc].sz, p.sum += t[p.lc].sum; if(p.rc) p.sz += t[p.rc].sz, p.sum += t[p.rc].sum; for(int d = 0 ; d < K ; ++d){ p.mn[d] = p.mx[d] = p.x[d]; if(p.lc) p.mn[d] = min(p.mn[d] , t[p.lc].mn[d]), p.mx[d] = max(p.mx[d] , t[p.lc].mx[d]); if(p.rc) p.mn[d] = min(p.mn[d] , t[p.rc].mn[d]), p.mx[d] = max(p.mx[d] , t[p.rc].mx[d]); }}vector<int> pool;inline void collect(int u){ if(!u) return; collect(t[u].lc); pool.push_back(u); collect(t[u].rc);}int build(int l , int r , int dep){ if(l > r) return 0; int mid = (l + r) >> 1; cmp_ = dep % K; nth_element(pool.begin() + l , pool.begin() + mid , pool.begin() + r + 1 , cmp); int u = pool[mid]; t[u].lc = build(l , mid - 1 , dep + 1); t[u].rc = build(mid + 1 , r , dep + 1); pushup(u); return u;}int new_node(LL *x , LL v){ tot += 1; for(int d = 0 ; d < K ; ++d){ t[tot].x[d] = x[d]; t[tot].mn[d] = t[tot].mx[d] = x[d]; } t[tot].val = t[tot].sum = v; t[tot].sz = 1; t[tot].lc = t[tot].rc = 0; return tot;}LL query(int u , LL *L , LL *R , int dep){ if(!u) return 0; bool flag = true; for(int d = 0 ; d < K ; ++d){ if(t[u].mn[d] < L[d] || t[u].mx[d] > R[d]){ flag = false; break; } } if(flag) return t[u].sum; for(int d = 0 ; d < K ; ++d) if(t[u].mn[d] > R[d] || t[u].mx[d] < L[d]) return 0; LL res = 0; flag = true; for(int d = 0 ; d < K ; ++d){ if(t[u].x[d] < L[d] || t[u].x[d] > R[d]){ flag = false; break; } } if(flag) res += t[u].val; res += query(t[u].lc , L , R , dep + 1); res += query(t[u].rc , L , R , dep + 1); return res;}int roots[25] , szs[25];inline void insert(LL *x , LL v){ int node = new_node(x , v); int cur1 = node , cur2 = 1; int pos = 0; while(pos < 25 && roots[pos]){ pool.clear(); collect(roots[pos]); collect(cur1); if(!pool.empty()){ pool.insert(pool.begin() , 0); cur1 = build(1 , pool.size() - 1 , 0); } else cur1 = 0; cur2 += szs[pos]; roots[pos] = 0; szs[pos] = 0; pos += 1; } roots[pos] = cur1; szs[pos] = cur2;}bool check_(int u , LL *x , LL v , int dep){ if(!u) return false; bool same = true; for(int d = 0 ; d < K ; ++d) if(t[u].x[d] != x[d]){ same = false; break; } if(same){ t[u].val += v; t[u].sum += v; return true; } int d = dep % K; bool found = false; if(x[d] <= t[u].x[d]) found = check_(t[u].lc , x , v , dep + 1); if(!found && x[d] >= t[u].x[d]) found = check_(t[u].rc , x , v , dep + 1); if(found) pushup(u); return found;}bool check(LL *x , LL v){ for(int i = 0 ; i < 25 ; ++i) if(roots[i] && check_(roots[i] , x , v , 0)) return true; return false;}int n;LL lst = 0;int main(){ scanf("%d" , &n); int op; while(true){ int op; scanf("%d" , &op); if(op == 1){ LL x[2] , v; scanf("%lld%lld%lld" , &x[0] , &x[1] , &v); x[0] ^= lst; x[1] ^= lst; v ^= lst; if(!check(x , v)) insert(x , v); } else if(op == 2){ LL L[2] , R[2]; scanf("%lld%lld%lld%lld" , &L[0] , &L[1] , &R[0] , &R[1]); L[0] ^= lst; L[1] ^= lst; R[0] ^= lst; R[1] ^= lst; LL ans = 0; for(int i = 0 ; i < 25 ; ++i) if(roots[i]) ans += query(roots[i] , L , R , 0); printf("%lld\n" , ans); lst = ans; } else break; } return 0;}```
~~其实我是先过了 P14312 才过的这道题,本份代码由 P14312 修改而来~~。
::::
## [P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)
K-D Tree 模板。K-D Tree 模板,要求动态插入、区间修改、区间查询,使用二进制分组维护即可。
::::success[二进制分组]### 二进制分组
[提交记录](https://www.luogu.com.cn/record/283656690)。[提交记录](https://www.luogu.com.cn/record/283933340)。
```cpp#include <bits/stdc++.h>#define N 20001065 行未改动 t[u].lazy = 0; pushup(u); return u;}int new_node(LL *x , LL v){//new是保留字/llint new_node(LL *x , LL v){ tot += 1; for(int d = 0 ; d < K ; ++d){ t[tot].x[d] = x[d]; t[tot].mn[d] = t[tot].mx[d] = x[d];73 行未改动 while(pos < 25 && roots[pos]){ pool.clear(); collect(roots[pos]); collect(cur1); if(!pool.empty()){ pool.insert(pool.begin() , 0); cur1 = build(1 , pool.size() - 1 , 0); pool.insert(pool.begin() , 0); cur1 = build(1 , pool.size() - 1 , 0); } else cur1 = 0; cur2 += szs[pos]; roots[pos] = 0; szs[pos] = 0; pos += 1;51 行未改动 } return 0;}```::::
::::success[替罪羊树]### 替罪羊树
[提交记录](https://www.luogu.com.cn/record/283656694)。
```cpp210 行未改动全部展开 } return 0;}```::::
可以看到替罪羊树还是比二进制分组略快的。## 参考资料与致谢
1. OI Wiki K-D Tree https://oi-wiki.org/ds/kdt/2. 基于替罪羊树维护的 K-D Tree 复杂度证明 https://www.luogu.com.cn/article/shh0v8zb3. P4148 简单题 https://www.luogu.com.cn/problem/P41484. P14312 【模板】K-D Tree https://www.luogu.com.cn/problem/P14312
由衷地感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021) 的指正和 @[MZMTab](https://www.luogu.com.cn/user/1046687) 的复杂度证明。