10ptsWA求调

P1253 扶苏的问题

Dry_ice @ 2022-08-23 11:33:28

RT。

#include <stdio.h>
template<typename Tp>
inline void Read(Tp &x) {
    x = 0; bool w = 0; char c = getchar();
    for (; c < '0' || c > '9'; c = getchar()) if (c == '-') w ^= 1;
    for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
    if (w) x = -x;
}
#define int long long
const int N = (int)1e6 + 5, inf = (int)1e18;
int n, Q, a[N];
struct Seg {
    int val, lz_cov, lz_sum;
} sg[N << 2];
inline int mx(int p, int q) {
    return p > q ? p : q;
}
inline void Pushup(int id) {
    sg[id].val = mx(sg[id << 1].val, sg[id << 1 | 1].val);
}
inline void Build(int id, int l, int r) {
    if (l == r) {sg[id] = (Seg){a[l], 0, 0}; return;}
    int mid = l + r >> 1;
    Build(id << 1, l, mid); Build(id << 1 | 1, mid + 1, r);
    Pushup(id);
}
inline void Pushdown(int id) {
    if (sg[id].lz_cov) {
        sg[id << 1].lz_sum = sg[id << 1 | 1].lz_sum = 0;
        sg[id << 1].lz_cov = sg[id << 1 | 1].lz_cov = sg[id].lz_cov;
        sg[id].val = sg[id].lz_cov;
        sg[id].lz_cov = 0;
    }
    if (sg[id].lz_sum) {
        sg[id << 1].lz_sum += sg[id].lz_sum, sg[id << 1 | 1].lz_sum += sg[id].lz_sum;
        sg[id].val += sg[id].lz_sum;
        sg[id].lz_sum = 0;
    }
}
inline void Update(int id, int l, int r, int &x, int &y, int opt, int key) {
    if (x <= l && r <= y) {
        if (opt ^ 2) sg[id].lz_sum = 0, sg[id].lz_cov = key, sg[id].val = key;
        else sg[id].lz_sum += key, sg[id].val += key;
        return;
    }
    Pushdown(id);
    int mid = l + r >> 1;
    if (x <= mid) Update(id << 1, l, mid, x, y, opt, key);
    if (y > mid) Update(id << 1 | 1, mid + 1, r, x, y, opt, key);
    Pushup(id);
}
inline int Query(int id, int l, int r, int &x, int &y) {
    if (x <= l  && r <= y) return sg[id].val;
    Pushdown(id);
    int mid = l + r >> 1, res = -inf;
    if (x <= mid) res = mx(Query(id << 1, l, mid, x, y), res);
    if (y > mid) res = mx(Query(id << 1 | 1, mid + 1, r, x, y), res);
    return res;
}
signed main(void) {
    Read(n), Read(Q);
    for (int i = 1; i <= n; ++i) Read(a[i]); Build(1, 1, n);
    for (int opt, l, r, x; Q--; ) {
        Read(opt); Read(l), Read(r);
        switch (opt) {
            case 1: Read(x); Update(1, 1, n, l, r, 1, x); break;
            case 2: Read(x); Update(1, 1, n, l, r, 2, x); break;
            default: printf("%lld\n", Query(1, 1, n, l, r));
        }
    }
    return 0;
}

by Miraik @ 2022-08-23 12:23:57

@Dry_ice pushdown有一个非常显然的问题。


by Dry_ice @ 2022-08-23 13:57:12

@SweetOrangeOvO 显然得很(bushi


by Miraik @ 2022-08-23 14:20:14

@Dry_ice 你确定改的是 kval


by Miraik @ 2022-08-23 14:22:13

@Dry_ice 改完 90pts,而且最后一个点几乎超时,你肯定还有哪里写炸了


|