版本比较
线段树——最好用的数据结构的学习笔记
正文
增加 683 行删除 17 行
121234567891011110 行未改动全部展开12212312412512112612712812913051 行未改动18218318418518118618718818919061 行未改动252253254255251256257258259260101 行未改动全部展开36236336436536136636736836937070 行未改动44144244344444044544644744844972 行未改动52252352452552152652752852953016 行未改动54754854955054655155255355455528 行未改动584585586587583588589590591592110 行未改动全部展开703704705706702707708709710711102 行未改动全部展开814815816817813818819820821822103 行未改动全部展开92692792892992593093193293393494 行未改动10291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150103211511152115311541155103 行未改动全部展开125912601261126211441145126312641265126612671268378 行未改动全部展开1647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219715322198## 更新日志write on 2025/11/25: 动态开点线段树与主席树的学习笔记。\upd on 2025/11/27: 增加基础线段树、线段树合并\upd on 2026/01/15: 增加线段树分治并修了一些已知的锅。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)。
# 线段树## 实现原理给你一个数组,让你区间加(修改),区间求和,要求 $\le\mathcal O((n+q)\log n)$ 实现,可以怎么做?
110 行未改动全部展开$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[[P3372 【模板】线段树 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$ 表示区间粮食最大值对应最小编号。378 行未改动全部展开 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 写的很清楚。
---写作不易、在期末前更新也不易,本篇文章共 $1532$ 行、$30109$ 个字符,如果这篇文章对你有帮助的话,麻烦点个小小的赞,谢谢!写作不易,本篇文章共 $2198$ 行、$42382$ 个字符,如果这篇文章对你有帮助的话,麻烦点个小小的赞,谢谢!