1. 程式人生 > >HDU - 3397 Sequence operation 線段樹 區間合併

HDU - 3397 Sequence operation 線段樹 區間合併

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

題解:小細節害死人,區間合併,分別記錄 連續0的和連續1 的  改變的之後交換就好,另外就是,在全部變成0或1後 異或也就沒用了,清0就好

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+10;
struct node
{
    int l,r,len;
    int l0,r0,max0;
    int l1,r1,max1;
    int sum;
    int laz;
    int fg;
}tree[N<<2];
int n,q,x;
void pushup(int cur)
{
    tree[cur].l0=tree[cur<<1].l0;
    if(tree[cur<<1].l0==tree[cur<<1].len)
        tree[cur].l0+=tree[cur<<1|1].l0;
    tree[cur].r0=tree[cur<<1|1].r0;
    if(tree[cur<<1|1].r0==tree[cur<<1|1].len)
        tree[cur].r0+=tree[cur<<1].r0;
    tree[cur].max0=max(tree[cur<<1].max0,tree[cur<<1|1].max0);
    tree[cur].max0=max(tree[cur].max0,tree[cur<<1].r0+tree[cur<<1|1].l0);

    tree[cur].l1=tree[cur<<1].l1;
    if(tree[cur<<1].l1==tree[cur<<1].len)
        tree[cur].l1+=tree[cur<<1|1].l1;
    tree[cur].r1=tree[cur<<1|1].r1;
    if(tree[cur<<1|1].r1==tree[cur<<1|1].len)
        tree[cur].r1+=tree[cur<<1].r1;
    tree[cur].max1=max(tree[cur<<1].max1,tree[cur<<1|1].max1);
    tree[cur].max1=max(tree[cur].max1,tree[cur<<1].r1+tree[cur<<1|1].l1);

    tree[cur].sum=tree[cur<<1].sum+tree[cur<<1|1].sum;
}
void build(int l,int r,int cur)
{
    tree[cur].laz=0;
    tree[cur].l=l;
    tree[cur].r=r;
    tree[cur].fg=-1;
    tree[cur].len=r-l+1;
    if(l==r)
    {
        scanf("%d",&x);
        tree[cur].l1=tree[cur].r1=tree[cur].max1=(x==1);
        tree[cur].l0=tree[cur].r0=tree[cur].max0=(x==0);
        tree[cur].sum=x;
        return;
    }
    int mid=(r+l)>>1;
    build(l,mid,cur<<1);
    build(mid+1,r,cur<<1|1);
    pushup(cur);
}
void change(int cur)
{
    swap(tree[cur].l0,tree[cur].l1);
    swap(tree[cur].r0,tree[cur].r1);
    swap(tree[cur].max0,tree[cur].max1);
    tree[cur].sum=tree[cur].len-tree[cur].sum;
}
void pushdown(int cur)
{
    if(tree[cur].len==1) return;
    if(tree[cur].fg!=-1)
    {
        tree[cur<<1].fg=tree[cur<<1|1].fg=tree[cur].fg;
        if(tree[cur].fg==1)
        {
            tree[cur<<1].l0=tree[cur<<1].r0=tree[cur<<1].max0=0;
            tree[cur<<1].l1=tree[cur<<1].r1=tree[cur<<1].max1=tree[cur<<1].len;
            tree[cur<<1|1].l0=tree[cur<<1|1].r0=tree[cur<<1|1].max0=0;
            tree[cur<<1|1].l1=tree[cur<<1|1].r1=tree[cur<<1|1].max1=tree[cur<<1|1].len;

            tree[cur<<1].sum=tree[cur<<1].len;
            tree[cur<<1|1].sum=tree[cur<<1|1].len;
        }
        else
        {
            tree[cur<<1].l0=tree[cur<<1].r0=tree[cur<<1].max0=tree[cur<<1].len;
            tree[cur<<1].l1=tree[cur<<1].r1=tree[cur<<1].max1=0;
            tree[cur<<1|1].l0=tree[cur<<1|1].r0=tree[cur<<1|1].max0=tree[cur<<1|1].len;
            tree[cur<<1|1].l1=tree[cur<<1|1].r1=tree[cur<<1|1].max1=0;

            tree[cur<<1].sum=0;
            tree[cur<<1|1].sum=0;
        }
        tree[cur].fg=-1;
        tree[cur<<1].laz=tree[cur<<1|1].laz=0;
    }

    if(tree[cur].laz)
    {
        tree[cur<<1].laz^=1;
        tree[cur<<1|1].laz^=1;
        change(cur<<1);
        change(cur<<1|1);
        tree[cur].laz=0;
    }
}

void update(int pl,int pr,int cur,int fg)
{
    
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        if(fg==0)
        {
            tree[cur].fg=0;
            tree[cur].l0=tree[cur].r0=tree[cur].max0=tree[cur].len;
            tree[cur].l1=tree[cur].r1=tree[cur].max1=0;
            tree[cur].sum=0;
            tree[cur].laz=0;
        }
        if(fg==1)
        {
            tree[cur].fg=1;
            tree[cur].l0=tree[cur].r0=tree[cur].max0=0;
            tree[cur].l1=tree[cur].r1=tree[cur].max1=tree[cur].len;
            tree[cur].sum=tree[cur].len;
            tree[cur].laz=0;
        }
        if(fg==2)
        {
            tree[cur].laz^=1;
            change(cur);
        }
        return;
    }
    pushdown(cur);
    if(pl<=tree[cur<<1].r) update(pl,pr,cur<<1,fg);
    if(pr>=tree[cur<<1|1].l) update(pl,pr,cur<<1|1,fg);
    pushup(cur);
}
int query(int pl,int pr,int cur)
{
    
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        return tree[cur].max1;
    }
    pushdown(cur);
    if(pr<=tree[cur<<1].r) return query(pl,pr,cur<<1);
    else if(pl>=tree[cur<<1|1].l) return query(pl,pr,cur<<1|1);
    else
    {
        int res=min(pr,tree[cur<<1|1].l1+tree[cur<<1|1].l-1)-max(pl,tree[cur<<1].r-tree[cur<<1].r1+1)+1;
        res=max(res,query(pl,pr,cur<<1));
        res=max(res,query(pl,pr,cur<<1|1));
        return res;
    }
}
int query_(int pl,int pr,int cur)
{
    
 //   printf("%d-%d %d %d-%d\n",tree[cur].l,tree[cur].r,tree[cur].sum,pl,pr);
    if(pl<=tree[cur].l && tree[cur].r<=pr)
    {

        return tree[cur].sum;
    }
    int res=0;
    pushdown(cur);
    if(pl<=tree[cur<<1].r) res+=query_(pl,pr,cur<<1);
    if(pr>=tree[cur<<1|1].l) res+=query_(pl,pr,cur<<1|1);

    return res;
}
int main()
{
    int op,l,r;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&q);
        build(1,n,1);
        while(q--)
        {
            scanf("%d%d%d",&op,&l,&r);
            l++,r++;
            if(op==0||op==1||op==2)
                update(l,r,1,op);
            else if(op==3)
                printf("%d\n",query_(l,r,1));
            else
                printf("%d\n",query(l,r,1));

        }
    }
    return 0;
}