BZOJ 4592: [Shoi2015]脑洞治疗仪

4592: [Shoi2015]脑洞治疗仪

Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 474 Solved: 209
[Submit][Status][Discuss]
Description

曾经发明了自动刷题机的发明家SHTSC又公开了他的新发明:脑洞治疗仪–一种可以治疗他因为发明而日益增大的脑洞的神秘装置。
为了简单起见,我们将大脑视作一个01序列。1代表这个位置的脑组织正常工作,0代表这是一块脑洞。
1 0 1 0 0 0 1 1 1 0
脑洞治疗仪修补某一块脑洞的基本工作原理就是将另一块连续区域挖出,将其中正常工作的脑组织填补在这块脑洞中。
(所以脑洞治疗仪是脑洞的治疗仪?)
例如,用上面第8号位置到第10号位置去修补第1号位置到第4号位置的脑洞。我们就会得到:
1 1 1 1 0 0 1 0 0 0
如果再用第1号位置到第4号位置去修补第8号位置到第10号位置:
0 0 0 0 0 0 1 1 1 1
这是因为脑洞治疗仪会把多余出来的脑组织直接扔掉。
如果再用第7号位置到第10号位置去填补第1号位置到第6号位置:
1 1 1 1 0 0 0 0 0 0
这是因为如果新脑洞挖出来的脑组织不够多,脑洞治疗仪仅会尽量填补位置比较靠前的脑洞。
假定初始时SHTSC并没有脑洞,给出一些挖脑洞和脑洞治疗的操作序列,你需要即时回答SHTSC的问题:
在大脑某个区间中最大的连续脑洞区域有多大。
Input

第一行两个整数n,m。表示SHTSC的大脑可分为从1到n编号的n个连续区域。有m个操作。
以下m行每行是下列三种格式之一。
0 l r :SHTSC挖了一个从l到r的脑洞。
1 l0 r0 l1 r2 :SHTSC进行了一次脑洞治疗,用从l0到r0的脑组织修补l1到r1的脑洞。
2 l r :SHTSC询问l到r这段区间最大的脑洞有多大。
n,m <=200000,1<=l<=r<=n
Output

对于每个询问,输出一行一个整数,表示询问区间内最大连续脑洞区域有多大。
Sample Input

10 10

0 2 2

0 4 6

0 10 10

2 1 10

1 8 10 1 4

2 1 10

1 1 4 8 10

2 1 10

1 7 10 1 6

2 1 10

Sample Output

3

3

6

6

woc好感动居然不看题解A了这道题。(虽然借了别人的程序来对拍 【你滚
可惜之前求num的时候没加pushdown WA了一发(太NC了)。但思路还是很完美的23333
就是线段树维护01序列的问题,注意填脑洞的时候从左往右填,判断各种情况。
1。左区间完全包含
2,右区间完全包含
3。跨越左右区间但不够填左边的脑洞(递归左区间继续填)
4。跨越左右区间且足够填左边的脑洞(就把左边完全覆盖,再用剩下的递归下去填右区间)

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

const int N = 1000010;

int n,m;

struct node{
    int mx0,mx1;
    int rm1,rm0,lm1,lm0;
    int num1,num0;
    int l,r;
    int flag;
}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;
    int 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);
    int b=t[root<<1].rm1+t[root<<1|1].lm1;
    t[root].mx1=Max(a,b);

    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;
    a=Max(t[root<<1].mx0,t[root<<1|1].mx0);
    b=t[root<<1].rm0+t[root<<1|1].lm0;
    t[root].mx0=Max(a,b);   
    t[root].num0=t[root<<1].num0+t[root<<1|1].num0;
    t[root].num1=t[root<<1].num1+t[root<<1|1].num1;
}

void pushdown(int root){
    int flag=t[root].flag;
    int mid=t[root].l+t[root].r>>1;
    if(flag!=-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;
        if(flag==1){
            t[root<<1].mx1=t[root<<1].num1=t[root<<1].lm1=t[root<<1].rm1=len1;
            t[root<<1|1].mx1=t[root<<1|1].num1=t[root<<1|1].lm1=t[root<<1|1].rm1=len2;
            t[root<<1].mx0=t[root<<1].num0=t[root<<1].lm0=t[root<<1].rm0=0;
            t[root<<1|1].mx0=t[root<<1|1].num0=t[root<<1|1].lm0=t[root<<1|1].rm0=0;
            t[root<<1].flag=1,t[root<<1|1].flag=1;          
        }
        else {
            t[root<<1].mx1=t[root<<1].num1=t[root<<1].lm1=t[root<<1].rm1=0;
            t[root<<1|1].mx1=t[root<<1|1].num1=t[root<<1|1].lm1=t[root<<1|1].rm1=0;
            t[root<<1].mx0=t[root<<1].num0=t[root<<1].lm0=t[root<<1].rm0=len1;
            t[root<<1|1].mx0=t[root<<1|1].num0=t[root<<1|1].lm0=t[root<<1|1].rm0=len2;
            t[root<<1].flag=0,t[root<<1|1].flag=0;                  
        }
    }
    t[root].flag=-1;
}

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

int query_num1(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);
    int ans=0;
    if(pos<=mid) ans+=query_num1(root<<1,pos,val);
    if(val>mid) ans+=query_num1(root<<1|1,pos,val);
    return ans;
}

int query_num0(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r) return t[root].num0;
    int mid=l+r>>1;
    pushdown(root);
    int ans=0;
    if(pos<=mid) ans+=query_num0(root<<1,pos,val);
    if(val>mid) ans+=query_num0(root<<1|1,pos,val);
    return ans; 
}

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

int query(int root,int pos,int val){
    int l=t[root].l,r=t[root].r;
    if(pos<=l&&val>=r) return t[root].mx0;
    int mid=(l+r)>>1;
    pushdown(root);
    if(val<=mid) return query(root<<1,pos,val);
    else if(pos>mid) return query(root<<1|1,pos,val);
    else{
        int a=query(root<<1,pos,val);
        int b=query(root<<1|1,pos,val);
        a=Max(a,b);

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

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

int main(){
    scanf("%d%d",&n,&m);
    build(1,1,n);
    while(m--){
        int x;
        scanf("%d",&x);
        if(x==0){
            int l,r;
            scanf("%d%d",&l,&r);
            modify(1,l,r,0);
        }
        else if(x==1){
            int l0,r0,l1,r1;
            scanf("%d%d%d%d",&l0,&r0,&l1,&r1);
            if(l0>r0) swap(l0,r0);
            if(l1>r1) swap(l1,r1);
            int cnt=query_num1(1,l0,r0);
            modify(1,l0,r0,0);
            print(1,l1,r1,cnt);
        }
        else {
            int l,r;
            scanf("%d%d",&l,&r);
            if(l>r) swap(l,r);
            printf("%d\n",query(1,l,r));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值