55分求调,实在不知道哪里写挂了
查看原帖
55分求调,实在不知道哪里写挂了
1229738
CyansQwQ楼主2025/8/4 00:16
#include <bits/stdc++.h>
#define int long long

template <class Info, class Tag>

struct SGT {
#define lc rt << 1
#define rc rt << 1 | 1

    struct node {
        int l, r;
        Info info;
        Tag tag;
    };

    std::vector<node> tree;
    int n;

    SGT(const std::vector<Info> &num, const int &_n) : n(_n), tree(4 * _n + 5) {
        auto build = [&](auto &&self, int rt, int l, int r) -> void {
            tree[rt] = {l, r, num[l], Tag()};

            tree[rt].info.len = r - l + 1;

            if (l == r) {
                return;
            }

            int m = l + r >> 1;

            self(self, lc, l, m);
            self(self, rc, m + 1, r);

            tree[rt].info = tree[lc].info + tree[rc].info;
        };

        build(build, 1, 1, n);
    }

    void update(int rt, int l, int r, const Tag &k) {
        if (r < tree[rt].l || l > tree[rt].r) {
            return;
        }

        if (l <= tree[rt].l && tree[rt].r <= r) {
            keep(rt, k);
            return;
        }

        pushdown(rt);

        int m = tree[rt].l + tree[rt].r >> 1;

        if (l <= m) {
            update(lc, l, r, k);
        }
        if (r > m) {
            update(rc, l, r, k);
        }

        tree[rt].info = tree[lc].info + tree[rc].info;
    }

    Info query(int rt, int l, int r) {
        if (l <= tree[rt].l && tree[rt].r <= r) {
            return tree[rt].info;
        }

        pushdown(rt);

        int m = tree[rt].l + tree[rt].r >> 1;
        Info sum;

        if (l <= m) {
            sum = sum + query(lc, l, r);
        }
        if (r > m) {
            sum = sum + query(rc, l, r);
        }

        return sum;
    }

    void keep(int rt, const Tag &k) {
        if (k.lazy == 0) {
            tree[rt].info.pre = tree[rt].info.suf = tree[rt].info.mx = tree[rt].info.len;
            tree[rt].info.sum = 0;
        } else if (k.lazy == 1) {
            tree[rt].info.pre = tree[rt].info.suf = tree[rt].info.mx = 0;
            tree[rt].info.sum = tree[rt].info.len;
        }
        tree[rt].tag = k;
    }

    void pushdown(int rt) {
        if (tree[rt].tag.lazy == 1) {
            keep(lc, Tag(1));
            keep(rc, Tag(1));
        } else if (tree[rt].tag.lazy == 0) {
            keep(lc, Tag(0));
            keep(rc, Tag(0));
        }
        tree[rt].tag = Tag();
    }
};

struct Tag {
    int lazy;
    Tag() {
        lazy = -1;
    }
    Tag(int _lazy) {
        lazy = _lazy;
    }
};

struct Info {
    int sum, pre, suf, len, mx;
    Info() {
        sum = pre = suf = len = mx = 0;
    }
    Info(int _x) {
        sum = pre = suf = len = mx = _x;
    }
};

Info operator+(const Info &a, const Info &b) {
    Info c;
    c.len = a.len + b.len;
    c.sum = a.sum + b.sum;
    if (a.sum) {
        c.pre = a.pre;
    } else {
        c.pre = a.len + b.pre;
    }
    if (b.sum) {
        c.suf = b.suf;
    } else {
        c.suf = b.len + a.suf;
    }
    c.mx = std::max({c.pre, c.suf, a.suf + b.pre});
    return c;
};

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<Info> v(n + 1);
    for (int i = 1; i <= n; i++) {
        v[i] = Info();
        v[i].sum = v[i].len = 1;
    }

    SGT<Info, Tag> sgt(v, n);

    for (int i = 1; i <= m; i++) {
        int ops;
        std::cin >> ops;

        if (ops == 0) {
            int l, r;
            std::cin >> l >> r;
            sgt.update(1, l, r, Tag(0));
        }

        if (ops == 1) {
            int l0, r0, l1, r1;
            std::cin >> l0 >> r0 >> l1 >> r1;

            Info tmp = sgt.query(1, l0, r0);

            if (tmp.sum == 0) {
                continue;
            }

            sgt.update(1, l0, r0, Tag(0));

            int l = l1 - 1, r = r1 + 1;
            while (l + 1 < r) {
                int mid = (l + r) >> 1;

                Info tmp2 = sgt.query(1, l1, mid);
                int cnt = tmp2.len - tmp2.sum;

                if (cnt <= tmp.sum) {
                    l = mid;
                } else {
                    r = mid;
                }
            }

            sgt.update(1, l1, l, Tag(1));
        }

        if (ops == 2) {
            int l, r;
            std::cin >> l >> r;
            std::cout << sgt.query(1, l, r).mx << '\n';
        }
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);

    int t = 1;

    while (t--) {
        solve();
    }

    return 0;
}
2025/8/4 00:16
加载中...