HDU 3397 Sequence operation 线段树 注意细节

Sequence operation

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5626    Accepted Submission(s): 1669


Problem Description
lxhgww got a sequence contains n characters which are all '0's or '1's.
We have five operations here:
Change operations:
0 a b change all characters into '0's in [a , b]
1 a b change all characters into '1's in [a , b]
2 a b change all '0's into '1's and change all '1's into '0's in [a, b]
Output operations:
3 a b output the number of '1's in [a, b]
4 a b output the length of the longest continuous '1' string in [a , b]
 

Input
T(T<=10) in the first line is the case number.
Each case has two integers in the first line: n and m (1 <= n , m <= 100000).
The next line contains n characters, '0' or '1' separated by spaces.
Then m lines are the operations:
op a b: 0 <= op <= 4 , 0 <= a <= b < n.
 

Output
For each output operation , output the result.
 

Sample Input
 
 
1 10 10 0 0 0 1 1 0 1 0 1 1 1 0 2 3 0 5 2 2 2 4 0 4 0 3 6 2 3 7 4 2 8 1 0 5 0 5 6 3 3 9
 

Sample Output
 
 
5 2 6 5
 

Author
lxhgww&&shǎ崽
 

Source
HDOJ Monthly Contest – 2010.05.01

传送门:HDU 3397 Sequence operation
题目大意:给你长度为n的一串01序列,m次操作。(n,m <= 100000)
一共五种操作:
0 L R 将【L,R】区间内的数覆盖为 0 。
1 L R 将【L,R】区间内的数覆盖为 1 。
2 L R 将【L,R】区间内的数异或( 0 变成 1,1 变成 0) 。
3 L R 输出【L,R】区间内的 1 的个数。
4 L R 输出【L,R】区间内最长的连续 1 的长度。

