跳到正文

题解

题解:P5586 [P5350] 序列 (加强版)

dvsfanjo约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年10月7日
收录
2026年10月12日
更新
2026年11月11日
篇幅
约 20 分钟
题目
P5586

版本

  1. v1当前

思路

加强版使珂朵莉树的复杂度不再正确,那么只有两种数据结构来维护,一是文艺平衡树,二是块状链表,当然,由于区间复制操作,无论哪种都需要用到可持久化的技巧。

首先区间推平、区间加、区间翻转都是普通文艺平衡树的经典操作,这里直接给出板子:

基础平衡树
C++
namespace fhq_treap {
	int root, idx, val[maxn], sum[maxn], siz[maxn], ls[maxn], rs[maxn];
	struct Tag {
		int add, turn, rev;
		Tag(int a = 0, int b = -1, int c = 0) {
			add = a, turn = b, rev = c;
		}
	} tag[maxn]; 
	mt19937 rnd(time(0));
	inline int newnode(int x) {
		int u = ++idx;
		val[u] = sum[u] = x, siz[u] = 1, ls[u] = rs[u] = 0, tag[u] = Tag();
		return u;
	}
	inline int clone(int u) {
		int v = ++idx;
		val[v] = val[u], siz[v] = siz[u], tag[v] = tag[u], 
		ls[v] = ls[u], rs[v] = rs[u], sum[v] = sum[u];
		return v;
	}
	inline void pushup(int u) {
		siz[u] = siz[ls[u]] + siz[rs[u]] + 1;
		sum[u] = (sum[ls[u]] + sum[rs[u]] + val[u]) % p;
	}
	inline void apply(int u, Tag t) {
		if (!u) return;
		if (t.turn != -1) {
			val[u] = t.turn, sum[u] = t.turn * siz[u] % p;
			tag[u].turn = t.turn, tag[u].add = 0;
		} 
		if (t.add) {
			(val[u] += t.add) %= p, (sum[u] += t.add * siz[u] % p) %= p;
			if (tag[u].turn != -1) (tag[u].turn += t.add) %= p;
			else (tag[u].add += t.add) %= p;
		}
		if (t.rev) {
			tag[u].rev ^= 1, swap(ls[u], rs[u]);
		}
		return;
	}
	inline void pushdown(int u) {
		if (!u) return; 
		if (tag[u].add == 0 && tag[u].turn == -1 && tag[u].rev == 0) return; 
		if (ls[u]) ls[u] = clone(ls[u]);
		if (rs[u]) rs[u] = clone(rs[u]);
		apply(ls[u], tag[u]), apply(rs[u], tag[u]), 
		tag[u] = Tag();
	}
	void split(int u, int x, int &a, int &b) {
		if (!u) return a = b = 0, void();
		pushdown(u); 
		if (siz[ls[u]] < x) {
			a = clone(u), split(rs[a], x - siz[ls[a]] - 1, rs[a], b);
			return pushup(a);
		} else {
			b = clone(u), split(ls[b], x, a, ls[b]);
			return pushup(b);
		}
	}
	int merge(int a, int b) {
		if (!a || !b) return a | b;
		bool tmp = rnd() % (siz[a] + siz[b]) < siz[a];
		int v = clone(tmp ? a : b);
		pushdown(v); 
		if (tmp) rs[v] = merge(rs[v], b);
		else ls[v] = merge(a, ls[v]);
		return pushup(v), v;
	} 
	int a, b, c, d, e, f, ans;
	inline int query(int l, int r) {
		a = b = c = ans = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		ans = sum[b], root = merge(a, merge(b, c));
		return ans;
	}
	inline void update_tur(int l, int r, int x) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(0, x, 0)), root = merge(a, merge(b, c));
		return;
	}
	inline void update_add(int l, int r, int x) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(x, -1, 0)), root = merge(a, merge(b, c));
		return;
	}
	inline void update_rev(int l, int r) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(0, -1, 1)), root = merge(a, merge(b, c));
		return;
	}
};

可能你已经注意到了,这个文艺平衡树是基于子树大小的 RBST (随机二叉搜索树),它和 FHQ-Treap 唯一的区别是它不会给每个结点固定的键值,而是每次合并都根据子树大小随机使一个为根,其他操作和 FHQ-Treap 一样,这种方法再下面的复制操作有用。

然后先看区间交换,这个不需要可持久化,只需要将原来的平衡树拆分为:

A[1,l1),B[l1,r1],C(r1,l2),D[l2,r2],E(r2,n]A[1,l1),B[l1,r1],C(r1,l2),D[l2,r2],E(r2,n]

这五个子树,交换 BB 子树和 DD 子树后再合并,就可以实现区间交换的操作,注意:这里要分 [l1,r1][l1,r1] 和 [l2,r2][l2,r2] 的先后顺序。

