萌新刚学线段树,爆零了,求助!qwq

P2572 [SCOI2010] 序列操作

Kevin_Zhen @ 2021-06-28 17:06:04

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ls (k << 1)
#define rs ((k << 1) | 1)
#define mid ((l + r) >> 1)
using namespace std;

const int maxn = 1e5 + 10;

struct Node {
    int sum, tag, rev, l1, r1, l0, r0, d1, d0; int L, R;
    void clear() { tag = -1; sum = rev = l1 = r1 = l0 = r0 = d1 = d0 = L = R = 0; }
} t[maxn << 2];

int n, m, a[maxn];

inline void pushup(int k, int l, int r) {
    t[k].sum = t[ls].sum + t[rs].sum;
    t[k].l1 = t[ls].l1; if (t[ls].l1 == (mid - l + 1)) t[k].l1 = (mid - l + 1) + t[rs].l1;
    t[k].l0 = t[ls].l0; if (t[ls].l0 == (mid - l + 1)) t[k].l0 = (mid - l + 1) + t[rs].l0;
    t[k].r1 = t[rs].r1; if (t[rs].r1 == (r - mid)) t[k].r1 = (r - mid) + t[ls].r1;
    t[k].r0 = t[rs].r0; if (t[rs].r0 == (r - mid)) t[k].r0 = (r - mid) + t[ls].r0;
    t[k].d1 = max(t[ls].r1 + t[rs].l1, max(t[ls].d1, t[rs].d1));
    t[k].d0 = max(t[ls].r0 + t[rs].l0, max(t[ls].d0, t[rs].d0));
}

inline void pushdown(int k, int l, int r) {
    if (~t[k].tag) {
        int v = t[k].tag; t[k].tag = -1;
        t[ls].rev = t[rs].rev = 0;
        t[ls].tag = t[rs].tag = v;
        t[ls].sum = (mid - l + 1) * v; t[rs].sum = (r - mid) * v;
        t[ls].l1 = t[ls].r1 = t[ls].d1 = (mid - l + 1) * v;
        t[ls].l0 = t[ls].r0 = t[ls].d0 = (mid - l + 1) - (mid - l + 1) * v;
        t[rs].l1 = t[rs].r1 = t[rs].d1 = (r - mid) * v;
        t[rs].l0 = t[rs].r0 = t[rs].d0 = (r - mid) - (r - mid) * v;
    }
    if (t[k].rev) {
        t[k].rev = 0;
        if (~t[ls].tag) t[ls].tag ^= 1; else t[ls].rev = 1;
        if (~t[rs].tag) t[rs].tag ^= 1; else t[rs].rev = 1;
        t[ls].sum = (mid - l + 1) - t[ls].sum; t[rs].sum = (r - mid) - t[rs].sum;
        swap(t[ls].l1, t[ls].l0); swap(t[rs].l1, t[rs].l0);
        swap(t[ls].r1, t[ls].r0); swap(t[rs].r1, t[rs].r0);
        swap(t[ls].d1, t[ls].d0); swap(t[rs].d1, t[rs].d0);
    }
}

void build(int k, int l, int r) {
    t[k].clear(); t[k].L = l; t[k].R = r;
    if (l == r) {
        t[k].sum = t[k].l1 = t[k].r1 = t[k].d1 = a[l];
        t[k].l0 = t[k].r0 = t[k].d0 = a[l] ^ 1;
        return;
    }
    build(ls, l, mid); build(rs, mid + 1, r);
    pushup(k, l, r);
}

void modify(int k, int l, int r, int x, int y, int val) {
    if (x <= l && r <= y) {
        t[k].sum = t[k].l1 = t[k].r1 = t[k].d1 = (r - l + 1) * val;
        t[k].l0 = t[k].r0 = t[k].d0 = (r - l + 1) - (r - l + 1) * val;
        t[k].tag = val; t[k].rev = 0;
        return;
    }
    pushdown(k, l, r);
    if (x <= mid) modify(ls, l, mid, x, y, val);
    if (mid < y) modify(rs, mid + 1, r, x, y, val);
    pushup(k, l, r);
}

void rev(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y) {
        if (~t[k].tag) t[k].tag ^= 1; else t[k].rev = 1;
        t[k].sum = (r - l + 1) - t[k].sum;
        swap(t[k].l1, t[k].l0);
        swap(t[k].r1, t[k].r0);
        swap(t[k].d1, t[k].d0);
        return;
    }
    pushdown(k, l, r);
    if (x <= mid) rev(ls, l, mid, x, y);
    if (mid < y) rev(rs, mid + 1, r, x, y);
    pushup(k, l, r);
}

Node query(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y) return t[k];
    pushdown(k, l, r);
    Node res, a, b; res.clear(), a.clear(), b.clear();
    if (x <= mid) a = query(ls, l, mid, x, y);
    if (mid < y) b = query(rs, mid + 1, r, x, y);
    if (mid >= y) return a;
    if (x > mid) return b;
    res.L = a.L; res.R = b.R;
    res.sum = a.sum + b.sum;
    res.l1 = a.l1; if (a.l1 == (a.R - a.L + 1)) res.l1 = (a.R - a.L + 1) + b.l1;
    res.r1 = b.r1; if (b.r1 == (b.R - b.L + 1)) res.r1 = (b.R - b.L + 1) + a.r1;
    res.d1 = max(a.r1 + b.l1, max(a.d1, b.d1));
    return res;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    build(1, 1, n);
    while (m--) {
        int opt, x, y; scanf("%d%d%d", &opt, &x, &y); ++x, ++y;
        if (!opt) modify(1, 1, n, x, y, 0);
        else if (opt == 1) modify(1, 1, n, x, y, 1);
        else if (opt == 2) rev(1, 1, n, x, y);
        else if (opt == 3) printf("%d\n", query(1, 1, n, x, y).sum);
        else if (opt == 4) printf("%d\n", query(1, 1, n, x, y).d1);

        /*for (int i = 1; i <= n; ++i) printf("%d ", query(1, 1, n, i, i).d1);
        putchar('\n'); system("pause");*/

    }
    return 0;
}

by Gokix @ 2021-10-10 09:59:11

你好强啊

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCOrz


|