HDU 3911 线段树区间合并

北京赛区快了,准备袭击数据结构和图论。倒计时 18天,线段树区间合并。维护一个最长连续。。

题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数

思路:非常裸的线段树区间合并
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<cmath>
#define lson id << 1
#define rson id << 1|1
using namespace std;
const int  M = 1e6+8;
int a[M];
struct tree{
    int l,r;
    int rsum1,lsum1,msum1,lsum0,rsum0,msum0;
    int flag;
    int mid(){
        return (l+r)/2;
    }
}node[M];
void pushdown(int id){
    if(node[id].flag){
        node[lson].flag ^= 1;
        node[rson].flag ^= 1;
        node[id].flag = 0;
        swap(node[lson].lsum1,node[lson].lsum0);
        swap(node[lson].rsum1,node[lson].rsum0);
        swap(node[lson].msum1,node[lson].msum0);

        swap(node[rson].lsum1,node[rson].lsum0);
        swap(node[rson].rsum1,node[rson].rsum0);
        swap(node[rson].msum1,node[rson].msum0);
    }
}
void pushup(int id){
    int ll = node[lson].r-node[lson].l + 1;
    int rl = node[rson].r-node[rson].l + 1;
    node[id].lsum1 = node[lson].lsum1;
    if(node[lson].lsum1 == ll)node[id].lsum1 += node[rson].lsum1;
    node[id].rsum1 = node[rson].rsum1;
    if(node[rson].rsum1 == rl)node[id].rsum1 +=  node[lson].rsum1;
    node[id].msum1 = max(max(node[rson].msum1,node[lson].msum1),node[lson].rsum1+node[rson].lsum1);

    node[id].lsum0 = node[lson].lsum0;
    if(node[lson].lsum0 == ll)node[id].lsum0 += node[rson].lsum0;
    node[id].rsum0 = node[rson].rsum0;
    if(node[rson].rsum0 == rl)node[id].rsum0 +=  node[lson].rsum0;
    node[id].msum0 = max(max(node[rson].msum0,node[lson].msum0),node[lson].rsum0+node[rson].lsum0);

}
void build(int l,int r,int id){
    node[id].l = l;
    node[id].r = r;
    node[id].flag = 0;
    if(l == r){
        if(a[l] == 1){
            node[id].lsum1 = node[id].rsum1 = node[id].msum1 = 1;
            node[id].lsum0 = node[id].rsum0 = node[id].msum0 = 0;
        }else{
            node[id].lsum1 = node[id].rsum1 = node[id].msum1 = 0;
            node[id].lsum0 = node[id].rsum0 = node[id].msum0 = 1;
        }
        return;
    }
    int mid = node[id].mid();
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(id);
}
void update(int id,int l,int r){
    if(node[id].l == l && node[id].r == r){
        node[id].flag ^= 1;
        swap(node[id].lsum1,node[id].lsum0);
        swap(node[id].rsum1,node[id].rsum0);
        swap(node[id].msum1,node[id].msum0);
        return ;
    }
    pushdown(id);
    int mid = node[id].mid();
    if(r <= mid)update(lson,l,r);
    else if(l>mid)update(rson,l,r);
    else {
        update(lson,l,mid);
        update(rson,mid+1,r);
    }
    pushup(id);
}
int query(int id,int l,int r){
    if(node[id].l == l && node[id].r ==r){
        return node[id].msum1;
    }
    pushdown(id);
    int mid = node[id].mid();
    if(r <=mid)return query(lson,l,r);
    else
        if(l > mid)return query(rson,l,r);
    else {
        int ll = query(lson,l,mid);
        int rr = query(rson,mid+1,r);
        int a = node[lson].rsum1;
        if(a > (node[lson].r - l +1))a = node[lson].r - l +1 ;

        int b = node[rson].lsum1;
        if(b > (r - node[rson].l+1))b = r - node[rson].l+1;
        return max(max(ll,rr),a+b);
    }

}
int main(){
    int n,m,op,l,r;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d%d%d",&op,&l,&r);
            if(!op)printf("%d\n",query(1,l,r));
            else update(1,l,r);
        }
    }
}


转载于:https://www.cnblogs.com/zhchoutai/p/7017046.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值