区间交换
C++
inline void update_swap(int l1, int r1, int l2, int r2) {
	a = b = c = d = e = f = 0;
	if (l1 < l2) {
		split(root, l2 - 1, a, b), split(b, r2 - l2 + 1, b, c), 
		split(a, l1 - 1, d, e), split(e, r1 - l1 + 1, e, f);
	} else {
		split(root, l1 - 1, a, b), split(b, r1 - l1 + 1, b, c), 
		split(a, l2 - 1, d, e), split(e, r1 - l1 + 1, e, f); 
	}
	root = merge(merge(d, merge(b, f)), merge(e, c));
}

看到这里,区间复制的操作也很显然了,只要可持久化后把复制后的 BB 子树代替掉 DD 子树即可。

区间复制
C++
inline void update_copy(int l1, int r1, int l2, int r2) {
	a = b = c = d = e = f = 0;
	if (l1 < l2) {
		split(root, l2 - 1, a, b), split(b, r2 - l2 + 1, b, c), 
		split(a, l1 - 1, d, e), split(e, r1 - l1 + 1, e, f), b = clone(e);
	} else {
		split(root, l1 - 1, a, b), split(b, r1 - l1 + 1, b, c), 
		split(a, l2 - 1, d, e), split(e, r1 - l1 + 1, e, f), e = clone(b); 
	}
	root = merge(merge(d, merge(e, f)), merge(b, c));
}

每次操作会将一个子树复制过来,若使用 FHQ-Treap 则会导致树的平衡出问题,因为两个树直接每个结点的键值是一样的,很容易就可以卡没,而 RBST 的优势就是复制子树后,原本和复制的子树形态不同,保证了树的平衡。

同时使用 RBST 建树也会很方便,可以直接以线段树的结构强建一个树高为 O(log⁡n)O(\log n) 的树,代码的实现比 FHQ-Treap 简单很多,因为建树不需要复制结点,但是平常的 FHQ-Treap 的 merge 操作需要,于是很难去维护,而这种类似线段树的建树方法是很简单的。

然而出题人在题目中说点“本题很良心,不卡空间!”是假的,别信,因为标准写法的文艺平衡树会在 pushdown 时重新复制一遍子结点,这会让总结点数增长的非常快,然而从始至终我们都只需要维护一个序列的 nn 个点,因此考虑定量重新建树,保证总结点数在可控范围内。

建树和重建
C++
int cnt;
void print(int u = root) {
	if (!u) return; pushdown(u);
	if (u == root) cnt = 0;
	print(ls[u]), arr[++cnt] = val[u], print(rs[u]);
} 
int build(int l, int r) {
	if (l > r) return 0;
	if (l == r) return newnode(arr[l]);
	int mid = l + r >> 1, u = newnode(arr[mid]);
	ls[u] = build(l, mid - 1), rs[u] = build(mid + 1, r);
	return pushup(u), u;
}
inline void rebuild() {
	if (idx <= 1000000) return; 
	print(), idx = 0, root = build(1, n);
	return;
}

细节

  1. 注意维护标记时的顺序,我是先维护推平,再维护加法,最后维护翻转,然后讨论一下顺序做的。
  2. 注意取模,不要忘记在某些地方取模。
  3. 总结点数的阈值开到 10610^6,这样可以尽可能平衡时间与空间,当然可能还有更好的阈值,但我用的就是这个。

完整代码

