BZOJ 1858 [Scoi2010]序列操作

1858: [Scoi2010]序列操作

Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 2866 Solved: 1385
[Submit][Status][Discuss]
Description

lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[a,b]区间内的所有数全部取反,也就是说把所有的0变成1,把所有的1变成0 3 a b 询问[a, b]区间内总共有多少个1 4 a b 询问[a, b]区间内最多有多少个连续的1 对于每一种询问操作,lxhgww都需要给出回答,聪明的程序员们,你们能帮助他吗?

Input

输入数据第一行包括2个数,n和m,分别表示序列的长度和操作数目 第二行包括n个数,表示序列的初始状态 接下来m行,每行3个数,op, a, b,(0 < = op < = 4,0 < = a < = b)

Output

对于每一个询问操作,输出一行,包括1个数,表示其对应的答案

Sample Input

10 10

0 0 0 1 1 0 1 0 1 1

1 0 2

3 0 5

2 2 2

4 0 4

0 3 6

2 3 7

4 2 8

1 0 5

0 5 6

3 3 9

Sample Output

5

2

6

5

HINT

对于30%的数据,1<=n, m<=1000 对于100%的数据,1< = n, m < = 100000

题解:就是一个线段树的题。其实用一个flag就好了。全部覆盖成0或1其实是可以直接覆盖的,swap的话就先判断一下之前有没有标记。两次swap等同于不交换,覆盖1等同于覆盖0,覆盖0同理。
居然忘了标记下传我个傻逼。。还信誓旦旦地说我觉得我的代码没大毛病,应该是哪儿的等号错了。绝望。
大概是废了。
其实对于0和1用数组会方便很多。两个序列的操作完全是对称的。这样代码太冗长了。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;

const int N = 300010;

int n,m;
int a[N];

struct node{
    int l,r;
    int mx1,mx0;
    int lm1,lm0,rm1,rm0;
    int num1,num0;
    int ord;
}t[N];

inline int Max(int a,int b){
    return a>b?a:b;
}

void update(int root){
    int len1=t[root<<1].r-t[root<<1].l+1,len2=t[root<<1|1].r-t[root<<1|1].l+1;
    t[root].lm1=t[root<<1].lm1;
    if(t[root<<1].lm1==len1) t[root].lm1+=t[root<<1|1].lm1;
    t[root].rm1=t[root<<1|1].rm1;
    if(t[root<<1|1].rm1==len2) t[root].rm1+=t[root<<1].rm1;
    //傻逼等号!!!! 
    int a=Max(t[root<<1].mx1,t[root<<1|1].mx1);
    t[root].mx1=Max(a,t[root<<1].rm1+t[root<<1|1].lm1);

    t[root].lm0=t[root<<1].lm0;
    if(t[root<<1].lm0==len1) t[root].lm0+=t[root<<1|1].lm0;
    t[root].rm0=t[root<<1|1].rm0;
    if(t[root<<1|1].rm0==len2) t[root].rm0+=t[root<<1].rm0;
    int aa=Max(t[root<<1].mx0,t[root<<1|1].mx0);
    t[root].mx0=Max(aa,t[root<<1].rm0+t[root<<1|1].lm0);    
    t[root].num1=t[root<<1].num1+t[root<<1|1].num1;
    t[root].num0=t[root<<1].num0+t[root<<1|1].num0;
}

void pushdown(int root){
    int l=t[root].l,r=t[root].r;
    if(t[root].ord==1){
        int len1=t[root<<1].r-t[root<<1].l+1;
        int len2=t[root<<1|1].r-t[root<<1|1].l+1;
        t[root<<1].rm1=t[root<<1].lm1=t[root<<1].num1=t[root<<1].mx1=len1;
        t[root<<1|1].rm1=t[root<<1|1].lm1=t[root<<1|1].num1=t[root<<1|1].mx1=len2;
        t[root<<1].rm0=t[root<<1].lm0=t[root<<1].num0=t[root<<1].mx0=0;
        t[root<<1|1].rm0=t[root<<1|1].lm0=t[root<<1|1].num0=t[root<<1|1].mx0=0; 
        t[root<<1].ord=t[root<<1|1].ord=1;  
    }
    else if(t[root].ord==0){
        int len1=t[root<<1].r-t[root<<1].l+1;
        int len2=t[root<<1|1].r-t[root<<1|1].l+1;
        t[root<<1].rm1=t[root<<1].lm1=t[root<<1].num1=t[root<<1].mx1=0;
        t[root<<1|1].rm1=t[root<<1|1].lm1=t[root<<1|1].num1=t[root<<1|1].mx1=0;
        t[root<<1].rm0=t[root<<1].lm0=t[root<<1].num0=t[root<<1].mx0=len1;
        t[root<<1|1].rm0=t[root<<1|1].lm0=t[root<<1|1].num0=t[root<<1|1].mx0=len2;              
        t[root<<1].ord=t[root<<1|1].ord=0;
    }
    else if(t[root].ord==2){
        swap(t[root<<1].mx1,t[root<<1].mx0);
        swap(t[root<<1].num1,t[root<<1].num0);
        swap(t[root<<1].lm1,t[root<<1].lm0);
        swap(t[root<<1].rm1,t[root<<1].rm0);
        if(t[root<<1].ord!=-1){
            if(t[root<<1].ord==2) t[root<<1].ord=-1;
            else t[root<<1].ord^=1;
        }
        else t[root<<1].ord=2;

        swap(t[root<<1|1].mx1,t[root<<1|1].mx0);
        swap(t[root<<1|1].num1,t[root<<1|1].num0);
        swap(t[root<<1|1].lm1,t[root<<1|1].lm0);
        swap(t[root<<1|1].rm1,t[root<<1|1].rm0);
        if(t[root<<1|1].ord!=-1){
            if(t[root<<1|1].ord==2) t[root<<1|1].ord=-1;
            else t[root<<1|1].ord^=1;
        }
        else t[root<<1|1].ord=2;            
    }
    t[root].ord=-1;
}