题目分析:
区间合并问题。
因为有两个覆盖操作以及一个异或操作,所以需要setv标记(覆盖)以及col标记(异或)。
因为本题的01异或,0和1的信息会进行交换,所以我们需要特别的分别记录每个区间的0和1的左连续最大lmax,区间最大mmax,右连续最大rmax。
记得覆盖的时候如果有异或 覆盖清异或 因为你咋异或覆盖一下就是那个值
对于异或操作的执行,尤其要注意,如果一个区间同时存在异或操作及覆盖操作,一定是覆盖操作后到(为什么?根据覆盖操作的执行思考一下,你如果先覆盖 那么就会清异或 所以你执行异或 一定是后覆盖)。对异或操作异或就行了。因为在PushDown里面先执行的是set,所以这样是不会影响结果的
然后注意一下细节就行了。
代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 100010;
int setv[MAX_N<<2],col[MAX_N<<2];
struct node {
    int len_l;
    int len_r;
    int len;
    int nnum;
}s[2][MAX_N<<2];
void SWAP ( int &X , int &Y ) {
  int tmp = X ; X = Y ; Y = tmp ;
}
void up(int x,int p,int l,int r){
    int mid = (l+r)>>1,len_len;
    s[x][p].nnum = s[x][p*2].nnum+s[x][p*2+1].nnum;
    s[x][p].len_l = s[x][p*2].len_l;
    if(s[x][p].len_l==mid-l+1)
        s[x][p].len_l+=s[x][p*2+1].len_l;
    s[x][p].len_r = s[x][p*2+1].len_r;
    if(s[x][p].len_r==r-mid)
        s[x][p].len_r+=s[x][p*2].len_r;
    len_len = max(s[x][p].len_r,s[x][p].len_l);
    s[x][p].len=max(len_len,max(max(s[x][p*2].len,s[x][p*2+1].len),s[x][p*2].len_r+s[x][p*2+1].len_l));
}
void build(int p,int l,int r){
    setv[p] = -1;
    col[p] = 0;
    if(l==r){
        scanf("%d",&s[1][p].len);
        if(s[1][p].len){
            setv[p] = 1;
            s[1][p].len_r=s[1][p].len_l=s[1][p].nnum=1;
            s[0][p].len=s[0][p].len_l=s[0][p].len_r=s[0][p].nnum= 0;
            col[p] = 0;
            return;
        }
        else {
            setv[p] = 0;
            s[0][p].nnum=s[0][p].len=s[0][p].len_r=s[0][p].len_l=1;
            s[1][p].len_l=s[1][p].len_r=s[1][p].nnum = 0;
            col[p] = 0;
            return ;
        }
    }
    int mid =(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    up(1,p,l,r);
    up(0,p,l,r);
}
void Swap(int p){
   col[p]^=1;
   SWAP(s[0][p].len_r,s[1][p].len_r);
   SWAP(s[0][p].len,s[1][p].len);
   SWAP(s[0][p].len_l,s[1][p].len_l);
   SWAP(s[0][p].nnum,s[1][p].nnum);
}
void down(int p,int l,int r){
    if(~setv[p]){
        int mid = (l+r)>>1;
        s[setv[p]][p*2].len_l=s[setv[p]][p*2].len_r=s[setv[p]][p*2].len=s[setv[p]][p*2].nnum = l-mid+1;
        s[!setv[p]][p*2].len_l=s[!setv[p]][p*2].len_r=s[!setv[p]][p*2].len=s[!setv[p]][p*2].nnum=0;
        s[setv[p]][p*2+1].len_l=s[setv[p]][p*2+1].len_r=s[setv[p]][p*2+1].len=s[setv[p]][p*2+1].nnum = r-mid;
        s[setv[p]^1][p*2+1].len_l=s[setv[p]^1][p*2+1].len_r=s[setv[p]^1][p*2+1].len=s[setv[p]^1][p*2+1].nnum=0;
        setv[p*2] = setv[p*2+1] = setv[p];
        setv[p] = -1;
        col[p*2]=col[p*2+1] = 0;
    }
    if(col[p]){
        Swap(p*2);
        Swap(p*2+1);
        col[p] = 0;
    }
}
void change(int type,int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        if(type==0){
            setv[p] = 0;
            s[0][p].nnum=s[0][p].len_l=s[0][p].len=s[0][p].len_r=r-l+1;
            s[1][p].nnum=s[1][p].len=s[1][p].len_l=s[1][p].len_r= 0;
            col[p] = 0;
        }
        else if(type==1){
            setv[p] = 1;
            s[0][p].nnum=s[0][p].len_l=s[0][p].len=s[0][p].len_r=0;
            s[1][p].nnum=s[1][p].len=s[1][p].len_l=s[1][p].len_r= r-l+1;
            col[p] = 0;
        }
        else if(type==2){
            Swap(p);
        }
        return ;
    }
    down(p,l,r);
    int mid = (l+r)>>1;
    if(x<=mid) change(type,p*2,l,mid,x,y);
    if(y>mid) change(type,p*2+1,mid+1,r,x,y);
    up(0,p,l,r);
    up(1,p,l,r);
}
int query(int type,int p,int l,int r,int x,int y){
     if(x<=l&&r<=y){
        if(type==3) return s[1][p].nnum;
        else if(type==4) return s[1][p].len;
     }
     int mid = (l+r)>>1,nnum =0,res1=-1,res2=-1,res3 = -1;
     down(p,l,r);
     if(type==3){
        if(x<=mid) nnum+=query(type,p*2,l,mid,x,y);
        if(y>mid) nnum+=query(type,p*2+1,mid+1,r,x,y);
        return nnum;
     }
     else if(type==4){
        if(x<=mid) res1=query(type,p*2,l,mid,x,y);
        if(y>mid) res2 = query(type,p*2+1,mid+1,r,x,y);
        if(x<=mid&&y>mid && (s[1][p*2].len_r&&s[1][p*2+1].len_l)){
               res3 = min(mid-x+1,s[1][p*2].len_r)+min(y-mid,s[1][p*2+1].len_l);
        }
        return max(max(res1,res2),res3);
     }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    memset(col,0,sizeof(col));
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,1,n);
    while(m--){
       int a,b,c;
       scanf("%d%d%d",&a,&b,&c);
       if(a==0||a==1||a==2){
        change(a,1,1,n,b+1,c+1);
       }
       else if(a==3||a==4){
        printf("%d\n",query(a,1,1,n,b+1,c+1));
    }
    }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值