轻微压行,仅供参考
C++
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int maxn = 2e6 + 5, p = 1000000007;
int n, q, arr[maxn], op, a, b, c, d, lstans;
namespace fhq_treap {
	int root, idx, val[maxn], sum[maxn], siz[maxn], ls[maxn], rs[maxn];
	struct Tag {
		int add, turn, rev;
		Tag(int a = 0, int b = -1, int c = 0) {
			add = a, turn = b, rev = c;
		}
	} tag[maxn]; 
	mt19937 rnd(time(0));
	inline int newnode(int x) {
		int u = ++idx;
		val[u] = sum[u] = x, siz[u] = 1, ls[u] = rs[u] = 0, tag[u] = Tag();
		return u;
	}
	inline int clone(int u) {
		int v = ++idx;
		val[v] = val[u], siz[v] = siz[u], tag[v] = tag[u], 
		ls[v] = ls[u], rs[v] = rs[u], sum[v] = sum[u];
		return v;
	}
	inline void pushup(int u) {
		siz[u] = siz[ls[u]] + siz[rs[u]] + 1;
		sum[u] = (sum[ls[u]] + sum[rs[u]] + val[u]) % p;
	}
	inline void apply(int u, Tag t) {
		if (!u) return;
		if (t.turn != -1) {
			val[u] = t.turn, sum[u] = t.turn * siz[u] % p;
			tag[u].turn = t.turn, tag[u].add = 0;
		} 
		if (t.add) {
			(val[u] += t.add) %= p, (sum[u] += t.add * siz[u] % p) %= p;
			if (tag[u].turn != -1) (tag[u].turn += t.add) %= p;
			else (tag[u].add += t.add) %= p;
		}
		if (t.rev) {
			tag[u].rev ^= 1, swap(ls[u], rs[u]);
		}
		return;
	}
	inline void pushdown(int u) {
		if (!u) return; 
		if (tag[u].add == 0 && tag[u].turn == -1 && tag[u].rev == 0) return; 
		if (ls[u]) ls[u] = clone(ls[u]);
		if (rs[u]) rs[u] = clone(rs[u]);
		apply(ls[u], tag[u]), apply(rs[u], tag[u]), 
		tag[u] = Tag();
	}
	void split(int u, int x, int &a, int &b) {
		if (!u) return a = b = 0, void();
		pushdown(u); 
		if (siz[ls[u]] < x) {
			a = clone(u), split(rs[a], x - siz[ls[a]] - 1, rs[a], b);
			return pushup(a);
		} else {
			b = clone(u), split(ls[b], x, a, ls[b]);
			return pushup(b);
		}
	}
	int merge(int a, int b) {
		if (!a || !b) return a | b;
		bool tmp = rnd() % (siz[a] + siz[b]) < siz[a];
		int v = clone(tmp ? a : b);
		pushdown(v); 
		if (tmp) rs[v] = merge(rs[v], b);
		else ls[v] = merge(a, ls[v]);
		return pushup(v), v;
	} 
	int a, b, c, d, e, f, ans;
	inline int query(int l, int r) {
		a = b = c = ans = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		ans = sum[b], root = merge(a, merge(b, c));
		return ans;
	}
	inline void update_tur(int l, int r, int x) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(0, x, 0)), root = merge(a, merge(b, c));
		return;
	}
	inline void update_add(int l, int r, int x) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(x, -1, 0)), root = merge(a, merge(b, c));
		return;
	}
	inline void update_rev(int l, int r) {
		a = b = c = 0, 
		split(root, l - 1, a, b), split(b, r - l + 1, b, c), 
		apply(b, Tag(0, -1, 1)), root = merge(a, merge(b, c));
		return;
	}
	inline void update_copy(int l1, int r1, int l2, int r2) {
		a = b = c = d = e = f = 0;
		if (l1 < l2) {
			split(root, l2 - 1, a, b), split(b, r2 - l2 + 1, b, c), 
			split(a, l1 - 1, d, e), split(e, r1 - l1 + 1, e, f), b = clone(e);
		} else {
			split(root, l1 - 1, a, b), split(b, r1 - l1 + 1, b, c), 
			split(a, l2 - 1, d, e), split(e, r1 - l1 + 1, e, f), e = clone(b); 
		}
		root = merge(merge(d, merge(e, f)), merge(b, c));
	}
	inline void update_swap(int l1, int r1, int l2, int r2) {
		a = b = c = d = e = f = 0;
		if (l1 < l2) {
			split(root, l2 - 1, a, b), split(b, r2 - l2 + 1, b, c), 
			split(a, l1 - 1, d, e), split(e, r1 - l1 + 1, e, f);
		} else {
			split(root, l1 - 1, a, b), split(b, r1 - l1 + 1, b, c), 
			split(a, l2 - 1, d, e), split(e, r1 - l1 + 1, e, f); 
		}
		root = merge(merge(d, merge(b, f)), merge(e, c));
	}
	int cnt;
	void print(int u = root) {
		if (!u) return; pushdown(u);
		if (u == root) cnt = 0;
		print(ls[u]), arr[++cnt] = val[u], print(rs[u]);
	} 
	int build(int l, int r) {
		if (l > r) return 0;
		if (l == r) return newnode(arr[l]);
		int mid = l + r >> 1, u = newnode(arr[mid]);
		ls[u] = build(l, mid - 1), rs[u] = build(mid + 1, r);
		return pushup(u), u;
	}
	inline void rebuild() {
		if (idx <= 1000000) return; 
		print(), idx = 0, root = build(1, n);
		return;
	}
};
using fhq_treap::root; 
using fhq_treap::build;
using fhq_treap::query;
using fhq_treap::update_tur;
using fhq_treap::update_add;
using fhq_treap::update_rev;
using fhq_treap::update_copy;
using fhq_treap::update_swap;
using fhq_treap::print; 
using fhq_treap::rebuild;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	root = build(1, n);
	for (int i = 1; i <= q; i++) {
		cin >> op >> a >> b;
		a ^= lstans, b ^= lstans;
		if (op == 1) cout << (lstans = query(a, b)) << '\n';
		if (op == 2) cin >> c, c ^= lstans, update_tur(a, b, c);
		if (op == 3) cin >> c, c ^= lstans, update_add(a, b, c);
		if (op == 4) cin >> c >> d, c ^= lstans, d ^= lstans, update_copy(a, b, c, d);
		if (op == 5) cin >> c >> d, c ^= lstans, d ^= lstans, update_swap(a, b, c, d);
		if (op == 6) update_rev(a, b); 
		rebuild();
	}
	print();
	for (int i = 1; i <= n; i++) {
		cout << arr[i] << ' ';
	} 
	return 0;
}
原文