当一个区间元素会变化时如何快速求得区间的最大连续子段和
int w[N];
struct Node
{
int l, r;
int tmax, lmax, rmax, sum;//最大连续字段和、最大前缀和、最大后缀和、区间元素和
}tr[N * 4];
void pushup(Node &u, Node &l, Node &r)
{
u.tmax = max({l.tmax, r.tmax, l.rmax + r.lmax});
u.lmax = max(l.sum + r.lmax, l.lmax);
u.rmax = max(r.sum + l.rmax, r.rmax);
u.sum = l.sum + r.sum;
}
void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r) tr[u] = {l, r, w[l], w[l], w[l], w[l]};
else
{
tr[u] = {l, r};
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u, int x, int v)
{
if (tr[u].l == x && tr[u].r == x) tr[u] = {x, x, v, v, v, v};
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
pushup(u);
}
}
Node query(int u, int l, int r)
{
if (l <= tr[u].l && tr[u].r <= r) return tr[u];
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
else if (l > mid) return query(u << 1 | 1, l, r);
else
{
Node left, right, res;
left = query(u << 1, l, r);
right = query(u << 1 | 1, l, r);
pushup(res, left, right);
return res;
}
}
}