正文
增加 670 行删除 20 行
123456789101152 行未改动646566676869707172737422 行未改动97989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113238 行未改动17117217317417517617717817933 行未改动2132142152162172182192204 行未改动225226227228229230231232233234235236237238239108 行未改动全部展开348349350351352353354355356357358364 行未改动全部展开723724725726>请确保你已经学会二叉搜索树。>主要讲解序列平衡树(普通平衡树可以使用值域线段树之类的替代)。### 无旋 Treap/FHQ TreapFHQ Treap 对于有一个**随机优先级**。FHQ Treap 满足**二叉搜索树**和**堆**的性质,即:- 对于树中的任意一个节点,**其左子树中所有节点的权值都小于该节点的权值;其右子树中所有节点的权值都大于该节点的权值**。- 整棵树必须满足**父节点的优先级大于(或小于)子节点的优先级**。52 行未改动分裂过程:当前遍历到了 $u$ 这个节点:- 若左子树加上 $u$ 这个节点这些的大小(即左子树子树大小加一) $\le k$,说明当前节点包括左子树都给 $L$,然后将 $k$ 减去对应大小分裂右子树。- 否则,给 $R$,分裂左子树。```cppvoid split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;22 行未改动那么,考虑当前应该谁接在谁下面。- 若 $L$ 的优先级 $< R$ 的,则 $L$ 成为根结点,由于 $R$ 的权值 $\ge L$,所以 $R$ 和 $L$ 的右子树合并。- 否则,$R$ 成为根节点,$R$ 和 $L$ 左子树合并。```cppvoid split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;return;}pushdown(u);if (t[t[u].ls].siz + 1 <= x) {l = u;split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, r);} else {r = u;split(t[u].ls, x, l, t[u].ls);}pushup(u);}```回到这一题,修改一个区间直接打上翻转标记即可:```cppint l, r, p;split(root, y, l, r);split(l, x - 1, l, p);t[p].lzy ^= 1;root = merge(merge(l, p), r);```::::success[完整代码]```cpp#include <bits/stdc++.h>#define int long long38 行未改动pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;if (t[l].pri > t[r].pri) {pushdown(l);t[l].rs = merge(t[l].rs, r);pushup(l);return l;33 行未改动}```::::### 例题#### [P2042 [NOI2005] 维护数列](https://www.luogu.com.cn/problem/P2042)4 行未改动| 1 | 插入 | $\operatorname{INSERT}\ posi \ tot \ c_1 \ c_2 \cdots c_{tot}$ | 在当前数列的第 $posi$ 个数字后插入 $tot$ 个数字:$c_1, c_2 \cdots c_{tot}$;若在数列首插入,则 $posi$ 为 $0$ || 2 | 删除 | $\operatorname{DELETE} \ posi \ tot$ | 从当前数列的第 $posi$ 个数字开始连续删除 $tot$ 个数字 || 3 | 修改 | $\operatorname{MAKE-SAME} \ posi \ tot \ c$ | 从当前数列的第 $posi$ 个数字开始的连续 $tot$ 个数字统一修改为 $c$ || 4 | 翻转 | $\operatorname{REVERSE} \ posi \ tot$ | 取出从当前数列的第 $posi$ 个数字开始的 $tot$ 个数字,翻转后放入原来的位置 || 5 | 求和 | $\operatorname{GET-SUM} \ posi \ tot$ | 计算从当前数列的第 $posi$ 个数字开始的 $tot$ 个数字的和并输出 || 6 | 求最大子串和 | $\operatorname{MAX-SUM}$ | 求出当前数列中和最大的一段**非空**子串,并输出最大和 |---考察你是否理解平衡树。对于每个位置,除了平衡树基本信息,维护翻转标记,子树和,子树最大前缀和,最大后缀和,最大子段和,赋值标记,合并左右儿子信息和小白逛公园一样。这题需要垃圾回收。注意下传标记时的顺序,需要先下传赋值标记,再下传翻转标记。108 行未改动全部展开完整代码就不放了。#### [P4036 [JSOI2008] 火星人](https://www.luogu.com.cn/problem/P4036)#### [P5066 [Ynoi Easy Round 2014] 人人本着正义之名](https://www.luogu.com.cn/problem/P5066)>你需要帮珂朵莉维护一个长为 $n$ 的 `01`序列 $a$,有 $m$ 个操作:>- `1 l r`:把区间 $[l,r]$ 的数变成 $0$。>- `2 l r`:把区间 $[l,r]$ 的数变成 $1$。>- `3 l r`:$[l,r-1]$ 内所有数 $a_i$,变为 $a_i$ 与 $a_{i+1}$ 按位或的值,这些数同时进行这个操作。>- `4 l r`:$[l+1,r]$ 内所有数 $a_i$,变为 $a_i$ 与 $a_{i-1}$ 按位或的值,这些数同时进行这个操作。364 行未改动全部展开while (min(t[root].Min[0], t[root].Min[1]) <= 0) clear(root);}```::::1234567891011121352 行未改动66676869707172737475767778798022 行未改动10310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015138 行未改动19019119219319419519619719833 行未改动2322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364 行未改动441442443444445446447448449450451452453454455456108 行未改动全部展开56556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004364 行未改动全部展开13691370137113721373137413751376>请确保你已经学会二叉搜索树(BST)。>主要讲解序列平衡树(普通平衡树可以使用值域线段树之类的替代)。由于本人是小升**区** xxs,数据结构学的不多,文章有错误还请多多谅解。### 无旋 Treap/FHQ TreapFHQ Treap 的每个节点都有一个**随机优先级**。FHQ Treap 满足**二叉搜索树**和**堆**的性质,即:- 对于树中的任意一个节点,**其左子树中所有节点的权值都小于该节点的权值;其右子树中所有节点的权值都大于该节点的权值**。- 整棵树必须满足**父节点的优先级大于(或小于)子节点的优先级**。52 行未改动分裂过程:当前遍历到了 $u$ 这个节点:- 若左子树加上 $u$ 这个节点这些的大小(即左子树子树大小加一)$\le k$,说明当前节点包括左子树都给 $L$,然后将 $k$ 减去对应大小分裂右子树。- 否则,给 $R$,分裂左子树。::::info[时间复杂度证明]对于最坏情况,因为每次只会递归一个方向,显然是递归深度等于从根到叶的最长路径长度,也就是树高 $h$,所以复杂度是 $O(h)$ 的。::::```cppvoid split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;22 行未改动那么,考虑当前应该谁接在谁下面。- 若 $L$ 的优先级 $< R$ 的,则 $L$ 成为根结点,由于 $R$ 的权值 $\ge L$,所以 $R$ 和 $L$ 的右子树合并。- 否则,$R$ 成为根节点,$R$ 和 $L$ 左子树合并。::::info[时间复杂度证明]和 `merge` 一样,每次往一个方向递归,最多递归树高 $h$ 次,所以复杂度也是 $O(h)$。::::```cppint merge(int l, int r) {if (l == 0 || r == 0) return l + r;if (t[l].pri < t[r].pri) {pushdown(l);t[l].rs = merge(t[l].rs, r);pushup(l);return l;} else {pushdown(r);t[r].ls = merge(l, t[r].ls);pushup(r);return r;}}```回到这一题,修改一个区间直接分裂出来打上翻转标记即可:```cppint l, r, p;split(root, y, l, r);split(l, x - 1, l, p);t[p].lzy ^= 1;root = merge(merge(l, p), r);```::::info[复杂度证明]空间复杂度显然是 $O(n)$ 的。FHQ Treap 的形态等价于将节点按优先级排序后依次插入 BST 得到的形态,而由于优先级随机,且随机插入 BST 的期望树高为 $O(\log n)$,所以 FHQ Treap 期望树高为 $O(\log n)$。对于一次操作,只需要 $O(1)$ 次 `split` 和 `merge` 和 $O(1)$ 的修改,由于 `split` 和 `merge` 操作复杂度是 $O(h)$,而树高期望 $O(\log n)$,所以复杂度是 $O(\log n)$。设 $n, m$ 同阶,那么总复杂度为 $O(n\log n)$。::::::::success[完整代码]```cpp#include <bits/stdc++.h>#define int long long38 行未改动pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;if (t[l].pri < t[r].pri) {pushdown(l);t[l].rs = merge(t[l].rs, r);pushup(l);return l;33 行未改动}```::::### 可持久化 FHQ Treap>【模板】可持久化文艺平衡树>>您需要写一种数据结构,来维护一个序列,其中需要提供以下操作(**对于各个以往的历史版本**):>>1. 在第 $p$ 个数后插入数 $x$。>2. 删除第 $p$ 个数。>3. 翻转区间 $[l,r]$,例如原序列是 $\{5,4,3,2,1\}$,翻转区间 $[2,4]$ 后,结果是 $\{5,2,3,4,1\}$。>4. 查询区间 $[l,r]$ 中所有数的和。>>**和原本平衡树不同的一点是,每一次的任何操作都是基于某一个历史版本,同时生成一个新的版本(操作 $4$ 即保持原版本无变化),新版本即编号为此次操作的序号。**对于 $1, 2$ 操作,分裂出来,在丢弃/加入一个节点再合并回去即可;对于 $4$ 操作,平衡树每个节点维护子树和,把区间分裂出来输出这个和即可。其实和普通的 FHQ Treap 区别就是:后面的操作不能影响到前面的节点,于是当一个节点发生改变时,要将自己复制一遍。对于每个版本,需要独立记录一个根节点。```cppint copy_node(int u) {t[++cnt] = t[u];return cnt;}```在分裂与合并时,根节点发生了改变,需要将当前根节点复制一遍。```cppvoid split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;return;}u = copy_node(u);pushdown(u);if (t[t[u].ls].siz + 1 <= x) {l = u;split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, r);} else {r = u;split(t[u].ls, x, l, t[u].ls);}pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;pushdown(l), pushdown(r);if (t[l].pri < t[r].pri) {int x = copy_node(l);t[x].rs = merge(t[x].rs, r);pushup(x);return x;} else {int x = copy_node(r);t[x].ls = merge(l, t[x].ls);pushup(x);return x;}}```::::info[复杂度证明]对于时间复杂度,由于 `split` 和 `merge` 只是多进行了 $O(1)$ 的复制节点,所以复杂度还是 $O(\log n)$,总复杂度还是 $O(n\log n)$。由于 `split` 和 `merge` 最多递归 $O(\log n)$ 次,每次操作进行 $O(1)$ 次 `split` 和 `merge`,总共复制 $O(\log n)$ 个节点,所以单次操作空间复杂度为 $O(\log n)$。那么总空间复杂度为 $O(n + m\log n)$。::::::::success[完整代码]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int MAXN = 2e7 + 10;int n;int root[MAXN], cnt;struct node {int ls, rs;int key, pri;int siz, sum;int lzy;} t[MAXN];int new_node(int x) {t[++cnt].ls = 0;t[cnt].rs = 0;t[cnt].key = x, t[cnt].pri = rand();t[cnt].siz = 1;t[cnt].sum = x;t[cnt].lzy = 0;return cnt;}void pushup(int u) {t[u].sum = t[t[u].ls].sum + t[t[u].rs].sum + t[u].key;t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;}int copy_node(int u) {t[++cnt] = t[u];return cnt;}void pushdown(int u) {if (t[u].lzy) {if (t[u].ls) {t[u].ls = copy_node(t[u].ls);t[t[u].ls].lzy ^= 1;}if (t[u].rs) {t[u].rs = copy_node(t[u].rs);t[t[u].rs].lzy ^= 1;}swap(t[u].ls, t[u].rs);t[u].lzy = 0;}}void split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;return;}u = copy_node(u);pushdown(u);if (t[t[u].ls].siz + 1 <= x) {l = u;split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, r);} else {r = u;split(t[u].ls, x, l, t[u].ls);}pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;pushdown(l), pushdown(r);if (t[l].pri < t[r].pri) {int x = copy_node(l);t[x].rs = merge(t[x].rs, r);pushup(x);return x;} else {int x = copy_node(r);t[x].ls = merge(l, t[x].ls);pushup(x);return x;}}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);srand(time(0));cin >> n;int last = 0;for (int i = 1; i <= n; i++) {int v, op;cin >> v >> op;int p, x, l, r;if (op == 1) {cin >> p >> x;p ^= last;x ^= last;int a, b;split(root[v], p, a, b);root[i] = merge(merge(a, new_node(x)), b);} else if (op == 2) {cin >> p;p ^= last;int a, b, c;split(root[v], p - 1, a, b);split(b, 1, b, c);root[i] = merge(a, c);} else if (op == 3) {cin >> l >> r;l ^= last;r ^= last;int a, b, c;split(root[v], l - 1, a, b);split(b, r - l + 1, b, c);b = copy_node(b);t[b].lzy ^= 1;root[i] = merge(merge(a, b), c);} else if (op == 4) {cin >> l >> r;l ^= last;r ^= last;int a, b, c;split(root[v], l - 1, a, b);split(b, r - l + 1, b, c);last = t[b].sum;cout << last << '\n';root[i] = merge(merge(a, b), c);}}return 0;}```::::### 例题#### [P2042 [NOI2005] 维护数列](https://www.luogu.com.cn/problem/P2042)4 行未改动| 1 | 插入 | $\operatorname{INSERT}\ posi \ tot \ c_1 \ c_2 \cdots c_{tot}$ | 在当前数列的第 $posi$ 个数字后插入 $tot$ 个数字:$c_1, c_2 \cdots c_{tot}$;若在数列首插入,则 $posi$ 为 $0$ || 2 | 删除 | $\operatorname{DELETE} \ posi \ tot$ | 从当前数列的第 $posi$ 个数字开始连续删除 $tot$ 个数字 || 3 | 修改 | $\operatorname{MAKE-SAME} \ posi \ tot \ c$ | 从当前数列的第 $posi$ 个数字开始的连续 $tot$ 个数字统一修改为 $c$ || 4 | 翻转 | $\operatorname{REVERSE} \ posi \ tot$ | 取出从当前数列的第 $posi$ 个数字开始的 $tot$ 个数字,翻转后放入原来的位置 || 5 | 求和 | $\operatorname{GET-SUM} \ posi \ tot$ | 计算从当前数列的第 $posi$ 个数字开始的 $tot$ 个数字的和并输出 || 6 | 求最大子串和 | $\operatorname{MAX-SUM}$ | 求出当前数列中和最大的一段**非空**子串,并输出最大和 |---考察你是否理解平衡树。对于每个位置,除了平衡树基本信息,维护翻转标记,子树和,子树最大前缀和,最大后缀和,最大子段和,赋值标记,合并左右儿子信息和小白逛公园一样,注意最大子段和不能为空。这题需要垃圾回收。注意下传标记时的顺序,需要先下传赋值标记,再下传翻转标记。108 行未改动全部展开完整代码就不放了。#### [P4036 [JSOI2008] 火星人](https://www.luogu.com.cn/problem/P4036)>定义了一个函数 $\operatorname{LCQ}(x, y)$,表示:该字符串中第 $x$ 个字符开始的字串,与该字符串中第 $y$ 个字符开始的字串,两个字串的公共前缀的长度。比方说。>你需要对一个字符串做以下操作:>1. 询问。语法:$Q$ $x$ $y$,$x, y$ 均为正整数。功能:计算 $LCQ(x,y)$。限制:$1 \le x, y \le$ 当前字符串长度。>2. 修改。语法:$R$ $x$ $d$,$x$ 是正整数,$d$ 是字符。功能:将字符串中第 $x$ 个字符修改为字符 $d$。限制:$x$ 不超过当前字符串长度。>3. 插入:语法:$I$ $x$ $d$,$x$ 是非负整数,$d$ 是字符。功能:在字符串第 $x$ 个字符之后插入字符 $d$,如果 $x=0$,则在字符串开头插入。限制:$x$ 不超过当前字符串长度。---对于修改和插入操作,显然是平衡树,于是考虑使用 FHQ Treap 维护。考虑怎么进行查询操作。我们可以考虑二分长度,然后使用 Hash 判断是否相等,于是考虑平衡树维护字数哈希值。考虑怎么维护子树哈希值。对于左儿子的子树哈希值,显然可以直接加过来;对于当前节点的哈希值,由于需要接在左子树后面,所以为 $val\times base^{siz_{l}}$,其中 $siz$ 表示子树大小;那么,对于右子树,哈希值为 $sum\times base^{siz_{l} + 1}$。::::success[pushup code]```cppvoid pushup(int u) {t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;t[u].sum = t[t[u].ls].sum + t[u].key * B[t[t[u].ls].siz] + t[t[u].rs].sum * B[t[t[u].ls].siz + 1];}```::::::::success[完整代码]```cpp//我是使用的自然溢出哈希#include <bits/stdc++.h>#define int long long#define ull unsigned long longusing namespace std;const int MAXN = 2e6 + 10;const int base = 137;int n, m;int cnt, root;struct node {int ls, rs;int pri;int siz, lzy;ull sum, key;} t[MAXN];ull B[MAXN];int new_node(ull x) {cnt++;t[cnt].siz = 1;t[cnt].ls = t[cnt].rs = 0;t[cnt].key = t[cnt].sum = x;t[cnt].pri = rand();return cnt;}void pushup(int u) {t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;t[u].sum = t[t[u].ls].sum + t[u].key * B[t[t[u].ls].siz] + t[t[u].rs].sum * B[t[t[u].ls].siz + 1];}void split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;return;}if (t[t[u].ls].siz + 1 <= x) {l = u;split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, r);} else {r = u;split(t[u].ls, x, l, t[u].ls);}pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;if (t[l].pri > t[r].pri) {t[l].rs = merge(t[l].rs, r);pushup(l);return l;} else {t[r].ls = merge(l, t[r].ls);pushup(r);return r;}}ull query(int x, int k) {int l, r, p;split(root, x - 1, l, r);split(r, k, r, p);ull res = t[r].sum;root = merge(merge(l, r), p);return res;}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);srand(time(0));B[0] = 1;for (int i = 1; i <= 1000000; i++) B[i] = B[i - 1] * base;string s;cin >> s;n = s.size();s = ' ' + s;for (int i = 1; i <= n; i++) root = merge(root, new_node(s[i] - 'a' + 1));cin >> m;while (m--) {char op;cin >> op;int x, y;cin >> x;if (op == 'Q') {cin >> y;int l = 0, r = min(n - x + 1, n - y + 1);while (l <= r) {int mid = (l + r) >> 1;if (query(x, mid) == query(y, mid)) l = mid + 1;else r = mid - 1;}cout << r << '\n';} else if (op == 'R') {char c;cin >> c;int qwq = c - 'a' + 1;int l, r, p;split(root, x - 1, l, r);split(r, 1, r, p);t[r].key = qwq;pushup(r);root = merge(merge(l, r), p);} else if (op == 'I') {char c;cin >> c;int qwq = c - 'a' + 1;int l, r;n++;split(root, x, l, r);root = merge(l, merge(new_node(qwq), r));}}return 0;}```::::#### [P3215 [HNOI2011] 括号修复 / [JSOI2011] 括号序列](https://www.luogu.com.cn/problem/P3215)>一个合法的括号序列是这样定义的:>>1. 空串是合法的。>2. 如果字符串 `S` 是合法的,则 `(S)` 也是合法的。>3. 如果字符串 `A` 和 `B` 是合法的,则 `AB` 也是合法的。>>现在给你一个长度为 $n$ 的由 `(` 和 `)` 组成的字符串,位置标号从 $1$ 到 $n$。对这个字符串有下列四种操作:>- `Replace a b c`:将 $[a,b]$ 之间的所有括号改成 $c$。假设原来的字符串为:`))())())(`,那么执行操作 `Replace 2 7 (` 后原来的字符串变为:`)(((((()(`。>- `Swap a b`:将 $[a,b]$ 之间的字符串翻转。假设原来的字符串为:`))())())(`,那么执行操作 `Swap 3 5` 后原来的字符串变为:`))))(())(`。>- `Invert a b`:将 $[a,b]$ 之间的 `(` 变成 `)`,`)` 变成 `(`。假设原来的字符串为:`))())())(`,那么执行操作 `Invert 4 8` 后原来的字符串变为:`))((()(((`。>>- `Query a b`:询问 $[a,b]$ 之间的字符串至少要改变多少位才能变成合法的括号序列。改变某位是指将该位的 `(` 变成 `)` 或 `)` 变成 `(`。注意执行操作 `Query` 并不改变当前的括号序列。假设原来的字符串为:`))())())(`,那么执行操作 `Query 3 6` 的结果为 $2$,因为要将位置 $5$ 的 `)` 变成 `(` 并将位置 $6$ 的 `(` 变成 `)`。---首先,考虑查询操作怎么做。我们把 $\texttt{(}$ 看成 $-1$,$\texttt{)}$ 看成 $1$。那么,一个括号串合法的条件为:- 总和为 $0$。- 前缀和都 $\le 0$。- 后缀和都 $\ge 0$。我们令 $\text{premax}$ 表示前缀和最大值,$\text{sufmin}$ 表示后缀和最小值。每次把一个 $\texttt{)}$ 改成 $\texttt{(}$,所有后续前缀和都减少 $2$,所以 $\text{premax}$ 减少 $2$。由于 $\text{sufmin}$ 是负的,取绝对值后变后缀左括号比右括号多了多少个。每次把一个 $\texttt{(}$ 改成 $\texttt{)}$,所有前驱后缀和绝对值都减少 $2$。那么,可推出一个区间最少要改多少次才能合法的公式为:$$\lceil \frac{\text{premax}}{2} \rceil+\lceil \frac{|\text{sufmin}|}{2} \rceil$$于是,FHQ Treap 维护子树和,前缀和最大、最小,后缀和最大、最小,以及三种标记。对于区间取反,前缀和最大需要变成负的前缀和最小,其他同理;另外两个修改是简单的。对于标记顺序,为取反、赋值、翻转。::::success[code]```cppstruct node {int ls, rs;int key, pri;int siz, sum;int fz, lzy, qf;int pm, pi, sm, si;//前缀最大、最小,后缀最大、最小} t[MAXN];int new_node(int x) {t[++cnt].siz = 1;t[cnt].ls = t[cnt].rs = t[cnt].lzy = t[cnt].qf = 0;t[cnt].fz = 0;t[cnt].key = t[cnt].sum = x;t[cnt].pri = rand();t[cnt].pm = t[cnt].pi = t[cnt].sm = t[cnt].si = 0;if (x == 1) t[cnt].pm = t[cnt].sm = 1;else t[cnt].pi = t[cnt].si = -1;return cnt;}void pushup(int u) {t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;t[u].sum = t[t[u].ls].sum + t[t[u].rs].sum + t[u].key;t[u].pm = max(t[t[u].ls].pm, t[t[u].ls].sum + t[u].key + t[t[u].rs].pm);t[u].pi = min(t[t[u].ls].pi, t[t[u].ls].sum + t[u].key + t[t[u].rs].pi);t[u].sm = max(t[t[u].rs].sm, t[t[u].rs].sum + t[u].key + t[t[u].ls].sm);t[u].si = min(t[t[u].rs].si, t[t[u].rs].sum + t[u].key + t[t[u].ls].si);}void reverse(int u) {if (u == 0) return;swap(t[u].ls, t[u].rs);swap(t[u].pm, t[u].sm);swap(t[u].pi, t[u].si);t[u].lzy ^= 1;}void cover(int u, int s) {if (u == 0) return;t[u].key = t[u].fz = s;t[u].sum = t[u].siz * s;// t[u].lzy = t[u].qf = 0;if (s == 1) {t[u].pm = t[u].sm = t[u].sum;t[u].pi = t[u].si = 0;} else {t[u].pm = t[u].sm = 0;t[u].pi = t[u].si = t[u].sum;}}void qwq(int u) {if (u == 0) return;t[u].sum *= -1, t[u].key *= -1;t[u].qf ^= 1;t[u].fz *= -1;int x = t[u].pm, y = t[u].pi;t[u].pm = -y, t[u].pi = -x;x = t[u].sm, y = t[u].si;t[u].sm = -y, t[u].si = -x;}void pushdown(int u) {if (u == 0) return;if (t[u].qf) {if (t[u].ls) qwq(t[u].ls);if (t[u].rs) qwq(t[u].rs);t[u].qf = 0;}if (t[u].fz) {if (t[u].ls) cover(t[u].ls, t[u].fz);if (t[u].rs) cover(t[u].rs, t[u].fz);t[u].fz = 0;}if (t[u].lzy) {if (t[u].ls) reverse(t[u].ls);if (t[u].rs) reverse(t[u].rs);t[u].lzy = 0;}}```::::::::success[完整代码]```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int MAXN = 4e6 + 10;const int inf = 1e18;int cnt, root;struct node {int ls, rs;int key, pri;int siz, sum;int fz, lzy, qf;int pm, pi, sm, si;//前缀最大、最小,后缀最大、最小} t[MAXN];int new_node(int x) {t[++cnt].siz = 1;t[cnt].ls = t[cnt].rs = t[cnt].lzy = t[cnt].qf = 0;t[cnt].fz = 0;t[cnt].key = t[cnt].sum = x;t[cnt].pri = rand();t[cnt].pm = t[cnt].pi = t[cnt].sm = t[cnt].si = 0;if (x == 1) t[cnt].pm = t[cnt].sm = 1;else t[cnt].pi = t[cnt].si = -1;return cnt;}void pushup(int u) {t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1;t[u].sum = t[t[u].ls].sum + t[t[u].rs].sum + t[u].key;t[u].pm = max(t[t[u].ls].pm, t[t[u].ls].sum + t[u].key + t[t[u].rs].pm);t[u].pi = min(t[t[u].ls].pi, t[t[u].ls].sum + t[u].key + t[t[u].rs].pi);t[u].sm = max(t[t[u].rs].sm, t[t[u].rs].sum + t[u].key + t[t[u].ls].sm);t[u].si = min(t[t[u].rs].si, t[t[u].rs].sum + t[u].key + t[t[u].ls].si);}void reverse(int u) {if (u == 0) return;swap(t[u].ls, t[u].rs);swap(t[u].pm, t[u].sm);swap(t[u].pi, t[u].si);t[u].lzy ^= 1;}void cover(int u, int s) {if (u == 0) return;t[u].key = t[u].fz = s;t[u].sum = t[u].siz * s;// t[u].lzy = t[u].qf = 0;if (s == 1) {t[u].pm = t[u].sm = t[u].sum;t[u].pi = t[u].si = 0;} else {t[u].pm = t[u].sm = 0;t[u].pi = t[u].si = t[u].sum;}}void qwq(int u) {if (u == 0) return;t[u].sum *= -1, t[u].key *= -1;t[u].qf ^= 1;t[u].fz *= -1;int x = t[u].pm, y = t[u].pi;t[u].pm = -y, t[u].pi = -x;x = t[u].sm, y = t[u].si;t[u].sm = -y, t[u].si = -x;}void pushdown(int u) {if (u == 0) return;if (t[u].qf) {if (t[u].ls) qwq(t[u].ls);if (t[u].rs) qwq(t[u].rs);t[u].qf = 0;}if (t[u].fz) {if (t[u].ls) cover(t[u].ls, t[u].fz);if (t[u].rs) cover(t[u].rs, t[u].fz);t[u].fz = 0;}if (t[u].lzy) {if (t[u].ls) reverse(t[u].ls);if (t[u].rs) reverse(t[u].rs);t[u].lzy = 0;}}void split(int u, int x, int &l, int &r) {if (u == 0) {l = r = 0;return;}pushdown(u);if (t[t[u].ls].siz + 1 <= x) {l = u;split(t[u].rs, x - t[t[u].ls].siz - 1, t[u].rs, r);} else {r = u;split(t[u].ls, x, l, t[u].ls);}pushup(u);}int merge(int l, int r) {if (l == 0 || r == 0) return l + r;if (t[l].pri > t[r].pri) {pushdown(l);t[l].rs = merge(t[l].rs, r);pushup(l);return l;} else {pushdown(r);t[r].ls = merge(l, t[r].ls);pushup(r);return r;}}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);srand(time(0));int n, m;cin >> n >> m;for (int i = 1; i <= n; i++) {char c;cin >> c;root = merge(root, new_node((c == '(' ? -1 : 1)));}while (m--) {string op;int x, y;char c;cin >> op;if (op == "Replace") {cin >> x >> y >> c;int l, r, p;split(root, x - 1, l, r);split(r, y - x + 1, r, p);cover(r, (c == '(' ? -1 : 1));root = merge(merge(l, r), p);} else if (op == "Swap") {cin >> x >> y;int l, r, p;split(root, x - 1, l, r);split(r, y - x + 1, r, p);reverse(r);root = merge(merge(l, r), p);} else if (op == "Invert") {cin >> x >> y;int l, r, p;split(root, x - 1, l, r);split(r, y - x + 1, r, p);qwq(r);root = merge(merge(l, r), p);} else {cin >> x >> y;int l, r, p;split(root, x - 1, l, r);split(r, y - x + 1, r, p);cout << ((t[r].pm + 1) / 2) + ((abs(t[r].si) + 1) / 2) << '\n';root = merge(merge(l, r), p);}}return 0;}```::::#### [P5066 [Ynoi Easy Round 2014] 人人本着正义之名](https://www.luogu.com.cn/problem/P5066)>你需要帮珂朵莉维护一个长为 $n$ 的 `01` 序列 $a$,有 $m$ 个操作:>- `1 l r`:把区间 $[l,r]$ 的数变成 $0$。>- `2 l r`:把区间 $[l,r]$ 的数变成 $1$。>- `3 l r`:$[l,r-1]$ 内所有数 $a_i$,变为 $a_i$ 与 $a_{i+1}$ 按位或的值,这些数同时进行这个操作。>- `4 l r`:$[l+1,r]$ 内所有数 $a_i$,变为 $a_i$ 与 $a_{i-1}$ 按位或的值,这些数同时进行这个操作。364 行未改动全部展开while (min(t[root].Min[0], t[root].Min[1]) <= 0) clear(root);}```::::---本文章使用了 deepseek-v4-pro 进行润色以保证格式的正确。