RT:
注意,不仅靠左最大值、靠右最大值会需要判定左右孩子是否为空,区间最大值也需要,注意在判定时谨慎分类讨论。
参考代码:
void pushup(int p){
const int ls = tree[p].ls, rs = tree[p].rs;
tree[p].sum = tree[ls].sum + tree[p].w + tree[rs].sum;
tree[p].siz = tree[ls].siz + 1 + tree[rs].siz;
tree[p].maxl = max({
tree[ls].maxl,
tree[ls].sum + tree[p].w + max(tree[rs].maxl, 0)
});
tree[p].maxr = max({
tree[rs].maxr,
tree[rs].sum + tree[p].w + max(tree[ls].maxr, 0)
});
tree[p].maxn = -INF;
if (ls) tree[p].maxn = max(tree[p].maxn, tree[ls].maxn);
if (rs) tree[p].maxn = max(tree[p].maxn, tree[rs].maxn);
tree[p].maxn = max(tree[p].maxn, (ls ? max(tree[ls].maxr, 0) : 0) + tree[p].w + (rs ? max(tree[rs].maxl, 0) : 0));
}
(写的有点长因为实在不会写简洁)