正文
增加 28 行删除 28 行
78 行未改动7980818283848586879 行未改动9798991001011021031041057 行未改动1131141151161171181191201215 行未改动12712812913013113213313413513613713813914014114216 行未改动15916016116216316416516616716816917017117217317412 行未改动18718818919019119219319419559 行未改动25525625725825926026126226335 行未改动29930030130230330430530630730830931031131231331431531631731831912 行未改动33233333433533633733833934072 行未改动41341441541641741841942042142242349 行未改动47347447547647747847948048135 行未改动51751851952052152252352452552652752852953053153253353453578 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define lc p<<1#define rc p<<1|1inline int read(){int s=0,f=1; char ch=getchar();while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}9 行未改动int l,r,x,y;}t[M];struct node{int x,y,h;}st[N];//用栈实现并查集撤销vector<pair<int,int> > v[M<<2];void change(int p,int l,int r,int x,int y,int k1,int k2){if(x>y||l>r) return;if(x<=l&&r<=y){7 行未改动int ask(int x){if(fa[x]==x) return x;return ask(fa[x]);}void merge(int x,int y){//按秩合并int rx=ask(x),ry=ask(y);if(rx==ry) return;if(h[rx]<h[ry]) swap(rx,ry);st[++top]={rx,ry,h[rx]};5 行未改动int now=top;for(int i=0;i<v[p].size();i++) merge(v[p][i].first,v[p][i].second);if(l==r){if(ask(Q[pos[l]].x)==ask(Q[pos[l]].y)) puts("Yes");else puts("No");}else{int mid=l+r>>1;dfs(lc,l,mid);dfs(rc,mid+1,r);}while(now<top){//并查集合并的撤销auto tmp=st[top--];fa[tmp.y]=tmp.y;h[tmp.x]=tmp.h;}16 行未改动for(int i=1;i<=tot;i++){if(t[i].r==-1) t[i].r=tim;change(1,1,tim,t[i].l,t[i].r,t[i].x,t[i].y);}dfs(1,1,tim);return 0;}```::::**易错点** :1.并查集不能路径压缩,按秩合并不要写反了。2.注意特判区间$l>r$的情况,不然线段树为RE。## 套路总结可以看出,线段树分治的题目是比较套路化的。只要问题类型符合上面总结的那个模型,就可以直接套模板。12 行未改动### [CF1814F Communication Towers](https://www.luogu.com.cn/problem/CF1814F)题意:给定 $n$ 个点,第 $i$ 个点有权值区间 $[l_i, r_i]$,以及 $m$ 条无向边。定义点 $b$ 从点 $a$ **可达**,当且仅当存在频率 $x$ 和路径 $a = v_1, v_2, \dots, v_k = b$,使得对路径上每点 $v_j$ 均有 $x \in [l_{v_j}, r_{v_j}]$。 (注意该可达性不具传递性。)求从点 $1$ 出发可达的所有点,按编号升序输出。59 行未改动if(rx==ry) return;if(h[rx]<h[ry]) swap(rx,ry);st[++top]={rx,ry,h[rx]};if(h[rx]==h[ry]) h[rx]++;fa[ry]=rx;tag[ry]-=tag[rx];}void dfs(int p,int l,int r){int now=top;35 行未改动::::类似题目:[P5227 [AHOI2013] 连通图](https://www.luogu.com.cn/problem/P5227)[P4219 [BJOI2014] 大融合](https://www.luogu.com.cn/problem/P4219)[P4246 [SHOI2008] 堵塞的交通](https://www.luogu.com.cn/problem/P4246)### [CF601E A Museum Robbery](https://www.luogu.com.cn/problem/CF601E)题意:最初给定 $n$ 个物品以及背包容量 $k$,有 $q$ 次操作,操作有三种:1 $v$ $w$ 在背包里添加一个体积为 $v$ 价值为 $w$ 的物品2 $x$ 删除编号为 $x$ 的物品3 查询背包总和,以$\sum_{m=1}^k s(m) \times p^{m-1}\ mod\ q$的形式输出,$s(m)$表示背包容量为$m$时得到的价值最大值。$n\le5000,k\le1000,q\le30000$ ,保证操作 1 的个数不超过 10000,且至少有一个操作 3。思路:12 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define lcp p<<1#define rcp p<<1|1#define int long longconst int P=1e7+19,Q=1e9+7;const int N=5e3+5,M=3e4+5;inline int read(){72 行未改动### [P5631 最小mex生成树](https://www.luogu.com.cn/problem/P5631)题意:给定 $n$ 个点 $m$ 条边的无向连通图,边有边权。设一个自然数集合 $S$ 的 $\text{mex}$ 为:最小的、没有出现在 $S$ 中的自然数。现在你要求出一个这个图的生成树,使得其边权集合的 $\text{mex}$ 尽可能小。**试看看**: 这道题怎么与线段树分治扯上关系?49 行未改动if(rx==ry) return;if(sz[rx]<sz[ry]) swap(rx,ry);st[++top]={rx,ry,sz[rx]};fa[ry]=rx;sz[rx]+=sz[ry];}int ans=2e9;void dfs(int p,int l,int r){int now=top;35 行未改动类似题目: [P4234 最小差值生成树](https://www.luogu.com.cn/problem/P4234)### 推荐练习(按照我个人认为的难度递增)[P5787 【模板】线段树分治 / 二分图](https://www.luogu.com.cn/problem/P5787)[CF1681F Unique Occurrences](https://www.luogu.com.cn/problem/CF1681F)[P4121 [WC2005] 双面棋盘](https://www.luogu.com.cn/problem/P4121)[P14532 [RMI 2018] 颜色 / Colors](https://www.luogu.com.cn/problem/P14532)[P4585 [FJOI2015] 火星商店问题](https://www.luogu.com.cn/problem/P4585)## 总结**线段树分治的适用范围**:首先需要符合“询问时刻,操作生效时间”的问题模型。**离线**:所有操作和询问的时刻必须预先已知,能够提前算出每个操作的生效区间**数据结构可撤销或可独立**:用来维护答案的数据结构需要支持撤销(如可回退并查集),或者可以每层 DFS 维护独立状态(如拷贝背包数组)。78 行未改动7980818283848586879 行未改动9798991001011021031041057 行未改动1131141151161171181191201215 行未改动12712812913013113213313413513613713813914014114216 行未改动15916016116216316416516616716816917017117217317412 行未改动18718818919019119219319419559 行未改动25525625725825926026126226335 行未改动29930030130230330430530630730830931031131231331431531631731831912 行未改动33233333433533633733833934072 行未改动41341441541641741841942042142242349 行未改动47347447547647747847948048135 行未改动51751851952052152252352452552652752852953053153253353453578 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define lc p<<1#define rc p<<1|1inline int read(){int s=0,f=1; char ch=getchar();while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}9 行未改动int l,r,x,y;}t[M];struct node{int x,y,h;}st[N];//用栈实现并查集撤销vector<pair<int,int> > v[M<<2];void change(int p,int l,int r,int x,int y,int k1,int k2){if(x>y||l>r) return;if(x<=l&&r<=y){7 行未改动int ask(int x){if(fa[x]==x) return x;return ask(fa[x]);}void merge(int x,int y){//按秩合并int rx=ask(x),ry=ask(y);if(rx==ry) return;if(h[rx]<h[ry]) swap(rx,ry);st[++top]={rx,ry,h[rx]};5 行未改动int now=top;for(int i=0;i<v[p].size();i++) merge(v[p][i].first,v[p][i].second);if(l==r){if(ask(Q[pos[l]].x)==ask(Q[pos[l]].y)) puts("Yes");else puts("No");}else{int mid=l+r>>1;dfs(lc,l,mid);dfs(rc,mid+1,r);}while(now<top){//并查集合并的撤销auto tmp=st[top--];fa[tmp.y]=tmp.y;h[tmp.x]=tmp.h;}16 行未改动for(int i=1;i<=tot;i++){if(t[i].r==-1) t[i].r=tim;change(1,1,tim,t[i].l,t[i].r,t[i].x,t[i].y);}dfs(1,1,tim);return 0;}```::::**易错点** :1.并查集不能路径压缩,按秩合并不要写反了。2.注意特判区间$l>r$的情况,不然线段树为RE。## 套路总结可以看出,线段树分治的题目是比较套路化的。只要问题类型符合上面总结的那个模型,就可以直接套模板。12 行未改动### [CF1814F Communication Towers](https://www.luogu.com.cn/problem/CF1814F)题意:给定 $n$ 个点,第 $i$ 个点有权值区间 $[l_i, r_i]$,以及 $m$ 条无向边。定义点 $b$ 从点 $a$ **可达**,当且仅当存在频率 $x$ 和路径 $a = v_1, v_2, \dots, v_k = b$,使得对路径上每点 $v_j$ 均有 $x \in [l_{v_j}, r_{v_j}]$。 (注意该可达性不具传递性。)求从点 $1$ 出发可达的所有点,按编号升序输出。59 行未改动if(rx==ry) return;if(h[rx]<h[ry]) swap(rx,ry);st[++top]={rx,ry,h[rx]};if(h[rx]==h[ry]) h[rx]++;fa[ry]=rx;tag[ry]-=tag[rx];}void dfs(int p,int l,int r){int now=top;35 行未改动::::类似题目:[P5227 [AHOI2013] 连通图](https://www.luogu.com.cn/problem/P5227)[P4219 [BJOI2014] 大融合](https://www.luogu.com.cn/problem/P4219)[P4246 [SHOI2008] 堵塞的交通](https://www.luogu.com.cn/problem/P4246)### [CF601E A Museum Robbery](https://www.luogu.com.cn/problem/CF601E)题意:最初给定 $n$ 个物品以及背包容量 $k$,有 $q$ 次操作,操作有三种:1 $v$ $w$ 在背包里添加一个体积为 $v$ 价值为 $w$ 的物品2 $x$ 删除编号为 $x$ 的物品3 查询背包总和,以$\sum_{m=1}^k s(m) \times p^{m-1}\ mod\ q$的形式输出,$s(m)$表示背包容量为$m$时得到的价值最大值。$n\le5000,k\le1000,q\le30000$ ,保证操作 1 的个数不超过 10000,且至少有一个操作 3。思路:12 行未改动```cpp#include<bits/stdc++.h>using namespace std;#define lcp p<<1#define rcp p<<1|1#define int long longconst int P=1e7+19,Q=1e9+7;const int N=5e3+5,M=3e4+5;inline int read(){72 行未改动### [P5631 最小mex生成树](https://www.luogu.com.cn/problem/P5631)题意:给定 $n$ 个点 $m$ 条边的无向连通图,边有边权。设一个自然数集合 $S$ 的 $\text{mex}$ 为:最小的、没有出现在 $S$ 中的自然数。现在你要求出一个这个图的生成树,使得其边权集合的 $\text{mex}$ 尽可能小。**试看看**: 这道题怎么与线段树分治扯上关系?49 行未改动if(rx==ry) return;if(sz[rx]<sz[ry]) swap(rx,ry);st[++top]={rx,ry,sz[rx]};fa[ry]=rx;sz[rx]+=sz[ry];}int ans=2e9;void dfs(int p,int l,int r){int now=top;35 行未改动类似题目: [P4234 最小差值生成树](https://www.luogu.com.cn/problem/P4234)### 推荐练习(按照我个人认为的难度递增)[P5787 【模板】线段树分治 / 二分图](https://www.luogu.com.cn/problem/P5787)[CF1681F Unique Occurrences](https://www.luogu.com.cn/problem/CF1681F)[P4121 [WC2005] 双面棋盘](https://www.luogu.com.cn/problem/P4121)[P14532 [RMI 2018] 颜色 / Colors](https://www.luogu.com.cn/problem/P14532)[P4585 [FJOI2015] 火星商店问题](https://www.luogu.com.cn/problem/P4585)## 总结**线段树分治的适用范围**:首先需要符合“询问时刻,操作生效时间”的问题模型。**离线**:所有操作和询问的时刻必须预先已知,能够提前算出每个操作的生效区间**数据结构可撤销或可独立**:用来维护答案的数据结构需要支持撤销(如可回退并查集),或者可以每层 DFS 维护独立状态(如拷贝背包数组)。