AcWing 245. 你能回答这些问题吗

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m;
int a[N];
struct Node {
    int l, r;
    int sum, lmax, rmax, tmax;
    //sum,
    //lmax最大前缀和
    //rmax最大后缀和
    //tmax最大连续子段和
}tr[N * 4];
void pushup(Node &U, Node &L, Node &R)
{
    U.sum = L.sum + R.sum;
    U.lmax = max(L.lmax, L.sum + R.lmax);
    U.rmax = max(R.rmax, R.sum + L.rmax);
    U.tmax = max(max(L.tmax, R.tmax), L.rmax + R.lmax);
}
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, a[l], a[l], a[l], a[l]}; return;}
    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 y)
{
    if (tr[u].l == x && tr[u].r == x)   {tr[u] = {x, x, y, y, y, y}; return;}
    int mid = tr[u].l + tr[u].r >> 1;
    if (x <= mid)   modify(u << 1, x, y);
    if (x > mid)    modify(u << 1 | 1, x, y);
    pushup(u);
}
Node query(int u, int l, int r)
{
    if(tr[u].l >= l && tr[u].r <= r) return tr[u];
    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
    {
        auto left = query(u << 1, l, r);
        auto right = query(u << 1 | 1, l, r);
        Node res;
        pushup(res, left, right);
        return res;
    }
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ )  scanf("%d", &a[i]);
    build(1, 1, n);
    while (m -- ) {
        int type, x, y;
        scanf("%d%d%d", &type, &x, &y);
        if (type == 2) 
            modify(1, x, y);
        else {
            if (x > y)  swap(x, y);
            printf("%d\n", query(1, x, y).tmax);
        }
    }
    return 0;
}
上一篇:#莫比乌斯反演,期望#CF1139D Steps to One


下一篇:高精度四件套(加减乘除)