萌新线段树 55pts 求助
查看原帖
萌新线段树 55pts 求助
610557
shinzanmonoszm 妹妹楼主2023/1/5 21:40
#include<iostream>
#include<algorithm>
const int sz = 2e5 + 10;
struct ST {
    struct node {
        int len, lens, lenl, lenr, num1, num0;
        node operator+(const node &a) const {
            return node {
                len + a.len,
                std::max(lenr + a.lenl, std::max(lens, a.lens)),
                lenl + a.lenl * (lenl == len),
                lenr * (a.lenr == a.len) + a.lenr,
                num1 + a.num1,
                num0 + a.num0
            };
        }
    } tree[sz << 2];
    int cov[sz << 2];
    bool iscov[sz << 2];
    void operation(int p, int val) {
        if (val) tree[p].lens = tree[p].lenl = tree[p].lenr = 0, tree[p].num1 = tree[p].len;
        else tree[p].lens = tree[p].lenl = tree[p].lenr = tree[p].num0 = tree[p].len, tree[p].num1 = 0;
        iscov[p] = true, cov[p] = val;
    }
    void pushdown(int p) {
        if (iscov[p]) {
            operation(p << 1, cov[p]);
            operation(p << 1 | 1, cov[p]);
            iscov[p] = false;
        }
    }
    void build(int p, int ln, int rn) {
        if (ln == rn) return tree[p] = {1, 0, 0, 0, 1, 0}, void();
        int mid = ln + rn >> 1;
        build(p << 1, ln, mid);
        build(p << 1 | 1, mid + 1, rn);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    void assign(int p, int ln, int rn, int l, int r, int val) {
        if (ln >= l && rn <= r) return operation(p, val);
        if (ln > r || rn < l) return;
        int mid = ln + rn >> 1;
        pushdown(p);
        assign(p << 1, ln, mid, l, r, val);
        assign(p << 1 | 1, mid + 1, rn, l, r, val);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    node query(int p, int ln, int rn, int l, int r) {
        if (ln >= l && rn <= r) return tree[p];
        if (ln > r || rn < l) return node{0, 0, 0, 0, 0, 0};
        int mid = ln + rn >> 1;
        node res = node{0, 0, 0, 0, 0, 0};
        pushdown(p);
        res = res + query(p << 1, ln, mid, l, r);
        res = res + query(p << 1 | 1, mid + 1, rn, l, r);
        return res;
    }
} st;
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m;
    std::cin >> n >> m;
    st.build(1, 1, n);
    while (m--) {
        int op, l, r, il, ir;
        std::cin >> op >> l >> r;
        if (op == 0) st.assign(1, 1, n, l, r, 0);
        if (op == 2) std::cout << st.query(1, 1, n, l, r).lens << "\n";
        if (op == 1) {
            std::cin >> il >> ir;
            int num1 = st.query(1, 1, n, l, r).num1;
            if (num1 == 0) continue;
            st.assign(1, 1, n, l, r, 0);
            if (st.query(1, 1, n, il, ir).num0 <= num1) {
                st.assign(1, 1, n, il, ir, 1);
                continue;
            }
            int bl = il, br = ir;
            while (bl <= br) {
                int mid = bl + br >> 1;
                if (st.query(1, 1, n, il, mid).num0 <= num1) bl = mid + 1;
                else br = mid - 1;
            }
            st.assign(1, 1, n, il, br, 1);
        }
    }
    return 0;
}

2023/1/5 21:40
加载中...