HDU3397 Sequence operation(线段树)

原创 2012年03月30日 16:30:10

点击打开题目链接,还有一个类似的题,只进行01翻转操作和最大连续查询,HDU3911 Black And White

对一个数列进行如下操作:

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]

区间属性

1.最大连续长度=Max(左区间最大,有区间最大,左区间右起最大+有区间左起最大)

2.左端点开始的最大连续长度=左区间最大左连续  或   左区间长度+右区间最大左连续(如果左区间最大左连续==左区间长度)

3.有端点开始的最大连续长度,同上;

注意:

 1. 所有翻转的flag都要用 ^操作,避免连续翻转两次而未更新的区间。

 2.全置的优先级高于翻转


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main{
    class SegTree{
        class node{
            int left,right;
            int flag,num0,num1;
            int max0,l0,r0;
            int max1,l1,r1;
            
            int mid(){
                return (left+right)>>1;
            }     
            int length(){
                return right-left+1;
            }
            void init(int k){
                if(k==0){
                    num0=max0=l0=r0=length();
                    num1=l1=r1=max1=0;
                }
                else
                {
                    max1=l1=r1=num1=length();
                    num0=max0=l0=r0=0;
                }
            }
        }
        
        node tree[];
        SegTree(int maxn){
            tree=new node[maxn*5];
        }
        void init(int left,int right,int idx,int[]a){
            tree[idx]=new node();
            tree[idx].left=left;
            tree[idx].right=right;
            tree[idx].flag=-1;
            if(left==right){
            tree[idx].init(a[left]);
            return;
            }
            int mid=tree[idx].mid();
            init(left,mid,idx<<1,a);
            init(mid+1,right,(idx<<1)|1,a);
            pushup(idx);
        }
        void update(int left,int right,int idx,int op){
            pushdown(idx);
            if(tree[idx].left>=left&&tree[idx].right<=right){
                if(op==2)
                    reverse(idx);
                else
                    tree[idx].flag=op;
                return;
            }
            int mid=tree[idx].mid();
            if(left<=mid)
                update(left,right,idx<<1,op);
            if(right>mid)
                update(left,right,(idx<<1)|1,op);
            pushup(idx);
        }
        int query(int left,int right,int idx,int type)
        {   
            pushdown(idx);
            if(tree[idx].left==left&&tree[idx].right==right){
                if(type==3)
                    return tree[idx].num1;
                else
                    return tree[idx].max1;
            }
            int mid=tree[idx].mid();
            if(right<=mid)
                return query(left,right,idx<<1,type);
            else if(left>mid)
                return query(left,right,(idx<<1)|1,type);
            else{
                int a=query(left,mid,idx<<1,type);
                int b=query(mid+1,right,(idx<<1)|1,type);
                if(type==3)
                    return a+b;
                int c = Math.min(tree[idx << 1].r1, mid - left + 1)
                + Math.min(tree[(idx << 1) | 1].l1, right - mid);
                return Math.max(Math.max(a,b),c);
            }
                
        }
        void pushup(int idx){
            if(tree[idx].left==tree[idx].right)
                return;
            pushdown(idx<<1);
            pushdown((idx<<1)|1);
            tree[idx].num1=tree[idx<<1].num1+tree[(idx<<1)|1].num1;
            tree[idx].num0=tree[idx<<1].num0+tree[(idx<<1)|1].num0;
            int a=tree[idx<<1].max0;
            int b=tree[(idx<<1)|1].max0;
            int c=tree[idx<<1].r0+tree[(idx<<1)|1].l0;
            tree[idx].max0=Math.max(Math.max(a,b), c);
                        
            tree[idx].l0=tree[idx<<1].l0;
            if(tree[idx].l0==tree[idx<<1].length())
                tree[idx].l0+=tree[(idx<<1)|1].l0;
            tree[idx].r0=tree[(idx<<1)|1].r0;
            if(tree[(idx<<1)|1].r0==tree[(idx<<1)|1].length())
                tree[idx].r0+=tree[idx<<1].r0;
            
             a=tree[idx<<1].max1;
             b=tree[(idx<<1)|1].max1;
             c=tree[idx<<1].r1+tree[(idx<<1)|1].l1;
            tree[idx].max1=Math.max(Math.max(a,b), c);                        
            tree[idx].l1=tree[idx<<1].l1;
            if(tree[idx].l1==tree[idx<<1].length())
                tree[idx].l1+=tree[(idx<<1)+1].l1;
            tree[idx].r1=tree[(idx<<1)|1].r1;
            if(tree[(idx<<1)|1].r1==tree[(idx<<1)|1].length())
                tree[idx].r1+=tree[idx<<1].r1;            
        }
        void pushdown(int idx){
            if(tree[idx].flag==2){
                int t=tree[idx].l0;
                tree[idx].l0=tree[idx].l1;
                tree[idx].l1=t;
                t=tree[idx].r0;
                tree[idx].r0=tree[idx].r1;
                tree[idx].r1=t;
                t=tree[idx].max0;
                tree[idx].max0=tree[idx].max1;
                tree[idx].max1=t;
                tree[idx].flag=-1;
                t=tree[idx].num0;
                tree[idx].num0=tree[idx].num1;
                tree[idx].num1=t;
                if(tree[idx].left!=tree[idx].right){
                    t=idx<<1;
                    reverse(t);
                    t++;
                    reverse(t);
                }
            }
            if(tree[idx].flag==1||tree[idx].flag==0){
                int t=tree[idx].flag;
                tree[idx].init(t);
                tree[idx].flag=-1;
                if(tree[idx].left!=tree[idx].right){
                    tree[idx<<1].flag=t;
                    tree[(idx<<1)|1].flag=t;
                }
            }
        }
        void reverse(int idx){
            if(tree[idx].flag==2)
                tree[idx].flag=-1;
                else if(tree[idx].flag==-1)
                    tree[idx].flag=2;
                else
                    tree[idx].flag^=1;
        }
    }
    StreamTokenizer in = new StreamTokenizer(new BufferedReader(
            new InputStreamReader(System.in)));

    final int next() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }
    SegTree st=new SegTree(100010);
    int a[]=new int[100010];
   void run() throws IOException{
       int t=next();
       while(t-->0){
           int n=next();
           int m=next();
           for(int i=1;i<=n;i++)
               a[i]=next();
           st.init(1, n, 1, a);
           while(m-->0){
               int k=next();
               int a=next()+1;
               int b=next()+1;
               if(k<3)
                   st.update(a, b, 1, k);
               else
                   System.out.println(st.query(a, b, 1, k));
           }
       }
   }
    public static void main(String[] args) throws IOException {
        new Main().run();
    }

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Hdu 3397 Sequence operation(线段树多操作,Lazy思想,成段更新)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth...

