正文
增加 258 行删除 240 行
1234567891045 行未改动56575859606162636479 行未改动14414514614714814915015115215315415515615715815916016116216310 行未改动17417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540624 行未改动431432433434435436437438439440441442443444445446178 行未改动全部展开62562662762862963063163266 行未改动699700701702703704705706707135 行未改动全部展开843844845846847[P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)。[查看 OI Wiki 上的 K-D Tree 页面](https://oiwiki.org/ds/kdt/)。update:感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021) dsa 的指正,替罪羊树维护 K-D Tree 的插入操作复杂度其实是假的,已在下文修正。## 写在前面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$。::::## 查询79 行未改动## 插入元素前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 K-D Tree 的平衡性。哪怎么办呢?我们可以每插入一个节点就重构一次,解决方法有很多,比如二进制分组、根号分治等。由于替罪羊树做法的时间复杂度不能严格保证,这里主要介绍较为常见的二进制分组的做法。::::info[替罪羊树]:::warning[关于复杂度]{open}由于 K-D Tree 的时间复杂度极度依赖树高,只有在树高严格为 $\log n + O(1)$ 时,才能保证复杂度的正确性,但替罪羊树维护 K-D Tree 的树高为 $O(\log n)$,所以不能严格保证复杂度的正确性,读者可以自行阅读 [OI wiki 上相关的介绍](https://oiwiki.org/ds/kdt/#%E5%A4%8D%E6%9D%82%E5%BA%A6%E5%88%86%E6%9E%90)。但由于替罪羊树存在一定的思维价值(~~真不是是我舍不得删~~),此处保留替罪羊树的维护方法。再次感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021) dsa 的指正。:::替罪羊树是平衡树的一种,它的基本思想是每次插入一个节点后,检查树是否失衡,如果失衡则拍扁重构。拍扁重构的过程是先对树进行中序遍历,将节点取出,然后重新构建一棵树。考虑到 K-D Tree 是不可旋转的,我们想到使用**替罪羊树**维护 K-D Tree 的平衡性,每次 K-D Tree 失衡就拍扁重构。10 行未改动u = build(0 , (int)pool.size() - 1 , dep);}```附使用替罪羊树通过 [P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312) 的代码::::success[替罪羊树][提交记录](https://www.luogu.com.cn/record/283656694)。```cpp#include <bits/stdc++.h>#define N 200010using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗const double ALPHA = 0.7;int K;struct Node{LL x[3] , val , sum , lazy;LL mn[3] , mx[3];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 apply(int u , LL v){t[u].val += v;t[u].sum += v * t[u].sz;t[u].lazy += v;}inline void pushdown(int u){if(t[u].lazy){LL v = t[u].lazy;if(t[u].lc)apply(t[u].lc , v);if(t[u].rc)apply(t[u].rc , v);t[u].lazy = 0;}}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;pushdown(u);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);t[u].lazy = 0;pushup(u);return u;}void rebuild(int &u , int dep){pool.clear();collect(u);if(pool.empty()){u = 0;return;}u = build(0 , (int)pool.size() - 1 , dep);}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].lazy = 0;t[tot].lc = t[tot].rc = 0;return tot;}void insert(int &u , int node , int dep){if(!u){u = node;return;}pushdown(u);int dim = dep % K;if(t[node].x[dim] < t[u].x[dim])insert(t[u].lc , node , dep + 1);elseinsert(t[u].rc , node , dep + 1);pushup(u);int lsz = t[u].lc ? t[t[u].lc].sz : 0;int rsz = t[u].rc ? t[t[u].rc].sz : 0;if(max(lsz , rsz) > ALPHA * t[u].sz)rebuild(u , dep);}inline void add(int u , LL *L , LL *R , LL v , int dep){if(!u) return;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){apply(u , v);return;}for(int d = 0 ; d < K ; ++d)if(t[u].mn[d] > R[d] || t[u].mx[d] < L[d])return;pushdown(u);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)t[u].val += v;add(t[u].lc , L , R , v , dep + 1);add(t[u].rc , L , R , v , dep + 1);pushup(u);}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;pushdown(u);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 rt = 0;int m;LL lst = 0 , L[3] , R[3] , v , A[10];int main(){scanf("%d%d" , &K , &m);while(m--){int op;scanf("%d" , &op);if(op == 1){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &A[d]);A[d] ^= lst;}scanf("%lld" , &v);v ^= lst;int node = new_node(A , v);insert(rt , node , 0);}else if(op == 2){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);L[d] ^= lst;}for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= lst;}scanf("%lld" , &v);v ^= lst;add(rt , L , R , v , 0);}else{for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);L[d] ^= lst;}for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= lst;}LL ans = query(rt , L , R , 0);printf("%lld\n" , ans);lst = ans;}}return 0;}```:::::::### 二进制分组二进制分组是一种将动态插入问题转化为若干个静态数据结构维护的技巧。24 行未改动## 复杂度分析| 复杂度维度 | 静态 K-D Tree | 二进制分组 | 替罪羊树重构(理论复杂度,不严格保证) || :--- | :--- | :--- | :--- || **静态建树** | $O(n \log n)$ | 无需建树,初始为空 | 无需建树,动态插入 || **单点插入**(摊还时间复杂度) | 不支持动态插入 | $O(\log n)$ | $O(\log^2 n)$ || **区间查询** | $O(n^{1-\frac{1}{k}})$ | $O(n^{1-\frac{1}{k}} \log n)$ | 最坏 $O(n)$ || **空间复杂度** | $O(n)$ | $O(n)$(常数略大) | $O(n)$ || **重构常数** | — | 较大 | 较小 |注:复杂度分析中 $K$ 视为常数。## [P4148 简单题](https://www.luogu.com.cn/problem/P4148)178 行未改动全部展开## [P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)K-D Tree 模板,要求动态插入、区间修改、区间查询,使用二进制分组维护即可。[提交记录](https://www.luogu.com.cn/record/283933340)。```cpp#include <bits/stdc++.h>66 行未改动t[u].lazy = 0;pushup(u);return u;}int new_node(LL *x , LL v){//new是保留字/lltot += 1;for(int d = 0 ; d < K ; ++d){t[tot].x[d] = x[d];t[tot].mn[d] = t[tot].mx[d] = x[d];135 行未改动全部展开return 0;}```最后的最后,再次感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021)。12345678910111245 行未改动58596061626364656679 行未改动14614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517610 行未改动18718818919019119219319424 行未改动219220221222223224225226227228229230231232233234235178 行未改动全部展开41441541641741841942042142242366 行未改动490491492493494495496497498135 行未改动全部展开634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865[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$。::::## 查询79 行未改动## 插入元素前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 K-D Tree 的平衡性。哪怎么办呢?我们可以每插入一个节点就重构一次,解决方法有很多,比如二进制分组、根号分治等。### 替罪羊树::::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 失衡就拍扁重构。10 行未改动u = build(0 , (int)pool.size() - 1 , dep);}```### 二进制分组二进制分组是一种将动态插入问题转化为若干个静态数据结构维护的技巧。24 行未改动## 复杂度分析| 复杂度维度 | 静态 K-D Tree | 二进制分组 | 替罪羊树重构 || :--- | :--: | :--: | :--: || **静态建树** | $O(n \log 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)178 行未改动全部展开## [P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)K-D Tree 模板,要求动态插入、区间修改、区间查询,使用二进制分组维护即可。### 二进制分组[提交记录](https://www.luogu.com.cn/record/283933340)。```cpp#include <bits/stdc++.h>66 行未改动t[u].lazy = 0;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];135 行未改动全部展开return 0;}```### 替罪羊树[提交记录](https://www.luogu.com.cn/record/283656694)。```cpp#include <bits/stdc++.h>#define N 200010using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗const double ALPHA = 0.7;int K;struct Node{LL x[3] , val , sum , lazy;LL mn[3] , mx[3];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 apply(int u , LL v){t[u].val += v;t[u].sum += v * t[u].sz;t[u].lazy += v;}inline void pushdown(int u){if(t[u].lazy){LL v = t[u].lazy;if(t[u].lc)apply(t[u].lc , v);if(t[u].rc)apply(t[u].rc , v);t[u].lazy = 0;}}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;pushdown(u);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);t[u].lazy = 0;pushup(u);return u;}void rebuild(int &u , int dep){pool.clear();collect(u);if(pool.empty()){u = 0;return;}u = build(0 , (int)pool.size() - 1 , dep);}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].lazy = 0;t[tot].lc = t[tot].rc = 0;return tot;}void insert(int &u , int node , int dep){if(!u){u = node;return;}pushdown(u);int dim = dep % K;if(t[node].x[dim] < t[u].x[dim])insert(t[u].lc , node , dep + 1);elseinsert(t[u].rc , node , dep + 1);pushup(u);int lsz = t[u].lc ? t[t[u].lc].sz : 0;int rsz = t[u].rc ? t[t[u].rc].sz : 0;if(max(lsz , rsz) > ALPHA * t[u].sz)rebuild(u , dep);}inline void add(int u , LL *L , LL *R , LL v , int dep){if(!u) return;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){apply(u , v);return;}for(int d = 0 ; d < K ; ++d)if(t[u].mn[d] > R[d] || t[u].mx[d] < L[d])return;pushdown(u);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)t[u].val += v;add(t[u].lc , L , R , v , dep + 1);add(t[u].rc , L , R , v , dep + 1);pushup(u);}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;pushdown(u);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 rt = 0;int m;LL lst = 0 , L[3] , R[3] , v , A[10];int main(){scanf("%d%d" , &K , &m);while(m--){int op;scanf("%d" , &op);if(op == 1){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &A[d]);A[d] ^= lst;}scanf("%lld" , &v);v ^= lst;int node = new_node(A , v);insert(rt , node , 0);}else if(op == 2){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);L[d] ^= lst;}for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= lst;}scanf("%lld" , &v);v ^= lst;add(rt , L , R , v , 0);}else{for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);L[d] ^= lst;}for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= lst;}LL ans = query(rt , L , R , 0);printf("%lld\n" , ans);lst = ans;}}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) 的复杂度证明。