正文
增加 338 行删除 142 行
1234567891045 行未改动56575859606162636477 行未改动14214314414514614714814915015115215315415515615715815916016110 行未改动17217317417517617717817918018118218318418518618718818919019119239 行未改动2322332342352362372382392409 行未改动2502512522532542552562572582592602612622632642652664 行未改动27127227327427527627727827928028128228328428528628728828929029129229329429529629715 行未改动3133143153163173183193203213223233243253263273283293303313323333343354 行未改动3403413423433443453463473483493503513523536 行未改动3603613623633643653663673683694 行未改动3743753763773783793803813822 行未改动38538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146239 行未改动5025035045055065075085095105119 行未改动5215225235245255265275285293 行未改动53353453553653753853954054154254315 行未改动5595605615625635645655665675685695705715725735745755765775785795805815825834 行未改动5885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206 行未改动6276286296306316326336346354 行未改动6406416426436446456466476486496502 行未改动653654655656657658659660661662663664665666667668669[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$。::::## 查询77 行未改动```## 插入元素前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 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];39 行未改动}}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);9 行未改动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];4 行未改动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;15 行未改动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);4 行未改动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);6 行未改动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]);4 行未改动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]);2 行未改动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;}```:::::::### 二进制分组二进制分组是一种将动态插入问题转化为若干个静态数据结构维护的技巧。二进制分组本质上模拟的是二进制计数器的进位过程。我们把已插入的点集拆分成若干个互不相交的静态 K-D Tree,所有树的大小都是 $2$ 的幂,第 $i$ 棵树的规模非 $0$ 即 $2^i$。插入一个新点时,先将其单独视为一棵规模为 $1$(即 $2^0$)的树,尝试放入第 $0$ 个桶中。若第 $0$ 个桶已被占用(说明已有 $2^0$ 个点),则将这两棵规模相同的树拍扁,即把它们的中序遍历取出来,暴力重构为一棵规模为 $2^1$ 的平衡 K-D Tree,并尝试放入第 $1$ 个桶。若依然冲突,则继续合并进位,直至遇到一个空桶。查询时,只需顺序遍历所有非空桶,对每个桶内的静态 K-D Tree 分别进行矩形查询,并将结果累加。这样便巧妙地利用二进制拆分,将动态插入问题转化为了若干个静态树的维护问题。参考代码:```cppinline 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);pool.insert(pool.begin() , 0);cur1 = build(1 , pool.size() - 1 , 0);cur2 += szs[pos];roots[pos] = 0;szs[pos] = 0;pos += 1;}roots[pos] = cur1;szs[pos] = cur2;}```## 复杂度分析| 复杂度维度 | 静态 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)[P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312) 的弱化版,要求维护一个静态 K-D Tree(20M 的空间卡掉了所有树套树,强制在线卡掉了 CDQ 分治,只能使用 k-D Tree)支持区间修改和区间查询,建议读者通过本题后再挑战 P14312。## [P14312 【模板】K-D Tree](https://www.luogu.com.cn/problem/P14312)K-D Tree 模板,要求动态插入、区间修改、区间查询,使用二进制分组维护即可。[提交记录](https://www.luogu.com.cn/record/283656690)。```cpp#include <bits/stdc++.h>#define N 200010using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗int K;struct Node{LL x[3] , val , sum , lazy;LL mn[3] , mx[3];39 行未改动}}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);9 行未改动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];3 行未改动t[tot].lazy = 0;t[tot].lc = t[tot].rc = 0;return tot;}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;15 行未改动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);4 行未改动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);pool.insert(pool.begin() , 0);cur1 = build(1 , pool.size() - 1 , 0);cur2 += szs[pos];roots[pos] = 0;szs[pos] = 0;pos += 1;}roots[pos] = cur1;szs[pos] = cur2;}int m;LL lst = 0 , L[3] , R[3] , v , A[10];int main(){scanf("%d%d" , &K , &m);6 行未改动A[d] ^= lst;}scanf("%lld" , &v);v ^= lst;insert(A , v);}else if(op == 2){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);4 行未改动R[d] ^= lst;}scanf("%lld" , &v);v ^= lst;for(int i = 0 ; i < 25 ; ++i)if(roots[i])add(roots[i] , L , R , v , 0);}else{for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);2 行未改动for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= 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;}}return 0;}```最后的最后,再次感谢 @[fish_love_cat](https://www.luogu.com.cn/user/754021)。12345678910111245 行未改动58596061626364656677 行未改动14414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517610 行未改动18718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043139 行未改动4714724734744754764774784794809 行未改动4904914924934944954964974 行未改动50250350450550650750850951051151215 行未改动5285295305315325335345355365375385395405415425435445455465475485495505515524 行未改动5575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885896 行未改动5965975985996006016026036044 行未改动6096106116126136146156166176186192 行未改动62262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165239 行未改动6926936946956966976986997009 行未改动7107117127137147157167177187197207217227237247257267273 行未改动73173273373473573673773873974074174274374474574674774874975075175275375475575675715 行未改动7737747757767777787797807817827837847857867877887897907917927937947954 行未改动8008018028038048058068078088098108118128136 行未改动8208218228238248258268278288294 行未改动8348358368378388398408418422 行未改动845846847848849850851852853854855856857858859860861862863864865[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$。::::## 查询77 行未改动```## 插入元素前面的区间查询、修改都和线段树类似,但是插入可就不一样了,这可是重头戏。首先我们暴力插入节点肯定是错误的,因为这样会破坏 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);}```### 二进制分组二进制分组是一种将动态插入问题转化为若干个静态数据结构维护的技巧。二进制分组本质上模拟的是二进制计数器的进位过程。我们把已插入的点集拆分成若干个互不相交的静态 K-D Tree,所有树的大小都是 $2$ 的幂,第 $i$ 棵树的规模非 $0$ 即 $2^i$。插入一个新点时,先将其单独视为一棵规模为 $1$(即 $2^0$)的树,尝试放入第 $0$ 个桶中。若第 $0$ 个桶已被占用(说明已有 $2^0$ 个点),则将这两棵规模相同的树拍扁,即把它们的中序遍历取出来,暴力重构为一棵规模为 $2^1$ 的平衡 K-D Tree,并尝试放入第 $1$ 个桶。若依然冲突,则继续合并进位,直至遇到一个空桶。查询时,只需顺序遍历所有非空桶,对每个桶内的静态 K-D Tree 分别进行矩形查询,并将结果累加。这样便巧妙地利用二进制拆分,将动态插入问题转化为了若干个静态树的维护问题。参考代码:```cppinline 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);pool.insert(pool.begin() , 0);cur1 = build(1 , pool.size() - 1 , 0);cur2 += szs[pos];roots[pos] = 0;szs[pos] = 0;pos += 1;}roots[pos] = cur1;szs[pos] = cur2;}```## 复杂度分析| 复杂度维度 | 静态 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)[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;}elsebreak;}return 0;}```~~其实我是先过了 P14312 才过的这道题,本份代码由 P14312 修改而来~~。::::## [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>#define N 200010using namespace std;typedef long long LL;//十年OI一场空,不开long long见祖宗int K;struct Node{LL x[3] , val , sum , lazy;LL mn[3] , mx[3];39 行未改动}}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);9 行未改动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];4 行未改动t[tot].lazy = 0;t[tot].lc = t[tot].rc = 0;return tot;}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;15 行未改动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);4 行未改动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);pool.insert(pool.begin() , 0);cur1 = build(1 , pool.size() - 1 , 0);cur2 += szs[pos];roots[pos] = 0;szs[pos] = 0;pos += 1;}roots[pos] = cur1;szs[pos] = cur2;}int m;LL lst = 0 , L[3] , R[3] , v , A[10];int main(){scanf("%d%d" , &K , &m);6 行未改动A[d] ^= lst;}scanf("%lld" , &v);v ^= lst;insert(A , v);}else if(op == 2){for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);4 行未改动R[d] ^= lst;}scanf("%lld" , &v);v ^= lst;for(int i = 0 ; i < 25 ; ++i)if(roots[i])add(roots[i] , L , R , v , 0);}else{for(int d = 0 ; d < K ; ++d){scanf("%lld" , &L[d]);2 行未改动for(int d = 0 ; d < K ; ++d){scanf("%lld" , &R[d]);R[d] ^= 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;}}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];39 行未改动}}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);9 行未改动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];3 行未改动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;15 行未改动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);4 行未改动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);6 行未改动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]);4 行未改动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]);2 行未改动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) 的复杂度证明。