其他分享
首页 > 其他分享> > 洛谷SP1716 GSS3 - Can you answer these queries III

洛谷SP1716 GSS3 - Can you answer these queries III

作者:互联网

题目链接

  操作一就是很简单的单点修改。操作二求最大子段和是本题的重点。
  最大子段和可能分布在这个节点的左儿子的最大子段和,要么是右儿子的最大子段和,要么是横跨两部分(左儿子的后缀加右儿子的前缀)。而当前节点的最大前缀是\(\max \{pre[ls], sum[ls] + pre[rs]\}\),最大后缀是\(\max \{suf[rs], sum[rs] + suf[ls]\}\).这样我们处理完之后就是去访问区间的最大子段和了。我们要先新开一个结构体去存储当前线段树中节点所对应的区间和,最大前缀,最大后缀,还有子段和。

struct Node {
    i64 val, pre, suf, res;
    Node () {
        pre = val = suf = res = 0;
    }
    Node (i64 a, i64 b, i64 c, i64 d) {
        val = a, pre = b, suf = c, res = d;
    }
};

  每个区间的最大子段和可能是左右两个小区间的最大子段和,也可能是左区间的紧靠右端的最大连续子段和\(+\)右区间紧靠左端的最大连续子段和,所以我们同时维护区间和,及区间紧靠左右最大子段和,进而维护每个区间的最大连续子段和,最后询问的时候也要遵循这个方法求最大子段和

#include <bits/stdc++.h>

using i64 = long long;

#define rep(i, a, n) for (int i = a; i < n; i ++ )
#define per(i, a, n) for (int i = n - 1; i >= a; i -- )
#define SZ(a) (int(a.size()))
#define pb push_back
#define all(a) a.begin(), a.end()
//head

constexpr int N = 50010;

int a[N];

struct SegmentTree {
    i64 val[N << 2], tag[N << 2], pre[N << 2], suf[N << 2];
    i64 ans[N << 2];

    void pushup(int u) {
        val[u] = val[u << 1] + val[u << 1 | 1];
        pre[u] = std::max(pre[u << 1], pre[u << 1 | 1] + val[u << 1]);
        suf[u] = std::max(suf[u << 1 | 1], val[u << 1 | 1] + suf[u << 1]);
        ans[u] = std::max({ans[u << 1], ans[u << 1 | 1], pre[u << 1 | 1] + suf[u << 1]});
    }

    void build(int u, int l, int r) {
        if (l == r) {
            val[u] = tag[u] = pre[u] = suf[u] = ans[u] = a[l];
            return ;
        }

        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }

    void modify(int u, int l, int r, int pos, i64 x) {
        if (l == r) {
            val[u] = x;
            ans[u] = x;
            pre[u] = x;
            suf[u] = x;
            return ;
        }
        int mid = l + r >> 1;
        if (pos <= mid) modify(u << 1, l, mid, pos, x);
        else modify(u << 1 | 1, mid + 1, r, pos, x);
        pushup(u);
    }

    struct Node {
        i64 val, pre, suf, res;
        Node () {
            pre = val = suf = res = 0;
        }
        Node (i64 a, i64 b, i64 c, i64 d) {
            val = a, pre = b, suf = c, res = d;
        }
    };

    Node query(int u, int l, int r, int ln, int rn) {
        if (l >= ln && r <= rn) 
            return Node(val[u], pre[u], suf[u], ans[u]);
        int mid = l + r >> 1;
        if (mid < ln) return query(u << 1 | 1, mid + 1, r, ln, rn);
        if (mid >= rn) return query(u << 1, l, mid, ln, rn);
        Node ls = query(u << 1, l, mid, ln, mid);
        Node rs = query(u << 1 | 1, mid + 1, r, mid + 1, rn);
        Node tr;
        tr.val = ls.val + rs.val;
        tr.pre = std::max(ls.pre, ls.val + rs.pre);
        tr.suf = std::max(rs.suf, rs.val + ls.suf);
        tr.res = std::max({ls.res, rs.res, ls.suf + rs.pre});
        return tr;
    }

    i64 Query(int u, int l, int r, int ln, int rn) {
        return query(u, l, r, ln, rn).res;
    }
}SGT;

int n, q;

int main() {

    scanf("%d", &n);    
    rep(i,1,n + 1) scanf("%d", a + i);

    SGT.build(1, 1, n);

    scanf("%d", &q);
    for (int i = 0; i < q; i ++ ) {
        int op;
        i64 x, y;
        scanf("%d%lld%lld", &op, &x, &y);
        if (op == 0) {
            SGT.modify(1, 1, n, x, y);
        } else {
            printf("%lld\n", SGT.Query(1, 1, n, x, y));
        }
    }

    return 0;
}

标签:pre,suf,洛谷,最大,子段,these,SP1716,i64,define
来源: https://www.cnblogs.com/Haven-/p/16214743.html