60pts求助,WA三个TLE一个,样例过了

P1253 扶苏的问题

wcyQwQ @ 2022-02-02 19:19:12

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
const ll inf = 114514;
struct Node
{
    int l, r;
    ll mx;
    ll cover, add;
} t[N << 2];
int n, q;
int a[N];

inline void pushup(int p)
{
    t[p].mx = max(t[p << 1].mx, t[p << 1 | 1].mx);
}

inline void coverdown(int p)
{
    Node &u = t[p], &l = t[p << 1], &r = t[p << 1 | 1];
    if (u.cover == inf)
        return;

    l.add = 0;
    l.mx = u.cover;
    l.cover = u.cover;

    r.add = 0;
    r.mx = u.cover;
    r.cover = u.cover;
}

inline void adddown(int p)
{
    Node &u = t[p], &l = t[p << 1], &r = t[p << 1 | 1];
    if (!u.add)
        return;

    coverdown(p);

    l.add += u.add;
    l.mx += u.add;

    r.add += u.add;
    r.mx += u.add;

    u.add = 0;
} 

inline void pushdown(int p)
{
    coverdown(p);
    adddown(p);
}

inline void build(int p, int l, int r)
{
    t[p].l = l;
    t[p].r = r;                                                                                                                             
    t[p].cover = inf;
    if (l == r)
    {
        t[p].mx = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

inline void modify(int p, int l, int r, int x)
{
    if (l <= t[p].l && t[p].r <= r)
    {
        t[p].cover = x;
        t[p].add = 0;
        t[p].mx = x;
        return;
    }
    pushdown(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid)
        modify(p << 1, l, r, x);
    if (r > mid)
        modify(p << 1 | 1, l, r, x);
    pushup(p);
}

inline void add(int p, int l, int r, int x)
{
    if (l <= t[p].l && t[p].r <= r)
    {
        t[p].add += x;
        t[p].mx += x;
        return;
    }
    pushdown(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid)
        add(p << 1, l, r, x);
    if (r > mid)
        add(p << 1 | 1, l, r, x);
    pushup(p);
}

inline ll query(int p, int l, int r)
{
    if (l <= t[p].l && t[p].r <= r)
        return t[p].mx;
    pushdown(p);
    int mid = (t[p].l + t[p].r) >> 1;
    ll res = -1e18;
    if (l <= mid)
        res = max(res, query(p << 1, l, r));
    if (r > mid)
        res = max(res, query(p << 1 | 1, l, r));
    return res;
}

int main()
{
    // freopen("a.in", "r", stdin);
    // freopen("a.out", "w", stdout);
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    build(1, 1, n);
    while (q--)
    {
        int op;
        cin >> op;
        if (op == 1)
        {
            int l, r, x;
            cin >> l >> r >> x;
            modify(1, l, r, x);
        }
        else if (op == 2)
        {
            int l, r, x;
            cin >> l >> r >> x;
            add(1, l, r, x);
        }
        else if (op == 3)
        {
            int l, r;
            cin >> l >> r;
            cout << query(1, l, r) << endl;
        }
    }
    return 0;
}

by wcyQwQ @ 2022-02-03 09:13:27

90pts了,最后一个点TLE


by wcyQwQ @ 2022-02-03 09:25:58

加了快读过了,此贴终结


|