• 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
    ",query(1,l,r));
                else update(1,l,r);
            }
        }
    }
    


  • 相关阅读:
    MySQL 知识点
    用PHP操作http中Etag、lastModified和Expires标签
    Open Flash Chart在php中的使用教程
    Cmake,source_group
    Cmake调用NSIS(一个可执行文件,其实就是一个编译器)编译NSIS脚本问题研究
    VS2010安装与测试编译问题(fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt)
    Cmake find_package()相关
    Cmake,链接一个外部(也可能是第三方,也可能是自己编译的)库
    逆向工程入门指南
    Cmake的install与file命令的区别
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7017046.html
Copyright © 2020-2023  润新知