线段树 9pts 求助

P4513 小白逛公园

Annie07 @ 2023-08-11 17:34:51

考虑交换了

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 5e5 + 5;
ll n, m;

ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

struct node {
    ll sum, val, lv, rv;
    node() { }
    node(ll _sum, ll _val, ll _lv, ll _rv) {
        sum = _sum, val = _val, lv = _lv, rv = _rv;
    }
}f[N * 4];

void pushup(ll k, ll l, ll r) {
    f[k].sum = f[k * 2].sum + f[k * 2 + 1].sum;
    f[k].lv = max(f[k * 2].lv, f[k * 2].sum + f[k * 2 + 1].lv);
    f[k].rv = max(f[k * 2 + 1].sum + f[k * 2].rv, f[k * 2 + 1].rv);
    f[k].val = max(max(f[k * 2].sum, f[k * 2 + 1].sum)
    , f[k * 2 + 1].lv + f[k * 2].rv);
}
ll a;
void build(ll k, ll l, ll r) {
    if (l == r) {
        a = read();
        f[k].lv = f[k].rv = f[k].sum = f[k].val = a;
        return;
    }
    ll mid = (l + r) >> 1;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
    pushup(k, l, r);
}

void update(ll k, ll l, ll r, ll x, ll s) {
    if (l == r && l == x) {
        f[k].lv = f[k].rv = f[k].sum = f[k].val = s;
        return;
    }
    ll mid = (l + r) >> 1;
    if (x <= mid)   update(k * 2, l, mid, x, s);
    else update(k * 2 + 1, mid + 1, r, x, s);
    pushup(k, l, r);
}
node ask(ll k, ll l, ll r, ll x, ll y) {
    if (x <= l && r <= y) return f[k];
    ll mid = (l + r) >> 1;
    node u, v;

    if (x <= mid) u = ask(k * 2, l, mid, x, y);
    if (y > mid) v = ask(k * 2 + 1, mid + 1, r, x, y);
    if (x <= mid) {
        return u;
    } else if (y > mid) {
        return v;
    } else {
        ll sum = u.sum + v.sum;
        ll lv = max(u.lv, u.sum + v.lv);
        ll rv = max(v.sum + u.rv, v.rv);
        ll val = max(max(u.sum, v.sum)
        , v.lv + u.rv);
        return node(sum, val, lv, rv);  
    }   
}

int main() {
    //freopen("P4513_2.in", "r", stdin);
    //freopen("P4513.out", "w", stdout);
    n = read(), m = read();
    build(1, 1, n);
    ll k, x, y, p, s;
    while(m--) {
        k = read();
        if (k == 1) {
            x = read(), y = read();
            if (x > y) swap(x, y);
            printf("%lld\n", ask(1, 1, n, x, y).val);   
        } else if (k == 2) {
            p = read(), s = read();
            update(1, 1, n, p, s);
        }
    }
    return 0;
}

谢谢!


by Annie07 @ 2023-08-11 17:35:59

第二个测试点数据

50 100
773
760
-578
-302
-664
272
367
352
891
-569
429
-208
-325
38
148
456
-960
-390
470
271
763
-458
-52
647
-205
-514
399
-611
882
665
257
-718
233
-756
237
-301
650
148
-894
-212
-820
-341
-240
-620
320
932
-498
-252
323
-428
2 5 818
1 35 49
2 15 -87
1 31 48
1 44 37
1 10 25
2 28 -761
2 38 913
1 28 30
1 25 38
2 26 996
1 35 22
2 27 59
2 49 -754
2 7 -366
2 3 -822
1 22 3
2 12 516
1 7 30
2 5 -693
2 22 -193
2 33 -474
1 1 3
1 37 35
2 39 829
2 28 865
1 37 20
1 38 42
1 17 5
1 38 5
2 41 -553
1 18 9
1 18 16
2 48 -20
2 10 -875
1 20 13
1 38 1
1 15 44
2 36 -247
1 11 32
2 18 -703
2 47 -24
1 39 15
1 9 2
1 7 32
1 36 6
2 19 253
2 40 -624
1 12 45
1 25 37
1 4 3
1 40 27
1 28 45
2 6 -851
2 10 410
1 31 48
2 25 -307
2 27 -704
1 25 7
1 35 12
1 17 24
1 29 21
1 15 5
2 18 -70
2 17 568
1 12 45
2 13 -276
2 21 -862
2 47 -513
2 24 -700
1 10 42
1 26 15
2 27 238
2 39 -264
1 10 8
2 8 -5
1 33 34
2 23 665
2 38 -646
1 8 35
2 1 -834
2 2 -908
2 3 -702
2 4 -994
2 5 -883
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
1 2 1
1 3 2
1 4 3
1 5 4
1 3 1
1 4 2
1 5 3
1 4 1
1 5 2
1 5 1

|