hdu 3397 Sequence operation (线段树+区间合并+双Lazy标记)

给你一个长度为n的0、1序列,然后对该序列进行m次操作(1 0 a b 将[a, b]上的所有数变为0; 1 a b 将[a, b]上的所有数变为1; 2 a b 将[a, b]上的0变...

HDU 3397 Sequence operation(线段树区间合并)

题意: 0 a b将区间[a,b]所有数全部变成0 1 a b将区间[a,b]所有数全部变成1 2 a b将区间[a,b]中所有数0 1互换,0变1,1变0 3 a b输出区间[...

HDU 3397 Sequence operation (线段树,成段更新,区间合并)

http://acm.hdu.edu.cn/showproblem.php?pid=3397 Sequence operation Time Limit: 10000/5000 MS (J...

HDU3397(Sequence operation)线段树的多种操作

/********************************************** 题目大意: 0 a b将区间[a,b]所有数全部变成0 1 a b将区间[a,b]所有数全部变成1 2 ...
  • Jarily
  • Jarily
  • 2013-03-13 13:50
  • 1080

hdu3397Sequence operation(线段树,段异或,段覆盖,段查找)

lxhgww got a sequence contains n characters which are all '0's or '1's. We have five operations here...

HDU 3397 Sequence operation 线段树

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth...

HDU3397:Sequence operation(线段树区间合并)

Problem Description lxhgww got a sequence contains n characters which are all '0's or '1's. We hav...

线段树 HDU 3397 Sequence operation

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 代码风格:http://www.notonlysuccess.com/index.php/se...

HDU 3397 Sequence operation(线段树·成段更新·区间合并·混合操作)

题意  给你一个只有0, 1的数组  有这些操作   0. 将[a, b]区间的所有数都改为0   1. 将[a, b]区间的所有数都改为1   2. 将[a, b]区间的所有数都取反 即与1异...
  • acvay
  • acvay
  • 2015-08-12 10:12
  • 612
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)