观前提醒:我自己是写不出来这么多文字的,对于正文部分,请直接使用AI总结,使用AI分析代码
前情提要(给人看的正文)
考虑这样一个问题:
给你一个长为n的序列,完成以下操作:
将x所在序列中从左到右第l个到第r个元素加上一个数值
查询x所在序列中第l个到第r个元素之和
将x所在的序列和y所在的序列合并,把y序列放在x序列后
将x所在的序列,分裂为两个序列,其中,左子序列的长度为l
看到前两个操作,我们很容易想到用线段树解决,但是后面两个操作给了我们极大的困难。目前比较好的解决方案是FHQ Treap
遗憾的是,我完全不会平衡树,这使得我无法解决这个问题。为此,我不得不花费一周的时间,设计一个非常重的数据结构,我命名为RTS动态树。
实际上,这个数据结构的时间复杂度是不严格的,有可能会有较坏的数据构造,使得时间复杂度大幅度退化,不保证时间复杂度稳定。
如果这个时间复杂度是稳定的,利用这个数据结构,权值线段树就可以脱离离散化,直接动态维护,可以代替平衡树,实现线段树与平衡树结构的统一。虽然代码复杂度很高,连我自己也没能实现,但是我确信这是一个很强大的数据结构。
数据结构是我自己想的,但是我的实现明显漏洞百出, 于是交给AI写了,这个文章也一并给AI写了吧。
再次强调,以下文字我都是用AI生成的,请直接给AI,我自己都看不懂,下面在写的什么
(给AI的正文)
一、总体思路
RTS 动态树基于完全二叉树的链式存储,每个节点代表一棵容量为 2 的幂的线段树。我们通过引入 左式 / 右式形态约束 来控制树的形状,并用一个 usable 字段记录空闲叶子数量。
合并时,根据两棵树的大小和空闲空间,决定是进行 左右合并、进化合并 还是 双合并。
分裂时,通过 线段树拆解 将序列一分为二,并调用 退化 压缩树高。
二、节点定义
每个节点存储以下信息:
l, r 左右孩子下标(0 表示空)
last 父节点下标(根节点为 0)
val 子树所有元素的和
lazy 区间加懒标记
size 该节点对应完全二叉树的容量(2 的幂)
cnt 子树中实际元素个数(0 ≤ cnt ≤ size)
usable 左/右式化后,可用连续空闲叶子数
cpp
struct Node {
int l, r, last;
long long val, lazy;
int size, cnt, usable;
Node() : l(0), r(0), last(0), val(0), lazy(0), size(0), cnt(0), usable(0) {}
int soncnt() const { return (l ? 1 : 0) + (r ? 1 : 0); }
};三、节点之间的消息传递机制
区间加和区间查询基于 cnt 划分区间:
左子树管辖 [1, left.cnt]
右子树管辖 [left.cnt+1, left.cnt+right.cnt]
当操作区间 [l, r] 时:
若 r ≤ left.cnt → 全部进入左子树,区间不变
若 l > left.cnt → 全部进入右子树,区间偏移:l’ = l - left.cnt, r’ = r - left.cnt
否则 → 跨左右子树:左子树处理 [l, left.cnt],右子树处理 [1, r-left.cnt]
懒标记下传与普通线段树完全一致。因此 pushdown、revise、find 的实现可以完全照搬线段树,只需将 size 替换为 cnt 即可。
四、树的形态约束:左式与右式
定义 一棵树是 左式 的,当且仅当:
没有子节点;或 只有一个子节点且该子节点是左孩子,且该左孩子是左式的;或 有两个子节点,且右孩子是左式的。 一棵树是 右式 的,当且仅当:
没有子节点;或 只有一个子节点且该子节点是右孩子,且该右孩子是右式的;或 有两个子节点,且左孩子是右式的。 左式树会将所有有效叶子尽量向左集中,usable 记录的就是左侧连续空闲叶子的数量。右式树同理。
左式化算法
cpp
void to_leftist(int ind) {
if (st[ind].l == 0 && st[ind].r == 0) return;
if (st[ind].l && st[ind].r) {
to_leftist(st[ind].r);
st[ind].usable = st[st[ind].r].usable;
return;
}
// 只有一个孩子,确保它是左孩子
if (st[ind].r) {
st[ind].l = st[ind].r;
st[ind].r = 0;
}
st[ind].usable = st[ind].size / 2;
to_leftist(st[ind].l);
st[ind].usable += st[st[ind].l].usable;
}右式化 to_rightist 对称实现。
五、线段树退化(degrade)——树高压缩
退化操作消除只有单个孩子的冗余节点,并将“两个单链孩子”的情况合并为直接孙子,从而降低树高。
cpp
int degrade(int ind) {
ind = head(ind);
while (true) {
if (st[ind].l && st[ind].r) {
int l = st[ind].l, r = st[ind].r;
if (st[l].soncnt() == 1 && st[r].soncnt() == 1) {
// 四种组合:把孙子提上来
if (st[l].l && st[r].l) {
st[st[l].l].last = ind; st[st[r].l].last = ind;
st[ind].l = st[l].l; st[ind].r = st[r].l;
} else if (st[l].r && st[r].l) {
st[st[l].r].last = ind; st[st[r].l].last = ind;
st[ind].l = st[l].r; st[ind].r = st[r].l;
} else if (st[l].l && st[r].r) {
st[st[l].l].last = ind; st[st[r].r].last = ind;
st[ind].l = st[l].l; st[ind].r = st[r].r;
} else if (st[l].r && st[r].r) {
st[st[l].r].last = ind; st[st[r].r].last = ind;
st[ind].l = st[l].r; st[ind].r = st[r].r;
} else continue;
// 回收 l 和 r
st[l] = Node(); recycle.push(l);
st[r] = Node(); recycle.push(r);
continue; // 继续尝试压缩
}
} else if (st[ind].l) {
int tmp = ind;
ind = st[ind].l;
// 更新父节点指针
if (st[tmp].last) {
int fa = st[tmp].last;
if (st[fa].l == tmp) st[fa].l = ind;
else st[fa].r = ind;
}
st[ind].last = st[tmp].last;
st[tmp] = Node(); recycle.push(tmp);
} else if (st[ind].r) {
int tmp = ind;
ind = st[ind].r;
if (st[tmp].last) {
int fa = st[tmp].last;
if (st[fa].l == tmp) st[fa].l = ind;
else st[fa].r = ind;
}
st[ind].last = st[tmp].last;
st[tmp] = Node(); recycle.push(tmp);
} else {
if (st[ind].cnt == 0) {
if (st[ind].last) {
int fa = st[ind].last;
if (st[fa].l == ind) st[fa].l = 0;
else st[fa].r = 0;
}
st[ind] = Node(); recycle.push(ind);
ind = 0;
}
break;
}
}
return ind;
}六、线段树合成(合并)
合并两个序列 t1(前)和 t2(后),返回新根。
左右合并 union_left / union_right
当小树可以嵌入到大树的左侧空闲区时使用。
cpp
void union_left(int indl, int indr) {
if (!indl || !indr) return;
if (st[indr].l) {
pushdown(indr);
union_left(indl, st[indr].l);
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
return;
}
if (st[st[indr].r].usable >= st[indl].size - st[indl].usable) {
pushdown(indr);
union_left(indl, st[indr].r);
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
return;
}
// 嵌入
pushdown(indl); pushdown(indr);
st[indr].l = st[indl].l;
st[st[indl].l].last = indr;
union_left(st[indl].r, st[indr].r);
recycle.push(indl); st[indl] = Node();
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
}union_right 对称实现。
进化合并 union_left2 / union_right2
当空闲空间不足时,新建根节点,将大树作为一侧,另一侧新建空节点,再嵌入小树。
cpp
void union_left2(int indl, int indr) {
if (st[indl].size == st[indr].size / 2) {
st[indr].r = indl;
st[indl].last = indr;
return;
}
int new_node = addnode();
st[indr].r = new_node;
st[new_node].last = indr;
st[new_node].size = st[indr].size / 2;
union_left2(indl, new_node);
st[indr].val = st[new_node].val;
st[indr].cnt = st[new_node].cnt;
}union_right2 对称。
总合并函数 unite
cpp
int unite(int t1, int t2) {
if (!t1) return t2; if (!t2) return t1;
t1 = head(t1); t2 = head(t2);
if (st[t1].size < st[t2].size) {
to_leftist(t1); to_rightist(t2);
if (st[t2].usable >= st[t1].size - st[t1].usable) {
union_left(t1, t2); return t2;
} else {
int h = addnode();
st[h].size = 2 * st[t2].size;
st[h].r = t2; st[t2].last = h;
st[h].l = addnode();
st[st[h].l].last = h; st[st[h].l].size = st[h].size / 2;
union_left2(t1, st[h].l);
st[h].val = st[st[h].l].val + st[st[h].r].val;
st[h].cnt = st[st[h].l].cnt + st[st[h].r].cnt;
return h;
}
} else if (st[t1].size > st[t2].size) {
// 对称
} else {
// 双合并
int h = addnode();
st[h].l = t1; st[h].r = t2;
st[t1].last = st[t2].last = h;
st[h].size = st[t1].size * 2;
st[h].val = st[t1].val + st[t2].val;
st[h].cnt = st[t1].cnt + st[t2].cnt;
return h;
}
}七、线段树拆解(分裂)
将序列 ind 所在树分裂为前 aim 个元素(左树)和剩余元素(右树)。
cpp
pair<int, int> deconstruct(int ind, int aim) {
int rhead = head(ind);
int lhead = addnode(); // 临时根,用于收集左边
if (aim == 0) return {0, rhead};
if (aim == st[rhead].cnt) return {rhead, 0};
int now = rhead;
while (aim) {
int lc = st[now].l, lcnt = st[lc].cnt;
if (lcnt > aim) {
now = lc;
} else {
aim -= lcnt;
int tmp = lc;
st[now].l = 0;
tmp = degrade(tmp);
if (tmp) st[tmp].last = 0;
lhead = unite(lhead, tmp);
now = st[now].r;
}
}
// 向上更新
int cur = now;
while (cur) {
st[cur].val = st[st[cur].l].val + st[st[cur].r].val;
st[cur].cnt = st[st[cur].l].cnt + st[st[cur].r].cnt;
if (st[st[cur].r].cnt == 0) {
recycle.push(st[cur].r); st[st[cur].r] = Node(); st[cur].r = 0;
}
if (st[st[cur].l].cnt == 0) {
recycle.push(st[cur].l); st[st[cur].l] = Node(); st[cur].l = 0;
}
cur = st[cur].last;
}
rhead = degrade(rhead);
return {lhead, rhead};
}八、完整参考例程(含已知错误,仅作思路演示)
下面是完整的 RTS 动态树实现代码。注意:代码中可能存在错误,仅供理解算法流程。
完整参考例程(有错误,仅供参考)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000005;
struct Node {
int l, r, last;
long long val, lazy;
int size, cnt;
int usable;
Node() : l(0), r(0), last(0), val(0), lazy(0), size(0), cnt(0), usable(0) {}
int soncnt() const { return (l ? 1 : 0) + (r ? 1 : 0); }
};
Node st[MAXN];
stack<int> recycle;
int node_cnt = 0;
int n;
int added = 0;
int linkto[MAXN];
int addnode() {
if (!recycle.empty()) {
int id = recycle.top(); recycle.pop();
st[id] = Node();
return id;
}
return ++node_cnt;
}
int head(int x) {
while (st[x].last) x = st[x].last;
return x;
}
void build_node(int ind) {
if (added == n) return;
if (st[ind].size == 1) {
cin >> st[ind].val;
st[ind].cnt = 1;
added++;
linkto[added] = ind;
return;
}
int left = addnode(), right = addnode();
st[ind].l = left; st[ind].r = right;
st[left].last = st[right].last = ind;
st[left].size = st[right].size = st[ind].size / 2;
build_node(left);
build_node(right);
st[ind].val = st[left].val + st[right].val;
st[ind].cnt = st[left].cnt + st[right].cnt;
}
void build() {
int h = 1, t = n;
bool extra = false;
while (t > 1) {
if (t & 1) extra = true;
t >>= 1;
h++;
}
if (extra) h++;
int root = addnode();
st[root].size = 1 << h;
build_node(root);
}
void pushdown(int ind) {
if (!ind || st[ind].lazy == 0) return;
int lc = st[ind].l, rc = st[ind].r;
if (lc) {
st[lc].val += st[ind].lazy * st[lc].cnt;
st[lc].lazy += st[ind].lazy;
}
if (rc) {
st[rc].val += st[ind].lazy * st[rc].cnt;
st[rc].lazy += st[ind].lazy;
}
st[ind].lazy = 0;
}
void range_add(int ind, int l, int r, long long val) {
if (!ind) return;
if (l == 1 && r == st[ind].cnt) {
st[ind].val += val * st[ind].cnt;
st[ind].lazy += val;
return;
}
pushdown(ind);
int lcnt = st[st[ind].l].cnt;
if (r <= lcnt)
range_add(st[ind].l, l, r, val);
else if (l > lcnt)
range_add(st[ind].r, l - lcnt, r - lcnt, val);
else {
range_add(st[ind].l, l, lcnt, val);
range_add(st[ind].r, 1, r - lcnt, val);
}
st[ind].val = st[st[ind].l].val + st[st[ind].r].val;
}
long long range_sum(int ind, int l, int r) {
if (!ind) return 0;
if (l == 1 && r == st[ind].cnt) return st[ind].val;
pushdown(ind);
int lcnt = st[st[ind].l].cnt;
if (r <= lcnt)
return range_sum(st[ind].l, l, r);
else if (l > lcnt)
return range_sum(st[ind].r, l - lcnt, r - lcnt);
else
return range_sum(st[ind].l, l, lcnt) + range_sum(st[ind].r, 1, r - lcnt);
}
void to_leftist(int ind) {
if (st[ind].l == 0 && st[ind].r == 0) return;
if (st[ind].l && st[ind].r) {
to_leftist(st[ind].r);
st[ind].usable = st[st[ind].r].usable;
return;
}
if (st[ind].r) {
st[ind].l = st[ind].r;
st[ind].r = 0;
}
st[ind].usable = st[ind].size / 2;
to_leftist(st[ind].l);
st[ind].usable += st[st[ind].l].usable;
}
void to_rightist(int ind) {
if (st[ind].l == 0 && st[ind].r == 0) return;
if (st[ind].l && st[ind].r) {
to_rightist(st[ind].l);
st[ind].usable = st[st[ind].l].usable;
return;
}
if (st[ind].l) {
st[ind].r = st[ind].l;
st[ind].l = 0;
}
st[ind].usable = st[ind].size / 2;
to_rightist(st[ind].r);
st[ind].usable += st[st[ind].r].usable;
}
int degrade(int ind) {
ind = head(ind);
while (true) {
if (st[ind].l && st[ind].r) {
int l = st[ind].l, r = st[ind].r;
if (st[l].soncnt() == 1 && st[r].soncnt() == 1) {
if (st[l].l && st[r].l) {
st[st[l].l].last = ind;
st[st[r].l].last = ind;
st[ind].l = st[l].l;
st[ind].r = st[r].l;
} else if (st[l].r && st[r].l) {
st[st[l].r].last = ind;
st[st[r].l].last = ind;
st[ind].l = st[l].r;
st[ind].r = st[r].l;
} else if (st[l].l && st[r].r) {
st[st[l].l].last = ind;
st[st[r].r].last = ind;
st[ind].l = st[l].l;
st[ind].r = st[r].r;
} else if (st[l].r && st[r].r) {
st[st[l].r].last = ind;
st[st[r].r].last = ind;
st[ind].l = st[l].r;
st[ind].r = st[r].r;
} else {
continue;
}
st[l] = Node(); recycle.push(l);
st[r] = Node(); recycle.push(r);
st[ind].val = st[st[ind].l].val + st[st[ind].r].val;
st[ind].cnt = st[st[ind].l].cnt + st[st[ind].r].cnt;
continue;
} else {
break;
}
} else if (st[ind].l) {
int tmp = ind;
ind = st[ind].l;
if (st[tmp].last) {
int fa = st[tmp].last;
if (st[fa].l == tmp) st[fa].l = ind;
else st[fa].r = ind;
}
st[ind].last = st[tmp].last;
st[tmp] = Node(); recycle.push(tmp);
} else if (st[ind].r) {
int tmp = ind;
ind = st[ind].r;
if (st[tmp].last) {
int fa = st[tmp].last;
if (st[fa].l == tmp) st[fa].l = ind;
else st[fa].r = ind;
}
st[ind].last = st[tmp].last;
st[tmp] = Node(); recycle.push(tmp);
} else {
if (st[ind].cnt == 0) {
if (st[ind].last) {
int fa = st[ind].last;
if (st[fa].l == ind) st[fa].l = 0;
else st[fa].r = 0;
}
st[ind] = Node(); recycle.push(ind);
ind = 0;
}
break;
}
}
return ind;
}
void union_left(int indl, int indr) {
if (indl == 0 || indr == 0) return;
if (st[indr].l) {
pushdown(indr);
union_left(indl, st[indr].l);
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
return;
}
if (st[st[indr].r].usable >= st[indl].size - st[indl].usable) {
pushdown(indr);
union_left(indl, st[indr].r);
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
return;
}
pushdown(indl); pushdown(indr);
st[indr].l = st[indl].l;
st[st[indl].l].last = indr;
union_left(st[indl].r, st[indr].r);
st[indl] = Node(); recycle.push(indl);
st[indr].val = st[st[indr].l].val + st[st[indr].r].val;
st[indr].cnt = st[st[indr].l].cnt + st[st[indr].r].cnt;
}
void union_right(int indl, int indr) {
if (indl == 0 || indr == 0) return;
if (st[indl].r) {
pushdown(indl);
union_right(st[indl].r, indr);
st[indl].val = st[st[indl].l].val + st[st[indl].r].val;
st[indl].cnt = st[st[indl].l].cnt + st[st[indl].r].cnt;
return;
}
if (st[st[indl].l].usable >= st[indr].size - st[indr].usable) {
pushdown(indl);
union_right(st[indl].l, indr);
st[indl].val = st[st[indl].l].val + st[st[indl].r].val;
st[indl].cnt = st[st[indl].l].cnt + st[st[indl].r].cnt;
return;
}
pushdown(indl); pushdown(indr);
st[indl].r = st[indr].r;
st[st[indr].r].last = indl;
union_right(st[indl].l, st[indr].l);
st[indr] = Node(); recycle.push(indr);
st[indl].val = st[st[indl].l].val + st[st[indl].r].val;
st[indl].cnt = st[st[indl].l].cnt + st[st[indl].r].cnt;
}
void union_left2(int indl, int indr) {
if (st[indl].size == st[indr].size / 2) {
st[indr].r = indl;
st[indl].last = indr;
return;
}
int new_node = addnode();
st[indr].r = new_node;
st[new_node].last = indr;
st[new_node].size = st[indr].size / 2;
union_left2(indl, new_node);
st[indr].val = st[new_node].val;
st[indr].cnt = st[new_node].cnt;
}
void union_right2(int indl, int indr) {
if (st[indr].size == st[indl].size / 2) {
st[indl].l = indr;
st[indr].last = indl;
return;
}
int new_node = addnode();
st[indl].l = new_node;
st[new_node].last = indl;
st[new_node].size = st[indl].size / 2;
union_right2(new_node, indr);
st[indl].val = st[new_node].val;
st[indl].cnt = st[new_node].cnt;
}
int unite(int t1, int t2) {
if (t1 == 0) return t2;
if (t2 == 0) return t1;
t1 = head(t1); t2 = head(t2);
if (st[t1].size < st[t2].size) {
to_leftist(t1);
to_rightist(t2);
if (st[t2].usable >= st[t1].size - st[t1].usable) {
union_left(t1, t2);
return t2;
} else {
int h = addnode();
st[h].size = 2 * st[t2].size;
st[h].r = t2;
st[t2].last = h;
st[h].l = addnode();
st[st[h].l].last = h;
st[st[h].l].size = st[h].size / 2;
union_left2(t1, st[h].l);
st[h].val = st[st[h].l].val + st[st[h].r].val;
st[h].cnt = st[st[h].l].cnt + st[st[h].r].cnt;
return h;
}
} else if (st[t1].size > st[t2].size) {
to_leftist(t1);
to_rightist(t2);
if (st[t1].usable >= st[t2].size - st[t2].usable) {
union_right(t1, t2);
return t1;
} else {
int h = addnode();
st[h].size = 2 * st[t1].size;
st[h].l = t1;
st[t1].last = h;
st[h].r = addnode();
st[st[h].r].last = h;
st[st[h].r].size = st[h].size / 2;
union_right2(st[h].r, t2);
st[h].val = st[st[h].l].val + st[st[h].r].val;
st[h].cnt = st[st[h].l].cnt + st[st[h].r].cnt;
return h;
}
} else {
int h = addnode();
st[h].l = t1; st[h].r = t2;
st[t1].last = st[t2].last = h;
st[h].size = st[t1].size * 2;
st[h].val = st[t1].val + st[t2].val;
st[h].cnt = st[t1].cnt + st[t2].cnt;
return h;
}
}
pair<int, int> deconstruct(int ind, int aim) {
int rhead = head(ind);
int lhead = addnode();
if (aim == 0) return {0, rhead};
if (aim == st[rhead].cnt) return {rhead, 0};
int now = rhead;
while (aim > 0) {
int lc = st[now].l;
int lcnt = st[lc].cnt;
if (lcnt > aim) {
now = lc;
} else {
aim -= lcnt;
int tmp = lc;
st[now].l = 0;
tmp = degrade(tmp);
if (tmp) st[tmp].last = 0;
lhead = unite(lhead, tmp);
now = st[now].r;
}
}
int cur = now;
while (cur) {
st[cur].val = st[st[cur].l].val + st[st[cur].r].val;
st[cur].cnt = st[st[cur].l].cnt + st[st[cur].r].cnt;
if (st[st[cur].r].cnt == 0) {
recycle.push(st[cur].r);
st[st[cur].r] = Node();
st[cur].r = 0;
}
if (st[st[cur].l].cnt == 0) {
recycle.push(st[cur].l);
st[st[cur].l] = Node();
st[cur].l = 0;
}
cur = st[cur].last;
}
rhead = degrade(rhead);
return {lhead, rhead};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
build();
int m;
cin >> m;
int op, x, y, l, r, num;
while (m--) {
cin >> op;
if (op == 1) {
cin >> x >> l >> r >> num;
int root = head(linkto[x]);
range_add(root, l, r, num);
} else if (op == 2) {
cin >> x >> l >> r;
int root = head(linkto[x]);
cout << range_sum(root, l, r) << '\n';
} else if (op == 3) {
cin >> x >> y;
unite(linkto[x], linkto[y]);
} else if (op == 4) {
cin >> x >> l;
deconstruct(linkto[x], l);
}
}
return 0;
}来自作者的补充:
可以观察到,size/cnt越大的树,需要令这个树退化的合并操作,那棵往上合并的树,稀疏度也会很大,这本身就是一个负反馈的过程,越稀疏越不容易退化,构造一稀疏度很大的树,用于提升稀疏度,这个要求也是非常高的,即使真的能大规模退化,这个不断构造使数据结构完全退化的过程,将比操作的规模要大的多。我认为这个复杂度是乐观的,但是我不能证明。