线段树 20pts WA 求调

P1253 扶苏的问题

FwbAway @ 2024-09-20 12:08:17

感觉 push\_down 的思路和题解的有些不一样,但是为什么挂了呢?

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1100000;
int n, q, a[N];
int id, l, r, x;
struct Tree {
    int l, r;
    int num;
    pair<int, int> lz;
} tree[N << 2];
inline void push_up(int u) {
    tree[u].num = max(tree[u << 1].num, tree[u << 1 | 1].num);
}
inline void push_down(int u) {
    if (tree[u].lz.first) {
        if (tree[u].lz.first == 1) {
            tree[u << 1].lz = make_pair(1, tree[u].lz.second);
            tree[u << 1 | 1].lz = make_pair(1, tree[u].lz.second);
        } else {
            tree[u << 1].lz = make_pair(tree[u << 1].lz.first, tree[u << 1].lz.second + tree[u].lz.second);
            tree[u << 1 | 1].lz = make_pair(tree[u << 1 | 1].lz.first, tree[u << 1 | 1].lz.second + tree[u].lz.second);
        }
        if (tree[u << 1].lz.first == 1) {
            tree[u << 1].num = tree[u << 1].lz.second;
        } else {
            tree[u << 1].num += tree[u].lz.second;
        }
        if (tree[u << 1 | 1].lz.first == 1) {
            tree[u << 1 | 1].num = tree[u << 1 | 1].lz.second;
        } else {
            tree[u << 1 | 1].num += tree[u].lz.second;
        }
        tree[u].lz = make_pair(0, 0);
    }
    return ;
}
inline void build(int u, int l, int r) {
    tree[u].l = l, tree[u].r = r;
    if (l == r) {
        tree[u].num = a[l];
        return ;
    }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    push_up(u);
}
inline void chg(int u, int l, int r, int val) {
    if (l <= tree[u].l && tree[u].r <= r) {
        tree[u].num = val;
        tree[u].lz = make_pair(1, val);
        return ;
    }
    push_down(u);
    if (l <= tree[u << 1].r) chg(u << 1, l, r, val);
    if (r >= tree[u << 1 | 1].l) chg(u << 1 | 1, l, r, val);
    push_up(u);
}
inline void add(int u, int l, int r, int val) {
    if (l <= tree[u].l && tree[u].r <= r) {
//      cout << u << ' ' << tree[u].l << ' ' << tree[u].r << ' ' << "yuan = " << tree[u].num << endl;
        tree[u].num += val;
        tree[u].lz = make_pair(2, val);
        return ;
    }
    push_down(u);
    if (l <= tree[u << 1].r) add(u << 1, l, r, val);
    if (r >= tree[u << 1 | 1].l) add(u << 1 | 1, l, r, val);
    push_up(u);
}
inline int search(int u, int l, int r) {
    if (l <= tree[u].l && tree[u].r <= r) {
        return tree[u].num;
    }
    push_down(u);
    int sum = -1e18;
    if (l <= tree[u << 1].r) sum = max(sum, search(u << 1, l, r));
    if (r >= tree[u << 1 | 1].l) sum = max(sum, search(u << 1 | 1, l, r));
    return sum;
}
signed main() {
    scanf("%lld%lld", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
    }
    build(1, 1, n);
    for (int i = 1; i <= q; i++) {
        scanf("%lld", &id);
        if (id == 1) {
            scanf("%lld%lld%lld", &l, &r, &x);
            chg(1, l, r, x);
        } else if (id == 2) {
            scanf("%lld%lld%lld", &l, &r, &x);
            add(1, l, r, x);
        } else {
            scanf("%lld%lld", &l, &r);
            printf("%lld\n", search(1, l, r));
        }
    }
    return 0;
}

by ZSYZSYZSYZSY @ 2024-11-22 16:24:26

666 orz


|