WA0pts求调,悬赏一关

P2572 [SCOI2010] 序列操作

Tjaweiof @ 2024-07-05 15:28:01

RT

#include <bits/stdc++.h>
using namespace std;
#define FILE(x) freopen(x".in", "r", stdin);freopen(x".out", "w", stdout);
const int N = 100000;
int n;
bool a[N + 1];
int t[N << 2], t2[N << 2][2], t3[N << 2][2], t4[N << 2][2], d[N << 2], d2[N << 2];
void add(int x, bool v, int L, int R){
    t[x] = v * (R - L + 1);
    t2[x][v] = R - L + 1;
    t2[x][v ^ 1] = 0;
    t3[x][v] = R - L + 1;
    t3[x][v ^ 1] = 0;
    t4[x][v] = R - L + 1;
    t4[x][v ^ 1] = 0;
    d[x] = v;
    return;
}
void add2(int x, int L, int R){
    t[x] = R - L + 1 - t[x];
    d2[x] ^= 1;
    swap(t2[x][0], t2[x][1]);
    swap(t3[x][0], t3[x][1]);
    swap(t4[x][0], t4[x][1]);
    return;
}
void pushup(int x, int L, int R){
    t[x] = t[x << 1] + t[x << 1 | 1];
    t2[x][0] = max(max(t2[x << 1][0], t2[x << 1 | 1][0]), t4[x << 1][0] + t3[x << 1 | 1][0]);
    t2[x][1] = max(max(t2[x << 1][1], t2[x << 1 | 1][1]), t4[x << 1][1] + t3[x << 1 | 1][1]);
    int mid = (L + R) >> 1;
    if (t3[x << 1][0] == R - mid){
        t3[x][0] = t3[x << 1][0] + t3[x << 1 | 1][0];
    } else {
        t3[x][0] = t3[x << 1][0];
    }
    if (t3[x << 1][1] == mid - L + 1){
        t3[x][1] = t3[x << 1][1] + t3[x << 1 | 1][1];
    } else {
        t3[x][1] = t3[x << 1][1];
    }
    if (t4[x << 1 | 1][0] == R - mid){
        t4[x][0] = t4[x << 1 | 1][0] + t4[x << 1][0];
    } else {
        t4[x][0] = t4[x << 1 | 1][0];
    }
    if (t4[x << 1 | 1][1] == R - mid){
        t4[x][1] = t4[x << 1 | 1][1] + t4[x << 1][1];
    } else {
        t4[x][1] = t4[x << 1 | 1][1];
    }
    return;
}
void pushdown(int x, int L, int R){
    int mid = (L + R) >> 1;
    if (d[x] != -1){
        d2[x] = 0;
        d[x << 1] = d[x];
        d[x << 1 | 1] = d[x];
        d2[x << 1] = 0;
        d2[x << 1 | 1] = 0;
        add(x << 1, d[x], L, mid);
        add(x << 1 | 1, d[x], mid + 1, R);
        t[x << 1] = d[x] * (mid - L + 1);
        t[x << 1 | 1] = d[x] * (R - mid);
        t2[x << 1][d[x]] = mid - L + 1;
        t2[x << 1][d[x] ^ 1] = 0;
        t2[x << 1 | 1][d[x]] = R - mid;
        t2[x << 1 | 1][d[x] ^ 1] = 0;
        t3[x << 1][d[x]] = mid - L + 1;
        t3[x << 1][d[x] ^ 1] = 0;
        t3[x << 1 | 1][d[x]] = R - mid;
        t3[x << 1 | 1][d[x] ^ 1] = 0;
        t4[x << 1][d[x]] = mid - L + 1;
        t4[x << 1][d[x] ^ 1] = 0;
        t4[x << 1 | 1][d[x]] = R - mid;
        t4[x << 1 | 1][d[x] ^ 1] = 0;
        d[x] = -1;
    }
    if (d2[x]){
        t[x << 1] = mid - L + 1 - t[x << 1];
        t[x << 1 | 1] = R - mid - t[x << 1 | 1];
        d2[x << 1] ^= 1;
        d2[x << 1 | 1] ^= 1;
        swap(t2[x << 1][0], t2[x << 1][1]);
        swap(t3[x << 1][0], t3[x << 1][1]);
        swap(t4[x << 1][0], t4[x << 1][1]);
        swap(t2[x << 1 | 1][0], t2[x << 1 | 1][1]);
        swap(t3[x << 1 | 1][0], t3[x << 1 | 1][1]);
        swap(t4[x << 1 | 1][0], t4[x << 1 | 1][1]);
        d2[x] = 0;
    }
    return;
}
void build(int x, int L, int R){
    d[x] = -1;
    if (L == R){
        t[x] = a[L];
        t2[x][a[L]] = 1;
        t2[x][a[L] ^ 1] = 0;
        t3[x][a[L]] = 1;
        t3[x][a[L] ^ 1] = 0;
        t4[x][a[L]] = 1;
        t4[x][a[L] ^ 1] = 0;
        return;
    }
    int mid = (L + R) >> 1;
    build(x << 1, L, mid);
    build(x << 1 | 1, mid + 1, R);
    pushup(x, L, R);
    return;
}
void change(int x, bool v, int l, int r, int L, int R){
    if (l == L && r == R){
        add(x, v, L, R);
        return;
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        change(x << 1, v, l, r, L, mid);
    } else if (l > mid){
        change(x << 1 | 1, v, l, r, mid + 1, R);
    } else {
        change(x << 1, v, l, mid, L, mid);
        change(x << 1 | 1, v, mid + 1, r, mid + 1, R);
    }
    pushup(x, L, R);
    return;
}
void change2(int x, int l, int r, int L, int R){
    if (l == L && r == R){
        add2(x, L, R);
        return;
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        change2(x << 1, l, r, L, mid);
    } else if (l > mid){
        change2(x << 1 | 1, l, r, mid + 1, R);
    } else {
        change2(x << 1, l, mid, L, mid);
        change2(x << 1 | 1, mid + 1, r, mid + 1, R);
    }
    pushup(x, L, R);
    return;
}
int query(int x, int l, int r, int L, int R){
    if (l == L && r == R){
        return t[x];
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        return query(x << 1, l, r, L, mid);
    } else if (l > mid){
        return query(x << 1 | 1, l, r, mid + 1, R);
    } else {
        return query(x << 1, l, mid, L, mid) + query(x << 1 | 1, mid + 1, r, mid + 1, R);
    }
}
int query3(int x, int l, int r, int L, int R){
    if (l == L && r == R){
        return t3[x][1];
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        return query3(x << 1, l, r, L, mid);
    } else if (l > mid){
        return query3(x << 1 | 1, l, r, mid + 1, R);
    } else {
        int res = query3(x << 1, l, mid + 1, L, mid + 1);
        if (res == mid - l + 1){
            return res + query3(x << 1 | 1, mid + 1, r, mid + 1, R);
        } else {
            return res;
        }
    }
}
int query4(int x, int l, int r, int L, int R){
    if (l == L && r == R){
        return t4[x][1];
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        return query4(x << 1, l, r, L, mid);
    } else if (l > mid){
        return query4(x << 1 | 1, l, r, mid + 1, R);
    } else {
        int res = query4(x << 1 | 1, mid + 1, r, mid + 1, R);
        if (res == r - mid){
            return res + query4(x << 1, l, mid, L, mid);
        } else {
            return res;
        }
    }
}
int query2(int x, int l, int r, int L, int R){
    if (l == L && r == R){
        return t2[x][1];
    }
    pushdown(x, L, R);
    int mid = (L + R) >> 1;
    if (r <= mid){
        return query2(x << 1, l, r, L, mid);
    } else if (l > mid){
        return query2(x << 1 | 1, l, r, mid + 1, R);
    } else {
        return max(query4(x << 1, l, mid, L, mid) + query3(x << 1 | 1, mid + 1, r, mid + 1, R), max(query2(x << 1, l, mid, L, mid), query2(x << 1 | 1, mid + 1, r, mid + 1, R)));
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m, op, l, r;
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    build(1, 1, n);
    while (m--){
        cin >> op >> l >> r;
        l++;
        r++;
        if (op == 0){
            change(1, 0, l, r, 1, n);
        } else if (op == 1){
            change(1, 1, l, r, 1, n);
        } else if (op == 2){
            change2(1, l, r, 1, n);
        } else if (op == 3){
            cout << query(1, l, r, 1, n) << endl;
        } else {
            cout << query2(1, l, r, 1, n) << endl;
        }
    }
    return 0;
}

|