正文
增加 683 行删除 17 行
123456110 行未改动全部展开11711811912012112212312412551 行未改动17717817918018118218318418561 行未改动247248249250251252253254255101 行未改动全部展开35735835936036136236336436570 行未改动43643743843944044144244344472 行未改动51751851952052152252352452516 行未改动54254354454554654754854955028 行未改动579580581582583584585586587110 行未改动全部展开698699700701702703704705706102 行未改动全部展开809810811812813814815816817103 行未改动全部展开92192292392492592692792892994 行未改动1024102510261027102810291030103110321033103410351036103 行未改动全部展开1140114111421143114411451146114711481149378 行未改动全部展开15281529153015311532upd on 2025/11/27: 增加基础线段树、线段树合并\upd on 2026/01/15: 增加线段树分治并修了一些已知的锅。# 线段树## 实现原理给你一个数组,让你区间加(修改),区间求和,要求 $\le\mathcal O((n+q)\log n)$ 实现,可以怎么做?110 行未改动全部展开$O(\log n)$。没了。:::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) 实现:]```cpp#include<bits/stdc++.h>using namespace std;#define ls (p<<1)61 行未改动```:::## 看例题### [P3373 【模板】线段树 2 - 洛谷](https://www.luogu.com.cn/problem/P3373)毒瘤,但只是 `pushdown` 多亿点点而已。考虑乘法懒标记下传。101 行未改动全部展开}```:::### [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)这题……$n\le 10^9$,只能上动态开点。`add()` 如果要懒标记下传,那么新开左右儿子存下懒标记;72 行未改动```:::## [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)板题。:::warning[Trick]{open}28 行未改动# 主席树接下来是主席树(其实是可持久化线段树)。## 先上板题[P3919 【模板】可持久化线段树 1(可持久化数组) - 洛谷](https://www.luogu.com.cn/problem/P3919)。### 思路考虑如何更新。更新是一大重点。110 行未改动全部展开```:::## [P3834 【模板】可持久化线段树 2 - 洛谷](https://www.luogu.com.cn/problem/P3834)主席树,全称可持久化权值线段树。权值,指某个值出现的次数。主席树维护区间内每个数出现的次数。102 行未改动全部展开```:::### [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)简单维护一下每个文本的长度就好。因为 `Q` 不算修改,所以将板子的 `qaq` 行删除。94 行未改动return 0;}```:::# 线段树合并一个很有意思的算法。## 先看一道经典例题[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)。这个差分好像啊。设 $sum$ 表示区间(粮食编号区间)粮食最大值,$mx$ 表示区间粮食最大值对应最小编号。378 行未改动全部展开return 0;}```:::写作不易、在期末前更新也不易,本篇文章共 $1532$ 行、$30109$ 个字符,如果这篇文章对你有帮助的话,麻烦点个小小的赞,谢谢!1234567891011110 行未改动全部展开12212312412512612712812913051 行未改动18218318418518618718818919061 行未改动252253254255256257258259260101 行未改动全部展开36236336436536636736836937070 行未改动44144244344444544644744844972 行未改动52252352452552652752852953016 行未改动54754854955055155255355455528 行未改动584585586587588589590591592110 行未改动全部展开703704705706707708709710711102 行未改动全部展开814815816817818819820821822103 行未改动全部展开92692792892993093193293393494 行未改动1029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155103 行未改动全部展开1259126012611262126312641265126612671268378 行未改动全部展开164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198## 更新日志write on 2025/11/25: 动态开点线段树与主席树的学习笔记。\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) 实现:]```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) 实现:]```cpp#include<bits/stdc++.h>using namespace std;#define ls (p<<1)61 行未改动```:::## 看例题### [P3373 【模板】线段树 2](https://www.luogu.com.cn/problem/P3373)毒瘤,但只是 `pushdown` 多亿点点而已。考虑乘法懒标记下传。101 行未改动全部展开}```:::### [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)这题……$n\le 10^9$,只能上动态开点。`add()` 如果要懒标记下传,那么新开左右儿子存下懒标记;72 行未改动```:::## [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)板题。:::warning[Trick]{open}28 行未改动# 主席树接下来是主席树(其实是可持久化线段树)。## 先上板题[P3919 【模板】可持久化线段树 1(可持久化数组)](https://www.luogu.com.cn/problem/P3919)。### 思路考虑如何更新。更新是一大重点。110 行未改动全部展开```:::## [P3834 【模板】可持久化线段树 2](https://www.luogu.com.cn/problem/P3834)主席树,全称可持久化权值线段树。权值,指某个值出现的次数。主席树维护区间内每个数出现的次数。102 行未改动全部展开```:::### [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)简单维护一下每个文本的长度就好。因为 `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)考虑暴力地线段树。开 $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)。这个差分好像啊。设 $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];}// +1array<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 manycin>>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 manycin>>oT_To#elseoT_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$ 个字符,如果这篇文章对你有帮助的话,麻烦点个小小的赞,谢谢!