9pts 求助

P4513 小白逛公园

Nygglatho @ 2023-01-30 20:39:39

如题,下载了数据发现我的输出总是莫名其妙变成 0

#include <bits/stdc++.h>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ul unsigned long long

const int M = 500000;

using namespace std;

struct node {
    int sum, mx, mxl, mxr;
    //区间和,区间最大值,区间最大前缀,最大后缀 
}d[M * 4];

int a[M * 4];

//调试函数,不用管
void Chesed(int p) {
    printf ("%d: %d %d %d %d\n", p, d[p].sum, d[p].mx, d[p].mxl, d[p].mxr);
    if (d[p * 2].mx || d[p * 2].mxl || d[p * 2].mxr || d[p * 2].sum) Chesed(p * 2); 
    if (d[p * 2 + 1].mx || d[p * 2 + 1].mxl || d[p * 2 + 1].mxr || d[p * 2 + 1].sum) Chesed(p * 2 + 1); 
}

void build(int p, int l, int r) {
    if (l == r) {
        d[p].sum = a[l];
        d[p].mx = a[l];
        d[p].mxl = a[l];
        d[p].mxr = a[l];
        return;
    }
    int m = (l + r) / 2;
    build(p * 2, l, m);
    build(p * 2 + 1, m + 1, r);
    d[p].sum = d[p * 2].sum + d[p * 2 + 1].sum;
    d[p].mx = max(d[p * 2].mx, max(d[p * 2 + 1].mx, d[p * 2 + 1].mxl + d[p * 2].mxr));
    d[p].mxl = max(d[p * 2].mxl, d[p * 2].sum + d[p * 2 + 1].mxl);
    d[p].mxr = max(d[p * 2 + 1].mxr, d[p * 2 + 1].sum + d[p * 2].mxr);
} 

void add(int p, int l, int r, int x, int v) {
    //cout<<p<<' '<<l<<' '<<r<<endl;
    if (l == r) {
        d[p].mx = v;
        d[p].mxl = v;
        d[p].mxr = v;
        d[p].sum = v;
        return;
    }
    int m = (l + r) / 2;
    if (x <= m) add(p * 2, l, m, x, v);
    else add(p * 2 + 1, m + 1, r, x, v);
    d[p].sum = d[p * 2].sum + d[p * 2 + 1].sum;
    d[p].mx = max(d[p * 2].mx, max(d[p * 2 + 1].mx, d[p * 2].mxr + d[p * 2 + 1].mxl));
    d[p].mxl = max(d[p * 2].mxl, d[p * 2].sum + d[p * 2 + 1].mxl);
    d[p].mxr = max(d[p * 2 + 1].mxr, d[p * 2 + 1].sum + d[p * 2].mxr);
}

node getsum(int p, int l, int r, int s, int t) {
    //cout<<p<<' '<<l<<' '<<r<<' '<<s<<' '<<t<<endl;
    if (l <= s && t <= r) {
        return d[p];
    }
    int m = (s + t) / 2;
    node x, y, F;
    if (l <= m) x = getsum(p * 2, l, r, s, m);
    if (m < r) y = getsum(p * 2 + 1, l, r, m + 1, t);
    if (l <= m && m < r) {
        F.sum = x.sum + y.sum;
        F.mx = max(x.mx, max(y.mx, x.mxr + y.mxl));
        F.mxl = max(x.mxl, x.sum + y.mxl);
        F.mxr = max(y.mxr, y.sum + x.mxr);
    } else if (l <= m) {
        F = x;
    } else {
        F = y;
    }
    return F;
}

int main() {
    //freopen ("P4513_2.in", "r", stdin);
    //freopen ("wa.out", "w", stdout);
    int n, t;
    scanf ("%d%d", &n, &t);
    for (int i = 1; i <= n; ++i) scanf ("%d", &a[i]);
    build(1, 1, n);
    for (int i = 1; i <= t; ++i) {
        int op, x, y;
        scanf ("%d%d%d", &op, &x, &y);
        if (op == 1) {
            printf ("%d\n", getsum(1, x, y, 1, n).mx);
        } else {
            add(1, 1, n, x, y);
        }
    }
}

by Leonid @ 2023-01-30 20:57:31

@SHM 测试数据可能会出现 a>b 的情况,需要进行交换;


by Nygglatho @ 2023-01-30 21:24:42

@h0494 thx,此贴结


by 喵仔牛奶 @ 2023-01-31 11:22:05

%%%%


|