50pts 球调

P1253 扶苏的问题

sstitch @ 2024-04-01 15:54:10

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000010, inf = 2e9;
struct node {
    int u, l, r, mx, lzy1, lzy2;
} tr[N * 4];
int a[N];
void pushup(int u) {
    tr[u].mx = max(tr[u << 1].mx, tr[u << 1 | 1].mx);
}
void cvdown(int u) {
    if(tr[u].lzy2 != inf) {
        tr[u].lzy1 = 0;
        tr[u << 1].lzy1 = tr[u << 1 | 1].lzy1 = 0;
        tr[u << 1].mx = tr[u << 1 | 1].mx = tr[u].lzy2;
        tr[u << 1].lzy2 = tr[u << 1 | 1].lzy2 = tr[u].lzy2;
        tr[u].lzy2 = inf;
    }
}
void addown(int u) {
    if(tr[u].lzy1) {
        tr[u << 1].mx += tr[u].lzy1, tr[u << 1 | 1].mx += tr[u].lzy1;
        tr[u << 1].lzy1 += tr[u].lzy1, tr[u << 1 | 1].lzy1 += tr[u].lzy1;
        tr[u].lzy1 = 0;
    }
}
void pushdown(int u) {
    cvdown(u); addown(u);
}
void build(int u, int l, int r) {
    tr[u].l = l, tr[u].r = r; tr[u].lzy2 = inf;
    if(l == r) {
        tr[u].mx = a[l];
        return ;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}
void Add(int u, int l, int r, int L, int R, int v) {
    if(L <= l && R >= r) {
        cvdown(u);
        tr[u].mx += v;
        tr[u].lzy1 += v;
        return ;
    }
    pushdown(u);
    int mid = l + r >> 1;
    if(L <= mid) Add(u << 1, l, mid, L, R, v);
    if(mid < R) Add(u << 1 | 1, mid + 1, r, L, R, v);
    pushup(u);
}
void cover(int u, int l, int r, int L, int R, int v) {
    if(L <= l && R >= r) {
        tr[u].lzy1 = 0;
        tr[u].mx = v;
        tr[u].lzy2 = v;
        return ;
    }
    pushdown(u);
    int mid = l + r >> 1;
    if(L <= mid) cover(u << 1, l, mid, L, R, v);
    if(mid < R) cover(u << 1 | 1, mid + 1, r, L, R, v);
    pushup(u);
}
int ask(int u, int l, int r, int L, int R) {
    if(L <= l && R >= r) {
        return tr[u].mx;
    }
    pushdown(u);
    int mid = l + r >> 1;
    int ans = -inf;
    if(L <= mid) ans = max(ans, ask(u << 1, l, mid, L, R));
    if(mid < R) ans = max(ans, ask(u << 1 | 1, mid + 1, r, L, R));
    return ans;
}
signed main() {
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while(m--) {
        int o, l, r;
        cin >> o >> l >> r;
        if(o == 2) {
            int k; cin >> k;
            Add(1, 1, n, l, r, k);
        }
        else if(o == 1) {
            int k; cin >> k;
            cover(1, 1, n, l, r, k);
        }
        else {
            cout << ask(1, 1, n, l, r) << '\n';
        }
    }
    return 0;
}

WA 了(


|