萌新线段树60pts 后面4个WA QWQ

P1253 扶苏的问题

Flwben @ 2024-08-11 21:52:57

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e6 + 5, M = 1e7 + 5, INF = -1e18;
long long n, q, lazy[M], L[M], R[M], a[N], t[M], lazy2[M];
void build(int k, int l, int r) {
    L[k] = l; R[k] = r;
    if (l == r) {
        t[k] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(k * 2, l, mid);
    build(k * 2 + 1, mid + 1, r);
    t[k] = max(t[k * 2], t[k * 2 + 1]);
}
void lazytag2(long long k) {
    if (lazy2[k] != INF) {
        lazy[k * 2] = lazy[k * 2 + 1] = lazy[k] = 0;
        int l = k * 2, r = k * 2 + 1;
        lazy2[l] = lazy2[k];
        lazy2[r] = lazy2[k];
        t[l] = lazy2[k];
        t[r] = lazy2[k];
        lazy2[k] = INF;
    }
}
void lazytag(long long k) {
    if (lazy[k]) {
        lazytag2(k);
        int l = k * 2, r = k * 2 + 1;
        lazy[l] += lazy[k];
        lazy[r] += lazy[k];
        t[l] += lazy[k];
        t[r] += lazy[k];
        lazy[k] = 0;
    }
}
void change(long long k, long long l, long long r, long long x, long long y, long long p) {
    if (l == x && r == y) {
        t[k] = p;
        lazy2[k * 2] = p; 
        lazy2[k * 2 + 1] = p; 
        lazy[k * 2] = lazy[k * 2 + 1] = lazy[k] = 0;
        t[k * 2] = t[k * 2 + 1] = p;
        return;
    }
    lazytag2(k);
    lazytag(k);

    int mid = (l + r) >> 1;
    if (x <= mid && y <= mid) {
        change(k * 2, l, mid, x, y, p);
    } else if (x > mid && y > mid) {
        change(k * 2 + 1, mid + 1, r, x, y, p);
    } else {
        change(k * 2, l, mid, x, mid, p);
        change(k * 2 + 1, mid + 1, r, mid + 1, y, p);
    }
    t[k] = max(t[k * 2], t[k * 2 + 1]);
}
void add(int k, int l, int r, int x, int y, long long p) {
    if (l == x && r == y) {
        t[k] += p;
        lazy[k * 2] += p; 
        lazy[k * 2 + 1] += p;
        t[k * 2] += p;
        t[k * 2 + 1] += p;
        return;
    }
    lazytag2(k);
    lazytag(k);
    int mid = (l + r) >> 1;
    if (x <= mid && y <= mid) {
        add(k * 2, l, mid, x, y, p);
    } else if (x > mid && y > mid) {
        add(k * 2 + 1, mid + 1, r, x, y, p);
    } else {
        add(k * 2, l, mid, x, mid, p);
        add(k * 2 + 1, mid + 1, r, mid + 1, y, p);
    }
    t[k] = max(t[k * 2], t[k * 2 + 1]);
}
long long query(int k, int l, int r, int x, int y) {
    if (l == x && r == y) {
        return t[k];
    }
    lazytag2(k);
    lazytag(k);
    int mid = (l + r) >> 1;
    if (x <= mid && y <= mid) {
        return query(k * 2, l, mid, x, y);
    } else if (x > mid && y > mid) {
        return query(k * 2 + 1, mid + 1, r, x, y);
    } else {
        return max(query(k * 2, l, mid, x, mid), query(k * 2 + 1, mid + 1, r, mid + 1, y));
    }
}
signed main() {

//  freopen("P1253_7.in", "r", stdin);
//  freopen("P1253_71.out", "w", stdout);
    for (int i = 0; i < M; i++) {
        lazy2[i] = INF;
    }
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
    }
    build(1, 1, n);
    while(q--) {
        long long op, l, r, x;
        scanf("%lld%lld%lld", &op, &l, &r);
        if (op == 1) {
            scanf("%lld", &x);
            change(1, 1, n, l, r, x);
        } else if (op == 2) {
            scanf("%lld", &x);
            add(1, 1, n, l, r, x);
        } else {
            printf("%lld\n", query(1, 1, n, l, r));
        }
    }

    return 0;
}

调了很久的下传顺序... 没调好


|