【HDU - 3911】Black And White 【线段树+区间操作+区间合并】

There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [i, j].
Input
There are multiple cases, the first line of each case is an integer n(1<= n <= 10^5), followed by n integer 1 or 0(1 indicates black stone and 0 indicates white stone), then is an integer M(1<=M<=10^5) followed by M operations formatted as x i j(x = 0 or 1) , x=1 means change the color of stones in range[i,j], and x=0 means ask the longest period of consecutive black stones in range[i,j]
Output
When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input
4
1 0 1 0
5
0 1 4
1 2 3
0 1 4
1 3 3
0 4 4
Sample Output
1
2
0
可以提前先把这个题写了

题意:有n个数和m此操作,操作分两种——
1,0 x y 表示把区间[x, y]里面的1改为0,0改为1;
2,1 x y 表示查询区间[x, y]最多的连续的1的个数。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5+11;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
struct Tree{
    int l,r,len;// 同时维护区间的连续1的个数,和0的个数。
    int l1,r1,s1;
    int l0,r0,s0;
    int lazy;
}tree[MAXN<<2];
void Up(int o){
    tree[o].l1=tree[o<<1].l1;  //维护1
    tree[o].r1=tree[o<<1|1].r1;
    tree[o].s1=max(tree[o<<1].s1,tree[o<<1|1].s1);
    if(tree[o<<1].l1==tree[o<<1].len) 
        tree[o].l1+=tree[o<<1|1].l1;
    if(tree[o<<1|1].r1==tree[o<<1|1].len) 
        tree[o].r1+=tree[o<<1].r1;
    tree[o].s1=max(tree[o].s1,max(tree[o].l1,tree[o].r1));
    tree[o].s1=max(tree[o].s1,tree[o<<1].r1+tree[o<<1|1].l1);

    tree[o].l0=tree[o<<1].l0;//维护0
    tree[o].r0=tree[o<<1|1].r0;
    tree[o].s0=max(tree[o<<1].s0,tree[o<<1|1].s0);
    if(tree[o<<1].l0==tree[o<<1].len) 
        tree[o].l0+=tree[o<<1|1].l0;
    if(tree[o<<1|1].r0==tree[o<<1|1].len) 
        tree[o].r0+=tree[o<<1].r0;
    tree[o].s0=max(tree[o].s0,max(tree[o].l0,tree[o].r0));
    tree[o].s0=max(tree[o].s0,tree[o<<1].r0+tree[o<<1|1].l0);
}

void Change(int o){// 如果操作0时候,就是相当于把0和1的数据交换就可以了。
    swap(tree[o].l1,tree[o].l0);
    swap(tree[o].r1,tree[o].r0);
    swap(tree[o].s1,tree[o].s0);
    tree[o].lazy^=1; //  lazy是延迟标记,如果有2次lazy到这个区间,就相当于没有变化,所以用的^
}
void Down(int o){
    if(tree[o].lazy){
        Change(o<<1);
        Change(o<<1|1);
        tree[o].lazy=0;
    }
}
void Build(int o,int le,int ri){
    tree[o].l=le,tree[o].r=ri,tree[o].len=ri-le+1;
    tree[o].lazy=0;
    if(le==ri){
        int val;scanf("%d",&val);
        tree[o].l1=tree[o].r1=tree[o].s1=val?1:0;
        tree[o].l0=tree[o].r0=tree[o].s0=val?0:1;
        return ;
    } 
    int mid=(le+ri)>>1;
    Build(o<<1,le,mid);
    Build(o<<1|1,mid+1,ri);
    Up(o);
}
void Update(int o,int le,int ri){
    if(le<=tree[o].l&&tree[o].r<=ri) {
        Change(o);
        return ;
    }
    Down(o);
    int mid=(tree[o].l+tree[o].r)>>1;
    if(ri<=mid) Update(o<<1,le,ri);
    else if(le>mid) Update(o<<1|1,le,ri);
    else {
        Update(o<<1,le,mid);
        Update(o<<1|1,mid+1,ri);
    } 
    Up(o);
}
int Query(int o,int le,int ri){
    if(tree[o].l>=le&&tree[o].r<=ri) {
        return tree[o].s1;
    }
    Down(o);
    int mid=(tree[o].l+tree[o].r)>>1;
    if(ri<=mid) return Query(o<<1,le,ri);
    else if(le>mid) return Query(o<<1|1,le,ri);
    else {
         int ans=Query(o<<1,le,mid);
         ans=max(ans,Query(o<<1|1,mid+1,ri));
         return ans=max(ans,min(mid-le+1,tree[o<<1].r1)+min(ri-mid,tree[o<<1|1].l1));
    }
}

int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n;
    while(scanf("%d",&n)!=EOF){
        Build(1,1,n);
        int m;scanf("%d",&m);
        while(m--){
            int op,a,b;
            scanf("%d%d%d",&op,&a,&b);
            if(op)
                Update(1,a,b);
            else 
                printf("%d\n",Query(1,a,b));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值