首页 > 代码库 > HDU 3911 线段树区间合并

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);
        }
    }
}


HDU 3911 线段树区间合并