代码RE20分,求调

P1253 扶苏的问题

ZHANGyutao123 @ 2023-12-04 19:55:08

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 9000004;
const ll inf = -LLONG_MAX;
int n, m;
ll tree[N << 2], add[N << 2], cov[N << 2], a[N];
inline int ls(int p) { return p << 1; }
inline int rs(int p) { return p << 1 | 1; }
inline void push_up(int p) { tree[p] = max(tree[ls(p)], tree[rs(p)]); }
inline void build(int l, int r, int p)
{
    cov[p] = inf;
    if (l == r)
    {
        tree[p] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, ls(p));
    build(mid + 1, r, rs(p));
    push_up(p);
}
inline void covdown(int p)
{
    if (cov[p] != inf)
    {
        tree[ls(p)] = tree[rs(p)] = cov[p];
        add[ls(p)] = add[rs(p)] = 0;
        cov[ls(p)] = cov[rs(p)] = cov[p]; //
        cov[p] = inf;
    }
}
inline void adddown(int p)
{
    if (add[p])
    {
        tree[ls(p)] += add[p];
        tree[rs(p)] += add[p];
        add[ls(p)] += add[p];
        add[rs(p)] += add[p];
        add[p] = 0;
    }
}
inline void push_down(int p)
{
    covdown(p);
    adddown(p);
}
inline void cover(int l, int r, int pl, int pr, int p, ll k)
{
    if (l <= pl && pr <= r)
    {
        tree[p] = k;
        cov[p] = k;
        add[p] = 0;
        return;
    }
    push_down(p);
    int mid = (pl + pr) >> 1;
    if (l <= mid)
        cover(l, r, pl, mid, ls(p), k);
    if (r > mid)
        cover(l, r, mid + 1, r, rs(p), k);
    push_up(p);
}
inline void addsum(int l, int r, int pl, int pr, int p, ll k)
{
    if (l <= pl && pr <= r)
    {
        covdown(p);
        add[p] += k;
        tree[p] += k;
        return;
    }
    push_down(p);
    int mid = (pl + pr) >> 1;
    if (l <= mid)
        addsum(l, r, pl, mid, ls(p), k);
    if (r > mid)
        addsum(l, r, mid + 1, r, rs(p), k);
    push_up(p);
}
inline ll sum(int l, int r, int pl, int pr, int p)
{
    if (l <= pl && pr <= r)
        return tree[p];
    push_down(p);
    int mid = (pl + pr) >> 1;
    ll ans = inf;
    if (l <= mid)
        ans = max(ans, sum(l, r, pl, mid, ls(p)));
    if (r > mid)
        ans = max(ans, sum(l, r, mid + 1, pr, rs(p)));
    return ans;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    build(1, n, 1);
    while (m--)
    {
        int op, x, y;
        ll k;
        scanf("%d", &op);
        if (op == 1)
        {
            scanf("%d%d%lld", &x, &y, &k);
            cover(x, y, 1, n, 1, k);
        }
        else if (op == 2)
        {
            scanf("%d%d%lld", &x, &y, &k);
            addsum(x, y, 1, n, 1, k);
        }
        else
        {
            scanf("%d%d", &x, &y);
            printf("%lld\n", sum(x, y, 1, n, 1));
        }
    }
    return 0;
}

|