HDU3397 Sequence Operation

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397

这道题是一个我认为非常非常麻烦的题,操作非常多,而且要打两个懒惰标记,并且懒惰标记有优先级之分,当一个节点收到一个覆盖标记的时候,取反标记就应该立即消失,这是一个麻烦,另外一个麻烦就是一个结点需要维持七个变量以满足本题的操作和所求。区间合并 的基本思想不用赘述,下面有一个成形的代码(貌似有bug),不过想法已经体现出来。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define lson l, m, rt<<1
#define rson m + 1, r, rt<<1|1
const int maxn = 111111;
int l1s[maxn<<2], r1s[maxn<<2], m1s[maxn<<2], l0s[maxn<<2], r0s[maxn<<2], m0s[maxn<<2], sum[maxn<<2];
int cover[maxn<<2], col[maxn<<2];
void PushDown(int rt, int m){
    if (cover[rt] != -1){
        cover[rt<<1] = cover[rt<<1|1] = cover[rt];
        col[rt] = col[rt<<1] = col[rt<<1|1] = 0;
        l1s[rt<<1] = r1s[rt<<1] = m1s[rt<<1] = sum[rt<<1] = cover[rt] ? m - (m >> 1) : 0;
        l1s[rt<<1|1] = r1s[rt<<1|1] = m1s[rt<<1|1] = sum[rt<<1|1] = cover[rt] ? (m >> 1) : 0;
        l0s[rt<<1] = r0s[rt<<1] = m0s[rt<<1] = cover[rt] ? 0 : m - (m >> 1);
        l0s[rt<<1|1] = r0s[rt<<1|1] = m0s[rt<<1|1] = cover[rt] ? 0 : (m >> 1);
        cover[rt] = -1;
    }
    else if (col[rt]){
        col[rt<<1] += col[rt];
        col[rt<<1|1] += col[rt];
        col[rt] %= 2;
        if (col[rt]){
            swap(l1s[rt<<1], l0s[rt<<1]);
            swap(l1s[rt<<1|1], l0s[rt<<1|1]);
            swap(r1s[rt<<1], r0s[rt<<1]);
            swap(r1s[rt<<1|1], r0s[rt<<1|1]);
            swap(m1s[rt<<1], m0s[rt<<1]);
            swap(m1s[rt<<1|1], m0s[rt<<1|1]);
            sum[rt<<1] = (m - (m>>1)) - sum[rt<<1];
            sum[rt<<1|1] = (m>>1) - sum[rt>>1|1];
        }
        col[rt] = 0;
    }
}
void PushUp(int rt, int m){
    l1s[rt] = l1s[rt<<1]; l0s[rt] = l0s[rt<<1];
    r1s[rt] = r1s[rt<<1|1]; r0s[rt] = r0s[rt<<1|1];
    if (l1s[rt] == m - (m >> 1)) l1s[rt] += l1s[rt<<1|1];
    if (r1s[rt] == (m >> 1)) r1s[rt] += r1s[rt<<1];
    if (l0s[rt] == m - (m >> 1)) l0s[rt] += l0s[rt<<1|1];
    if (r0s[rt] == (m >> 1)) r0s[rt] += r0s[rt<<1];
    m1s[rt] = max(l1s[rt<<1|1] + r1s[rt<<1], max(m1s[rt<<1], m1s[rt<<1|1]));
    m0s[rt] = max(l0s[rt<<1|1] + r0s[rt<<1], max(m0s[rt<<1], m0s[rt<<1|1]));
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l, int r, int rt){
    cover[rt] = -1; col[rt] = 0;
    if (l == r){
        scanf("%d", &sum[rt]);
        l1s[rt] = r1s[rt] = m1s[rt] = cover[rt] = sum[rt];
        l0s[rt] = r0s[rt] = m0s[rt] = 1 - sum[rt];
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt, r - l + 1);
}
void update(int ll, int rr, int c, int l, int r, int rt){
    if (ll <= l && rr >= r){
        if (c == 2){
            col[rt] += 1;
            sum[rt] = (r - l + 1) - sum[rt];
            swap(m1s[rt], m0s[rt]);
            swap(l1s[rt], l0s[rt]);
            swap(r1s[rt], r0s[rt]);
        }
        else{
            l1s[rt] = r1s[rt] = m1s[rt] = sum[rt] = c ? (r - l + 1) : 0;
            l0s[rt] = r0s[rt] = m0s[rt] = c ? 0 : r - l + 1;
            col[rt] = 0;
            cover[rt] = c;
        }
    }
    PushDown(rt, r - l + 1);
    int m = (l + r) >> 1;
    if (ll <= m) update(ll, rr, c, lson);
    if (rr > m) update(ll, rr, c, rson);
    PushUp(rt, r - l + 1);
}
int query_sum(int ll, int rr, int l, int r, int rt){
    if (ll <= l && rr >= r) return sum[rt];
    PushDown(rt, r - l + 1);
    int m = (l + r) >> 1;
    int ret = 0;
    if (ll <= m) ret += query_sum(ll, rr, lson);
    if (rr > m) ret += query_sum(ll, rr, rson);
    return ret;
}
int query_maxlen(int ll, int rr, int l, int r, int rt){
    if (ll == l && rr == r) return m1s[rt];
    PushDown(rt, r - l + 1);
    int m = (l + 1) >> 1;
    if (rr <= m) return query_maxlen(ll, rr, lson);
    else if (ll >= m) return query_maxlen(ll, rr, rson);
    else{
        int a = query_maxlen(ll, m, lson);
        int b = query_maxlen(m+1, rr, rson);
        int c = 0;
        c += max(r1s[rt<<1], m - ll + 1);
        c += max(l1s[rt<<1|1], rr - m);
        return max(a, max(b, c));
    }
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        int op, a, b;
        int n, m;
        scanf("%d%d", &n, &m);
        build(1, n, 1);
        while(m--){
            scanf("%d%d%d", &op, &a, &b);
            a += 1; b += 1;
            if (op <= 2)
              update(a, b, op, 1, n, 1);
            else if (op == 3){
                int ans = query_sum(a, b, 1, n, 1);
                printf("%d\n", ans);
            }
            else{
                int ans = query_maxlen(a, b, 1, n, 1);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值