void build(int root,int l,int r){
    t[root].l=l,t[root].r=r,t[root].ord=-1;
    if(l==r){
        if(a[l]==1){
            t[root].num1=t[root].mx1=t[root].lm1=t[root].rm1=1;
            t[root].num0=t[root].mx0=t[root].lm0=t[root].rm0=0;
        }
        else{
            t[root].num0=t[root].mx0=t[root].lm0=t[root].rm0=1;
            t[root].num1=t[root].mx1=t[root].lm1=t[root].rm1=0;
        }
        t[root].ord=-1;
        return ;
    }
    int mid=l+r>>1;
    t[root].l=l,t[root].r=r,t[root].ord=-1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    t[root].ord=-1;
    update(root);
}

void Swap1(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r){
        swap(t[root].num0,t[root].num1);
        swap(t[root].mx1,t[root].mx0);
        swap(t[root].lm1,t[root].lm0);
        swap(t[root].rm1,t[root].rm0);
        if(t[root].ord!=-1){
            if(t[root].ord==2) t[root].ord=-1;
            else t[root].ord^=1;
        }
        else t[root].ord=2;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) Swap1(root<<1,pos,val);
    else if(pos>mid) Swap1(root<<1|1,pos,val);
    else {Swap1(root<<1,pos,val);Swap1(root<<1|1,pos,val);}
    update(root);
}

void modify0(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r){
        int len=r-l+1;
        t[root].lm0=t[root].num0=t[root].rm0=t[root].mx0=len;
        t[root].lm1=t[root].num1=t[root].rm1=t[root].mx1=0;
        t[root].ord=0;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) modify0(root<<1,pos,val);
    else if(pos>mid) modify0(root<<1|1,pos,val);
    else {modify0(root<<1,pos,val);modify0(root<<1|1,pos,val);}
    update(root);
}

void modify1(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r){
        int len=r-l+1;
        t[root].lm0=t[root].num0=t[root].rm0=t[root].mx0=0;     
        t[root].lm1=t[root].num1=t[root].rm1=t[root].mx1=len;
        t[root].ord=1;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) modify1(root<<1,pos,val);
    else if(pos>mid) modify1(root<<1|1,pos,val);
    else {modify1(root<<1,pos,val);modify1(root<<1|1,pos,val);}
    update(root);
}

int query1(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r) return t[root].num1;
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) return query1(root<<1,pos,val);
    else if(pos>mid) return query1(root<<1|1,pos,val);
    else return query1(root<<1,pos,val)+query1(root<<1|1,pos,val);
}

int query2(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r) return t[root].mx1;
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) return query2(root<<1,pos,val);
    else if(pos>mid) return query2(root<<1|1,pos,val);
    else{
        int la=query2(root<<1,pos,val);
        int ra=query2(root<<1|1,pos,val);
        la=Max(la,ra);

        int aa=t[root<<1].rm1;
        if(aa>t[root<<1].r-pos+1) aa=t[root<<1].r-pos+1;
        int bb=t[root<<1|1].lm1;
        if(bb>val-t[root<<1|1].l+1) bb=val-t[root<<1|1].l+1;
        return Max(la,aa+bb);
    }
}

int main(){
    scanf("%d%d",&n,&m);
    for(register int i=1;i<=n;i++)
       scanf("%d",&a[i]);
    build(1,1,n);
    while(m--){
        int x,l,r;
        scanf("%d%d%d",&x,&l,&r);
        l++,r++;
        if(x==0) modify0(1,l,r);
        else if(x==1) modify1(1,l,r);
        else if(x==2) Swap1(1,l,r);
        else if(x==3) printf("%d\n",query1(1,l,r));
        else printf("%d\n",query2(1,l,r));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值