萌新求助9分WA

P4513 小白逛公园

andyli @ 2019-07-26 03:02:19

不要回复qndmx之类的

#include <algorithm>
#include <cstdio>
#include <iostream>
#define lc (o << 1)
#define rc (o << 1 | 1)
using namespace std;
const int maxn = 500005;

struct Node {
    int l, r, lmax, rmax, tmax, sum;
    void update(int x) { sum = lmax = rmax = tmax = x; }
} tree[maxn << 4];

void maintain(int o)
{
    tree[o].sum = tree[lc].sum + tree[rc].sum;
    tree[o].lmax = max(tree[lc].lmax, tree[rc].lmax + tree[lc].sum);
    tree[o].rmax = max(tree[lc].rmax + tree[rc].sum, tree[rc].rmax);
    tree[o].tmax =
        max(max(tree[lc].tmax, tree[rc].tmax), tree[lc].rmax + tree[rc].lmax);
}

void build(int o, int l, int r)
{
    tree[o].l = l, tree[o].r = r;
    if (l == r) {
        int x;
        scanf("%d", &x);
        tree[o].update(x);
        return;
    }
    int m = (l + r) >> 1;
    build(lc, l, m);
    build(rc, m + 1, r);
    maintain(o);
}

void update(int o, int p, int x)
{
    int l = tree[o].l, r = tree[o].r;
    if (l == r) {
        tree[o].update(x);
        return;
    }
    int m = (l + r) >> 1;
    if (p <= m)
        update(lc, p, x);
    else
        update(rc, p, x);
    maintain(o);
}

Node query(int o, int x, int y)
{
    int l = tree[o].l, r = tree[o].r;
    if (l <= x && r >= y)
        return tree[o];
    int m = (l + r) >> 1;
    if (x <= m)
        return query(lc, x, y);
    if (y > m)
        return query(rc, x, y);
    Node ans, lans = query(lc, x, y), rans = query(rc, x, y);
    ans.lmax = max(lans.lmax, rans.lmax + lans.sum);
    ans.rmax = max(lans.rmax + rans.sum, rans.rmax);
    ans.tmax = max(max(lans.tmax, rans.tmax), lans.rmax + rans.lmax);
    return ans;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    build(1, 1, n);
    while (m--) {
        int opt, x, y;
        scanf("%d%d%d", &opt, &x, &y);
        if (opt == 1) {
            if (x > y)
                swap(x, y);
            printf("%d\n", query(1, x, y).tmax);
        } else
            update(1, x, y);
    }
    return 0;
}

by mcyqwq @ 2019-07-26 07:29:06

STO萌新竟然做线段树维护最大子段和QAQ,我也不会,逃了


by Eason_AC @ 2019-07-26 07:34:20

三倍经验?还有【SP1716】GSS3和4680(就是那个臭名昭著的帖子所在的题)好像也是用线段树维护最大子段和呢)。


by Smallbasic @ 2019-07-26 07:47:37

#define rc ((o << 1) + 1)

试试


by andyli @ 2019-07-26 12:52:23

@CZK QAQ


by andyli @ 2019-07-26 12:59:07

@Smallbasic 这两种写法是等价的吧


by Cesare @ 2019-07-26 12:59:40

@andyli 这俩没区别的,你左移一位之后最后一位肯定是 0 。


by andyli @ 2019-07-26 13:22:33

@Cesare 是这样的


by By_Ha @ 2019-08-03 11:03:44

15 5
121143 86087 -3033 137553 28294 -58514 -31727 217687 -138511 10662 -158172 -220525 -94372 95792 37276 2 9 38773
2 15 -151816
1 3 8
2 12 -66324
2 10 74935

by By_Ha @ 2019-08-03 11:04:26

@andyli 我才不承认我也写了个对拍才调出来


|