版本比较
线段树——最好用的数据结构的学习笔记
正文
增加 944 行删除 18 行
12134567389101132 行未改动4445464744484950515269 行未改动12212312412512212612712812913051 行未改动18218318418518218618718818919061 行未改动252253254255252256257258259260101 行未改动全部展开36236336436536236636736836937070 行未改动44144244344444144544644744844972 行未改动52252352452552252652752852953016 行未改动54754854955054755155255355455528 行未改动584585586587584588589590591592110 行未改动全部展开703704705706703707708709710711102 行未改动全部展开814815816817814818819820821822103 行未改动全部展开92692792892992693093193293393494 行未改动10291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150103311511152115311541155103 行未改动全部展开125912601261126211451146126312641265126612671268117 行未改动全部展开1386138713881389127213901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198## 更新日志write on 2025/11/25: 动态开点线段树与主席树的学习笔记。\upd on 2025/11/27: 增加基础线段树、线段树合并upd on 2025/11/27: 增加基础线段树、线段树合并。\upd on 2026/01/15: 增加线段树分治并修了一些已知的锅。\upd on 2026/04/16: 我又回来了,这次看到一个[好题](https://atcoder.jp/contests/abc453/tasks/abc453_g),必须加上!另外补充动态 DP。\upd on 2026/04/24: 我又回来了,这次更线段树分裂!要不要我更可持久化并查集?[要](https://vote.saobby.com/v/DA7aOe5A/0);[不要](https://vote.saobby.com/v/DA7aOe5A/1)。
ps: 线段树分裂我还没学,学完加上!# 线段树## 实现原理给你一个数组,让你区间加(修改),区间求和,要求 $\le\mathcal O((n+q)\log n)$ 实现,可以怎么做?
32 行未改动 pushup(l,r,p);}```
$O(n\log n)$。$O(n)$。
接下来是查询:
其实查询的过程就是将小区间合并为大区间。69 行未改动$O(\log n)$。
没了。
:::success[[P3374 【模板】树状数组 1 - 洛谷](https://www.luogu.com.cn/problem/P3374) 实现:]:::success[[P3374 【模板】树状数组 1](https://www.luogu.com.cn/problem/P3374) 实现:]```cpp#include<bits/stdc++.h>using namespace std;#define ls (p<<1)51 行未改动lazy tag,即懒标记,指在修改时找到一个包含的线段即可将其修改、打上懒标记,表示当前修改了 $lazy_{p}$ 个偏移量未下传,即分下去的线段还没有偏移,不用再下传造成 $O(len\log n)$ 的时间,而到了查询或修改时区间不完全被包含再下传懒标记,就是左右儿子的 `segtree` 数组。
然后没了,记得在 `query` 时也要 `pushdown`!
:::success[[P3374 【模板】线段树 1 - 洛谷](https://www.luogu.com.cn/problem/P3372) 实现:]:::success[[P3372 【模板】线段树 1](https://www.luogu.com.cn/problem/P3372) 实现:]```cpp#include<bits/stdc++.h>using namespace std;#define ls (p<<1)61 行未改动```:::## 看例题
### [P3373 【模板】线段树 2 - 洛谷](https://www.luogu.com.cn/problem/P3373)### [P3373 【模板】线段树 2](https://www.luogu.com.cn/problem/P3373)
毒瘤,但只是 `pushdown` 多亿点点而已。
考虑乘法懒标记下传。101 行未改动全部展开}```:::
### [P1531 I Hate It - 洛谷](https://www.luogu.com.cn/problem/P1531)### [P1531 I Hate It](https://www.luogu.com.cn/problem/P1531)
维护最大值区间。这题不用 lazy tag,但是我还是打了。
:::success[AC code]70 行未改动有些区间无需查询,那么就动态开点,需要查一个就开一个。
每次加点都是 $O(\log n)$ 级别的,总共 $O(q\log n)$,没必要开 $O(n\log n)$ 个点,只需要在 `add()` 时传递下去即可。`query()` 根本不用管没开的节点。空的就返回 $0$。
## [P13825 【模板】线段树 1.5 - 洛谷](https://www.luogu.com.cn/problem/P13825)## [P13825 【模板】线段树 1.5](https://www.luogu.com.cn/problem/P13825)
这题……$n\le 10^9$,只能上动态开点。
`add()` 如果要懒标记下传,那么新开左右儿子存下懒标记;72 行未改动```
:::
## [P3184 [USACO16DEC] Counting Haybales S - 洛谷](https://www.luogu.com.cn/problem/P3184)## [P3184 [USACO16DEC] Counting Haybales S](https://www.luogu.com.cn/problem/P3184)
杀鸡用宰牛刀。
(甚至不需要 `add()`)16 行未改动 return 0;}```
## [P13825 【模板】线段树 1.5 - 洛谷](https://www.luogu.com.cn/problem/P13825)## [P13825 【模板】线段树 1.5](https://www.luogu.com.cn/problem/P13825)
板题。
:::warning[Trick]{open}28 行未改动# 主席树
接下来是主席树(其实是可持久化线段树)。
## 先上板题[P3919 【模板】可持久化线段树 1(可持久化数组) - 洛谷](https://www.luogu.com.cn/problem/P3919)。## 先上板题[P3919 【模板】可持久化线段树 1(可持久化数组)](https://www.luogu.com.cn/problem/P3919)。
### 思路
考虑如何更新。更新是一大重点。110 行未改动全部展开```
:::
## [P3834 【模板】可持久化线段树 2 - 洛谷](https://www.luogu.com.cn/problem/P3834)## [P3834 【模板】可持久化线段树 2](https://www.luogu.com.cn/problem/P3834)
主席树,全称可持久化权值线段树。
权值,指某个值出现的次数。主席树维护区间内每个数出现的次数。102 行未改动全部展开```
:::
### [P3755 [CQOI2017] 老C的任务 - 洛谷](https://www.luogu.com.cn/problem/P3755)### [P3755 [CQOI2017] 老C的任务](https://www.luogu.com.cn/problem/P3755)
先离散化。
主席树一个一个加,就当 $x_i$ 是 $i$,`sgtree.add(i-1,y,p)`。103 行未改动全部展开}```:::
### [P1383 高级打字机 - 洛谷](https://www.luogu.com.cn/problem/P1383)### [P1383 高级打字机](https://www.luogu.com.cn/problem/P1383)
简单维护一下每个文本的长度就好。
因为 `Q` 不算修改,所以将板子的 `qaq` 行删除。94 行未改动 return 0;}```:::
### [Copy Query](https://atcoder.jp/contests/abc453/tasks/abc453_g)
哎这不进入了我的舒适区吗?一眼主席树:$n$ 个数组,初始相同(只开一个线段树)、修改某数组的单个位置(主席树修改)、copy 数组(copy 根节点索引)。
es 结束。
:::success[AC code]```cpp#include<bits/stdc++.h>using namespace std;const int maxn=1e6+5;int m,n,q;typedef long long ll;struct pnode{ ll sum{},ls{},rs{};};class psegtree{ vector<pnode>tree; vector<int>rt; int newrt(int t){ rt.push_back({t}); return rt.size()-1; } int newnode(){ tree.push_back({}); return tree.size()-1; } void pushup(int p){ tree[p].sum=0; if(tree[p].ls)tree[p].sum+=tree[tree[p].ls].sum; if(tree[p].rs)tree[p].sum+=tree[tree[p].rs].sum; } void build(int l,int r,int p){ if(l==r){ tree[p].sum=0; return; } int mid=l+r>>1; build(l,mid,tree[p].ls=newnode()); build(mid+1,r,tree[p].rs=newnode()); pushup(p); } void add(int lst,int l,int r,int p,int x,int c){ if(l==r){ tree[p].sum=c; return; } int mid=l+r>>1; if(x<=mid){ tree[p].ls=newnode(); tree[p].rs=tree[lst].rs; add(tree[lst].ls,l,mid,tree[p].ls,x,c); }else{ tree[p].rs=newnode(); tree[p].ls=tree[lst].ls; add(tree[lst].rs,mid+1,r,tree[p].rs,x,c); } pushup(p); } ll query(int l,int r,int p,int s,int t){ if(!p||l>t||r<s)return 0; if(s<=l&&r<=t)return tree[p].sum; int mid=l+r>>1; ll ans=0; ans+=query(l,mid,tree[p].ls,s,t); ans+=query(mid+1,r,tree[p].rs,s,t); return ans; }public: psegtree(){ newnode(); newrt(newnode()); build(1,m,1); } void add(int k,int x,int c){ int p=newrt(newnode()); add(rt[k],1,m,rt[p],x,c); } ll query(int k,int x,int c){ return query(1,m,rt[k],x,c); }};map<int,int>mp;int now=0;int main(){ ios::sync_with_stdio(false); cin.tie(nullptr),cout.tie(nullptr); cin>>n>>m>>q; psegtree sgt; while(q--){ int op; cin>>op; if(op==1){ int x,y; cin>>x>>y; mp[x]=mp[y]; }else if(op==2){ int x,y,z; cin>>x>>y>>z; sgt.add(mp[x],y,z); mp[x]=++now; }else{ int x,y,z; cin>>x>>y>>z; cout<<sgt.query(mp[x],y,z)<<"\n"; } } return 0;}```:::[AC submission](https://atcoder.jp/contests/abc453/submissions/74880775).
# 线段树合并
一个很有意思的算法。
## 先看一道经典例题[P3201 [HNOI2009] 梦幻布丁 - 洛谷](https://www.luogu.com.cn/problem/P3201)## 先看一道经典例题[P3201 [HNOI2009] 梦幻布丁](https://www.luogu.com.cn/problem/P3201)
考虑暴力地线段树。
开 $10^6$ 棵线段树,存下每个颜色所在的所有位置(这你要不动态开点就会有大大的 $\Large\colorbox{#052242}{\color{white}MLE}$),然后修改直接暴力一个一个删、一个一个加过去。103 行未改动全部展开 return 0;}```:::## [P4556 【模板】线段树合并 / [Vani有约会] 雨天的尾巴 - 洛谷](https://www.luogu.com.cn/problem/P4556)前置知识:[P3128 [USACO15DEC] Max Flow P - 洛谷](https://www.luogu.com.cn/problem/P3128)。## [P4556 【模板】线段树合并 / [Vani有约会] 雨天的尾巴](https://www.luogu.com.cn/problem/P4556)前置知识:[P3128 [USACO15DEC] Max Flow P](https://www.luogu.com.cn/problem/P3128)。
这个差分好像啊。
设 $sum$ 表示区间(粮食编号区间)粮食最大值,$mx$ 表示区间粮食最大值对应最小编号。117 行未改动全部展开 return 0;}```:::$1272$ 行,$24934$ 字符,制作不易,如果本篇文章对你有帮助的话,帮忙点个小小的赞,谢谢!# 线段树分治~~lg 的题目有点难,看 loj 的题。~~
[直接看题](https://loj.ac/p/121)。
我们想到离线维护边存在时间的区间。
对时间开一棵线段树,每个节点存当前节点存在的边的编号。~~当然没有 `query`,所以不用 lazy tag。~~
那么怎么处理答案呢?
直接像遍历线段树一样遍历下去,对于一个节点,先加上这个节点保存的所有边,再往下遍历,最后删除当前节点储存的边。如果当前节点储存的 $l$ 等于 $r$ 并且在此处有查询,那么就先加边,再查询。
问题来了:怎么加边、查询、删边?
查询连通性,肯定用并查集啊。
加边也好,怎么删边?
其实就是维护上一次加的边的栈,将连接的两个 `fa` 压栈,出战直接断开联系就好。注意不能路径压缩,只能按秩合并(或启发式合并)。
:::success[AC code && [AC submission](https://loj.ac/s/2497433)]```cpp#include<bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGE#define debug(x)#else#define debug(x) cout<<"In function "<<__FUNCTION__<<" "<<#x<<"="<<x<<"\n";#endifint n,m;class Segtree{ int n;public: vector<vector<array<int,2>>>tree; Segtree(int _n):n(_n+3){ tree.assign(n<<2,decltype(tree)::value_type()); } void add(int l,int r,int p,int L,int R,array<int,2>tim){ if(L>r||R<l)return; if(L<=l&&r<=R){ tree[p].emplace_back(tim); return; } int mid=l+r>>1; add(l,mid,p<<1,L,R,tim); add(mid+1,r,p<<1|1,L,R,tim); }};class DSU{ vector<int>f; vector<int>d; stack<tuple<int,int,int>>stk;public: DSU(int _n){ f.resize(_n+3); d.resize(_n+3); iota(f.begin(),f.end(),0); fill(d.begin(),d.end(),1); } int find(int x){ return f[x]==x?x:find(f[x]); } bool unite(int x,int y){ x=find(x),y=find(y); if(x==y)return false; if(d[x]>d[y])swap(x,y); f[x]=y; stk.push({x,y,d[y]}); d[y]=max(d[y],d[x]+1); return true; } void erase(){ auto[x,y,D]=stk.top(); stk.pop(); f[x]=x; d[y]=D; }};map<array<int,2>,int>mp;vector<array<int,2>>query;void solve(Segtree&sgt,DSU&dsu,vector<char>&now,int l,int r,int p){ if(l==r){ if(query[l][0]){ int cnt=0; for(auto[x,y]:sgt.tree[p])cnt+=dsu.unite(x,y); debug(query[l][0]) debug(query[l][1]) debug(dsu.find(query[l][0])) debug(dsu.find(query[l][1])) now.emplace_back(dsu.find(query[l][0])==dsu.find(query[l][1])?'Y':'N'); while(cnt--)dsu.erase(); } return; } int cnt=0; for(auto[x,y]:sgt.tree[p])cnt+=dsu.unite(x,y); int mid=l+r>>1; solve(sgt,dsu,now,l,mid,p<<1); solve(sgt,dsu,now,mid+1,r,p<<1|1); while(cnt--)dsu.erase();}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n>>m; DSU dsu(n); Segtree sgt(m); query.assign(m+1,{}); for(int i=1;i<=m;i++){ int op,l,r; cin>>op>>l>>r; if(l>r)swap(l,r); if(op==0) mp[{l,r}]=i; if(op==1){ sgt.add(1,m,1,mp[{l,r}],i,{l,r}); mp.erase({l,r}); } if(op==2){ query[i]={l,r}; } } for(auto[x,y]:mp)sgt.add(1,m,1,y,m,{x[0],x[1]}); vector<char>ans; solve(sgt,dsu,ans,1,m,1); for(auto i:ans)cout<<i<<"\n"; return 0;}```:::
## [P5787 【模板】线段树分治 / 二分图](https://www.luogu.com.cn/problem/P5787)
这原来是二分图模板。。。
前置知识:并查集解决二分图问题。
大家都知道扩展域并查集可以判断二分图,那就再传一个 `flag` 表示当前判断是不是已经不是二分图,如果当前已经不是二分图了,再无论如何也拯救不回来,所以要用 `|=` 判。
依旧并查集加线段树。
:::success[AC code]```cpp#include<bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGE#define debug(x)void(114514)#else#define debug(x)cout<<"In function "<<__FUNCTION__<<" "<<#x<<"="<<(x)<<"\n"#endifint n,m,k;class Segtree{ int n;public: vector<vector<array<int,2>>>tree; Segtree(int _n):n(_n+3){ tree.assign(n<<2,decltype(tree)::value_type()); } void add(int l,int r,int p,int L,int R,array<int,2>tim){ if(L>r||R<l)return; if(L<=l&&r<=R){ tree[p].emplace_back(tim); return; } int mid=l+r>>1; add(l,mid,p<<1,L,R,tim); add(mid+1,r,p<<1|1,L,R,tim); }};class DSU{ vector<int>f; vector<int>d; stack<tuple<int,int,int>>stk;public: DSU(int _n){ f.resize(_n+3); d.resize(_n+3); iota(f.begin(),f.end(),0); fill(d.begin(),d.end(),1); } int find(int x){ return f[x]==x?x:find(f[x]); } bool unite(int x,int y){ x=find(x),y=find(y); if(x==y)return false; if(d[x]>d[y])swap(x,y); f[x]=y; stk.push({x,y,d[y]}); d[y]=max(d[y],d[x]+1); return true; } void erase(){ auto[x,y,D]=stk.top(); stk.pop(); f[x]=x; d[y]=D; }};map<array<int,2>,int>mp;void solve(Segtree&sgt,DSU&dsu,vector<string>&now,int l,int r,int p,bool k=false){ if(l==r){ int cnt=0; debug(l); debug(r); for(auto[x,y]:sgt.tree[p]){ cnt+=dsu.unite(x,y+n); cnt+=dsu.unite(x+n,y); k|=(dsu.find(x)==dsu.find(x+n)); k|=(dsu.find(y)==dsu.find(y+n)); debug(x); debug(dsu.find(x)==dsu.find(x+n)); debug(y); debug(dsu.find(y)==dsu.find(y+n)); } now.push_back(k?"No":"Yes"); while(cnt--)dsu.erase(); return; } int cnt=0; debug(l); debug(r); for(auto[x,y]:sgt.tree[p]){ cnt+=dsu.unite(x,y+n); cnt+=dsu.unite(x+n,y); k|=(dsu.find(x)==dsu.find(x+n)); k|=(dsu.find(y)==dsu.find(y+n)); debug(x); debug(dsu.find(x)==dsu.find(x+n)); debug(y); debug(dsu.find(y)==dsu.find(y+n)); } debug(endl); int mid=l+r>>1; solve(sgt,dsu,now,l,mid,p<<1,k); solve(sgt,dsu,now,mid+1,r,p<<1|1,k); while(cnt--)dsu.erase();}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n>>m>>k; DSU dsu(n<<1); Segtree sgt(k); for(int i=1;i<=m;i++){ int x,y,l,r; cin>>x>>y>>l>>r; ++l; if(x>y)swap(x,y); sgt.add(1,k,1,l,r,{x,y}); } vector<string>ans; solve(sgt,dsu,ans,1,k,1); for(auto i:ans)cout<<i<<"\n"; return 0;}```:::
# 动态 DP咋地聊着聊着线段树咋就到了 DP 呢?这个 DP 不简单,是用线段树实现的。
> 前置知识:矩阵乘法。:::success[模板我放这儿]{open}```cppconst int K=/*your matrix size*/;class Mat{ array<array<ll,K>,K>arr;public: Mat(decltype(arr)t):arr(t){} array<ll,K>&operator[](size_t pos){return arr[pos];} Mat operator*(Mat mt)const{ Mat matr(array<array<ll,K>,K>{}); for(int k=0;k<K;k++){// floyd? for(int i=0;i<K;i++){ for(int j=0;j<K;j++){ matr[i][j]+=mt[i][k]*arr[k][j]; } } } return matr; }};```:::## 引入让你用矩阵求 $a+b$,并且此形式的矩阵求完 $a+b$ 之后还能求 $(a+b)+c$。
首先很简单。
$$\begin{bmatrix}a\end{bmatrix}$$
哎不是 $1$ 维不够。
那很简单,在后面加个 $t$(待定常数)
使得
$$\begin{bmatrix}a&t\end{bmatrix}\begin{bmatrix}x_0&x_1\\x_2&x_3\end{bmatrix}=\begin{bmatrix}a+b&t\end{bmatrix}\\\therefore \begin{cases}ax_0+tx_2=a+b\\ax_1+tx_3=t\end{cases}$$
随便搞搞都知道 $x_0=1,x_1=0,t=1,x_2=b,x_3=1$。
那么
$$\begin{bmatrix} a&1\end{bmatrix}\begin{bmatrix} 1&0\\ b&1\end{bmatrix}=\begin{bmatrix} a+b&1\end{bmatrix}$$
还能乘下去。
$$\begin{bmatrix} a&1\end{bmatrix}\begin{bmatrix} 1&0\\ b&1\end{bmatrix}\begin{bmatrix} 1&0\\ c&1\end{bmatrix}=\begin{bmatrix} a+b+c&1\end{bmatrix}$$
又因为矩阵有结合律,加法也有结合律,所以
$$\begin{bmatrix} a&1\end{bmatrix}( \begin{bmatrix} 1&0\\ b&1 \end{bmatrix} \begin{bmatrix} 1&0\\ c&1 \end{bmatrix})=\begin{bmatrix} a+(b+c)&1\end{bmatrix}=\begin{bmatrix} a&1\end{bmatrix}\begin{bmatrix} 1&0\\ b+c&1\end{bmatrix}$$
所以
$$\begin{bmatrix} 1&0\\ b&1\end{bmatrix}\begin{bmatrix} 1&0\\ c&1\end{bmatrix}=\begin{bmatrix} 1&0\\ b+c&1\end{bmatrix}$$好的。
接下来让我们用这个实现[静态区间求和](https://www.luogu.com.cn/problem/B3612)吧。
什么?你不会做除法?
~~虽然逆矩阵会好写很多~~,但是有些矩阵不能逆啊!线段树,启动!
其实线段树加矩阵就是我们的动态 DP。线段树自带单点修改。至于区间查,要多个 `lazy`。
## 例题(全是大炮打蚊子(吗?
## [B3612]((https://www.luogu.com.cn/problem/B3612))刚刚的 link。
好的其实就是线段树板子。
:::success[AC code]```cpp#include<bits/stdc++.h>using namespace std;typedef long long ll;// #define manyconst int K=2;class Mat{ array<array<ll,2>,2>arr;public: Mat(ll t=0){arr={1,0,t,1};}// 为了线段树好写 Mat(decltype(arr)t):arr(t){} operator ll(){return arr[1][0];}// +1 array<ll,2>&operator[](size_t pos){return arr[pos];} Mat operator*(Mat mt)const{ Mat matr(array<array<ll,2>,2>{}); for(int k=0;k<K;k++){ for(int i=0;i<K;i++){ for(int j=0;j<K;j++){ matr[i][j]+=mt[i][k]*arr[k][j]; } } } return matr; }};const int maxn=1e5+5;Mat segtree[maxn<<2];ll arr[maxn];#define ls (p<<1)#define rs (p<<1|1)void pushup(int p){ segtree[p]=segtree[ls]*segtree[rs];}void build(int l,int r,int p){ if(l==r){ segtree[p]=arr[l]; return; } int mid=l+r>>1; build(l,mid,ls); build(mid+1,r,rs); pushup(p);}Mat query(int l,int r,int p,int s,int t){ if(r<s||l>t)return Mat(0); if(s<=l&&r<=t)return segtree[p]; int mid=l+r>>1; return query(l,mid,ls,s,t)* query(mid+1,r,rs,s,t);}void solve(){ int n; cin>>n; for(int i=1;i<=n;i++)cin>>arr[i]; int q; cin>>q; build(1,n,1); while(q--){ int l,r; cin>>l>>r; cout<<query(1,n,1,l,r)<<"\n"; }}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr),cout.tie(nullptr); int oT_To=1; for(#ifdef many cin>>oT_To#endif ;oT_To--;solve()); return~oT_To;}```:::
(哎我没用过正经的前缀和做?!五次提交,分别是:分块、莫队、动态 DP、树状数组、线段树
~~咱还可以大炮打蚊子交到 P1001 题解区呢~~。
## [P3374](https://www.luogu.com.cn/problem/P3374)就多一个修改,怕个啥?上!
```cppvoid add(int l,int r,int p,int t,int x){ if(l==r){ segtree[p]=segtree[p]+x;// 隐式转换换来换去的 return; } int mid=l+r>>1; if(t<=mid)add(l,mid,ls,t,x); else add(mid+1,r,rs,t,x); pushup(p);}```
## [Shortest Path Query](https://atcoder.jp/contests/abc429/tasks/abc429_f)其实这才是动态 DP 模板题。赛时没学这玩意,没做出来。
啥?你让我维护线段树?额……那就维护第 $i$ 列和第 $j$ 列的连通性吧!
对于第 $x$ 行第 $y$ 列,维护的是以当前线段树节点 “$l$” 为列编号、$x+1$ 为行编号的位置与以当前线段树节点 “$r$” 为列编号、$y+1$ 为行编号的位置的最短距离(不越界行走),如果其中一个是墙或走不到,距离为 $+\infty$。
由于线段树左右儿子的“区间”是相邻的,所以我们可以对于第 $i$ 行、第 $j$ 列,找一个 $k$ 作为桥,$ans_{i,j}=\min\{ans_{i,j},lhs_{i,k}+rhs_{k,j}+1\}$,其中 $1$ 为走相邻两个格子所需要的时间。
所以这就是为什么我在放板子时写了个 `floyd?`,因为这就是 `floyd` 更新。
```cppMat operator*(Mat mt)const{ Mat matr; matr.arr.fill({inf,inf,inf}); for(int k=0;k<K;k++) for(int i=0;i<K;i++) for(int j=0;j<K;j++) matr[i][j]=min(matr[i][j],arr[i][k]+mt.arr[k][j]+1); return matr;}```
好的接下来看初始化。当然,从第 $i$ 行 $j$ 列到第 $k$ 行 $j$ 列的距离是:如果这两个点之间没有 `#`,则是 $\vert i-k\vert$,否则 $+\infty$。
```cppMat(array<char,K>t){ for(int i=0;i<K;i++){ for(int j=0;j<K;j++){ bool flag=true; for(int k=min(i,j);k<=max(i,j);k++) flag&=(t[k]=='.'); arr[i][j]=(!flag)*inf+abs(i-j); } }}```
剩下正常线段树。
[AC submission](https://atcoder.jp/contests/abc429/submissions/72438747).
# 线段树分裂
咕了好久。
## 直接看[题](https://www.luogu.com.cn/problem/P5494)
好的 $0$ 操作不会;$1$ 是线段树合并;$2$ 是 `add`;$3$ 是 `query`;$4$ 是线段树上二分。
看看 $0$ 操作,其实就是把线段树中某些节点挪出去。这就是线段树分裂。
怎么做呢?想想线段树合并怎么做的,分裂就是逆运算。
对的直接对应 `dfs`,边 `dfs` 边开新节点,如果这个节点管辖的区间都要分裂出去,这个节点就直接给过去。否则继续 `dfs`;如果这个节点管辖的区间根本不用分裂,那么直接 `return`。记得 `pushup`。
:::warning[注意要回收空间!]```cppint New(){ int x; if(bac.size())x=bac.top(),bac.pop(); else x=++cnt; return x;}void Del(int p){ if(!p)return; segtree[p]=ls[p]=rs[p]=0; bac.push(p);}```:::
```cppint split(int p,int l,int r,int s,int t){ if(!p)return p; if(s<=l&&r<=t)return p; int mid=l+r>>1; int t0=New(); if(s<=mid){ ls[t0]=split(ls[p],l,mid,s,t); if(ls[t0]==ls[p])ls[p]=0; } if(t>mid){ rs[t0]=split(rs[p],mid+1,r,s,t); if(rs[t0]==rs[p])rs[p]=0; } pushup(t0); pushup(p); return t0;}```实现依旧很简单,但是要注意如果直接给你就得与原树断开关系。
~~又成功水了一道紫。~~
:::success[完整代码]```cpp#define pbds// #define many#ifndef pbds#include<bits/stdc++.h>using namespace std;using namespace __gnu_cxx;#define pqueue priority_queuetemplate<typename Tp>using heap=pqueue<Tp,vector<Tp>,greater<Tp>>;#else#include<bits/extc++.h>using namespace std;using namespace __gnu_cxx;using namespace __gnu_pbds;#define unordered_map __gnu_pbds::gp_hash_table#define pqueue __gnu_pbds::priority_queuetypedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>Trie;template<typename Tp>using heap=pqueue<Tp,greater<Tp>>;#endiftypedef long long ll;typedef unsigned long long ull;typedef pair<int,int>pii;typedef pair<ll,ll>pll;typedef pair<ull,ull>pull;template<size_t _Nw>using arri=array<int,_Nw>;template<size_t _Nw>using arrl=array<ll,_Nw>;mt19937 mt(chrono::system_clock::now().time_since_epoch().count());mt19937_64 mt64(chrono::system_clock::now().time_since_epoch().count());const bool mdsw=true;const int mod=mdsw?998244353:(int)(1e9+7);template<typename T>T inf=INFINITY;template<>ll inf<ll> =0x3f3f3f3f3f3f3f3f;template<>int inf<int> =0x3f3f3f3f;template<>short inf<short> =0x3f3f;template<>char inf<char> =0x3f;template<>ull inf<ull> =0x3f3f3f3f3f3f3f3f;template<>unsigned int inf<unsigned int> =0x3f3f3f3f;template<>unsigned short inf<unsigned short> =0x3f3f;template<>unsigned char inf<unsigned char> =0x3f;const int maxn=2e5+5;int n,q,cnt=1,rootcnt=1;int a[maxn],ls[maxn<<5],rs[maxn<<5],root[maxn<<2];ll segtree[maxn<<5];stack<int>bac;int New(){ int x; if(bac.size())x=bac.top(),bac.pop(); else x=++cnt; return x;}void pushup(int p){ segtree[p]=segtree[ls[p]]+segtree[rs[p]];}void Del(int p){ if(!p)return; segtree[p]=ls[p]=rs[p]=0; bac.push(p);}void build(int p,int l,int r){ if(l==r){ segtree[p]=a[l]; return; } ls[p]=New(); rs[p]=New(); int mid=l+r>>1; build(ls[p],l,mid); build(rs[p],mid+1,r); pushup(p);}void add(int p,int l,int r,int x,int q){ if(l==r){ segtree[p]+=x; return; } int mid=l+r>>1; if(q<=mid){ if(!ls[p])ls[p]=New(); add(ls[p],l,mid,x,q); }else{ if(!rs[p])rs[p]=New(); add(rs[p],mid+1,r,x,q); } pushup(p);}int kth(int p,int l,int r,int k){ if(l==r)return l; int mid=l+r>>1; if(segtree[ls[p]]>=k)return kth(ls[p],l,mid,k); else return kth(rs[p],mid+1,r,k-segtree[ls[p]]);}int merge(int x,int y){ if(!x||!y)return x+y; ls[x]=merge(ls[x],ls[y]); rs[x]=merge(rs[x],rs[y]); segtree[x]+=segtree[y]; Del(y); return x;}int split(int p,int l,int r,int s,int t){ if(!p)return p; if(s<=l&&r<=t)return p; int mid=l+r>>1; int t0=New(); if(s<=mid){ ls[t0]=split(ls[p],l,mid,s,t); if(ls[t0]==ls[p])ls[p]=0; } if(t>mid){ rs[t0]=split(rs[p],mid+1,r,s,t); if(rs[t0]==rs[p])rs[p]=0; } pushup(t0); pushup(p); return t0;}ll sum(int p,int l,int r,int s,int t){ if(!p||l>t||r<s)return 0; if(s<=l&&r<=t){ return segtree[p]; } int mid=l+r>>1; return sum(ls[p],l,mid,s,t)+sum(rs[p],mid+1,r,s,t);}void solve(){ cin>>n>>q; for(int i=1;i<=n;i++)cin>>a[i]; build(root[1]=1,1,n); while(q--){ int op; cin>>op; if(op==0){ int p,x,y; cin>>p>>x>>y; root[++rootcnt]=split(root[p],1,n,x,y); } if(op==1){ int p,t; cin>>p>>t; merge(root[p],root[t]); } if(op==2){ int p,x,q; cin>>p>>x>>q; add(root[p],1,n,x,q); } if(op==3){ int p,x,y; cin>>p>>x>>y; cout<<sum(root[p],1,n,x,y)<<"\n"; } if(op==4){ int p,k; cin>>p>>k; if(segtree[root[p]]>=k)cout<<kth(root[p],1,n,k)<<"\n"; else cout<<"-1\n"; } }}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr),cout.tie(nullptr); int oT_To; for(#ifdef many cin>>oT_To#else oT_To=1#endif ;oT_To--;solve()); long double some_Random_in_double=uniform_real_distribution<long double>(mt64(),mt64())(mt64); if(some_Random_in_double==NAN||some_Random_in_double==INFINITY||abs(some_Random_in_double-some_Random_in_double)>114514)return 114514; else if(1+1==3)exit(0x3f3f3f3f); else if(clock()==66666)return clock(); else if(!time(nullptr))return 1919810; else return 0;}```:::
## [P2824 [HEOI2016/TJOI2016] 排序](https://www.luogu.com.cn/problem/P2824)这题……很多细节。
开许多权值线段树里维护排序区间,用 `set` 维护每个排序段第一个元素下标。用 $n+1$ 当边界。然后对于每次排序,排序端点可能会落在一个有序区间内,要拆了。
算了,如果觉得我没说清楚的看看[这篇 tj](https://www.luogu.com.cn/article/x34hrsp4),Ta 写的很清楚。
---写作不易,本篇文章共 $2198$ 行、$42382$ 个字符,如果这篇文章对你有帮助的话,麻烦点个小小的赞,谢谢!