zoukankan      html  css  js  c++  java
  • 【BZOJ1858】序列操作(线段树)

    【BZOJ1858】序列操作(线段树)

    题面

    BZOJ

    题解

    这题思路很简单,细节很烦,很码

    维护区间翻转和区间赋值标记

    当打到区间赋值标记时直接覆盖掉翻转标记

    下放标记的时候先放赋值标记再放翻转标记

    这样可以维护前4个操作

    对于第5个操作

    维护区间从左/右端点开始的最大连续(0/1)的个数

    以及区间内的最大连续(0/1)的个数

    做区间翻转的时候所有的关于(0/1)连续个数的计数全部要交换过来

    其他的细节自己注意一下

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define ll long long
    #define RG register
    #define MAX 120000
    #define lson (now<<1)
    #define rson (now<<1|1)
    inline int read()
    {
        RG int x=0,t=1;RG char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return x*t;
    }
    int n,m;
    struct Node
    {
    	int tag1,tag2;
    	int v,l,r;
    	int lg0,rg0,mg0;
    	int lg1,rg1,mg1;
    	void len0(){lg0=rg0=mg0=r-l+1;lg1=rg1=mg1=0;}
    	void len1(){lg1=rg1=mg1=r-l+1;lg0=rg0=mg0=0;}
    	void rev(){swap(lg0,lg1);swap(rg0,rg1);swap(mg0,mg1);}
    	void clear(){tag1=-1;tag2=v=l=r=lg0=rg0=mg0=lg1=rg1=mg1=0;}
    }t[MAX<<2];
    Node operator+(Node a,Node b)
    {
    	Node c;c.tag1=-1;c.tag2=0;
    	c.v=a.v+b.v;c.l=a.l;c.r=b.r;
    	c.lg1=a.lg1;c.rg1=b.rg1;
    	if(a.mg1==a.r-a.l+1)c.lg1+=b.lg1;
    	if(b.mg1==b.r-b.l+1)c.rg1+=a.rg1;
    	c.mg1=max(a.mg1,b.mg1);
    	c.mg1=max(c.mg1,c.lg1);
    	c.mg1=max(c.mg1,c.rg1);
    	c.mg1=max(c.mg1,a.rg1+b.lg1);
    	c.lg0=a.lg0;c.rg0=b.rg0;
    	if(a.mg0==a.r-a.l+1)c.lg0+=b.lg0;
    	if(b.mg0==b.r-b.l+1)c.rg0+=a.rg0;
    	c.mg0=max(a.mg0,b.mg0);
    	c.mg0=max(c.mg0,c.lg0);
    	c.mg0=max(c.mg0,c.rg0);
    	c.mg0=max(c.mg0,a.rg0+b.lg0);
    	return c;
    }
    void Build(int now,int l,int r)
    {
    	t[now].l=l;t[now].r=r;
    	t[now].tag1=-1;t[now].tag2=0;
    	if(l==r)
    	{
    		t[now].v=read();
    		if(t[now].v)t[now].len1();
    		else t[now].len0();
    		return;
    	}
    	int mid=(l+r)>>1;
    	Build(lson,l,mid);Build(rson,mid+1,r);
    	t[now]=t[lson]+t[rson];
    }
    void putrev(int now,int l,int r)
    {
    	t[now].v=r-l+1-t[now].v;
    	t[now].rev();
    	t[now].tag2^=1;
    }
    void puteql(int now,int l,int r,int w)
    {
    	if(w)t[now].len1(),t[now].v=r-l+1;
    	else t[now].len0(),t[now].v=0;
    	t[now].tag1=w;t[now].tag2=0;
    }
    void pushdown(int now,int l,int r)
    {
    	int mid=(l+r)>>1;
    	if(t[now].tag1!=-1)
    	{
    		puteql(lson,l,mid,t[now].tag1);
    		puteql(rson,mid+1,r,t[now].tag1);
    		t[now].tag1=-1;
    	}
    	if(t[now].tag2)
    	{
    		putrev(lson,l,mid);
    		putrev(rson,mid+1,r);
    		t[now].tag2^=1;
    	}
    }
    void Modify_Eql(int now,int L,int R,int w)
    {
    	if(L<=t[now].l&&t[now].r<=R){puteql(now,t[now].l,t[now].r,w);return;}
    	pushdown(now,t[now].l,t[now].r);
    	int mid=(t[now].l+t[now].r)>>1;
    	if(L<=mid)Modify_Eql(lson,L,R,w);
    	if(R>mid)Modify_Eql(rson,L,R,w);
    	t[now]=t[lson]+t[rson];
    }
    void Modify_rev(int now,int L,int R)
    {
    	if(L<=t[now].l&&t[now].r<=R){putrev(now,t[now].l,t[now].r);return;}
    	pushdown(now,t[now].l,t[now].r);
    	int mid=(t[now].l+t[now].r)>>1;
    	if(L<=mid)Modify_rev(lson,L,R);
    	if(R>mid)Modify_rev(rson,L,R);
    	t[now]=t[lson]+t[rson];
    }
    int Query_Sum(int now,int L,int R)
    {
    	if(L<=t[now].l&&t[now].r<=R)return t[now].v;
    	pushdown(now,t[now].l,t[now].r);
    	int mid=(t[now].l+t[now].r)>>1,ret=0;
    	if(L<=mid)ret+=Query_Sum(lson,L,R);
    	if(R>mid)ret+=Query_Sum(rson,L,R);
    	return ret;
    }
    Node Query_One(int now,int L,int R)
    {
    	if(L==t[now].l&&t[now].r==R)return t[now];
    	pushdown(now,t[now].l,t[now].r);
    	int mid=(t[now].l+t[now].r)>>1;
    	if(R<=mid)return Query_One(lson,L,R);
    	if(L>mid)return Query_One(rson,L,R);
    	return Query_One(lson,L,mid)+Query_One(rson,mid+1,R);
    }
    int main()
    {
    	n=read();m=read();
    	Build(1,1,n);
    	while(m--)
    	{
    		int opt=read(),l=read()+1,r=read()+1;
    		if(opt==0)Modify_Eql(1,l,r,0);
    		if(opt==1)Modify_Eql(1,l,r,1);
    		if(opt==2)Modify_rev(1,l,r);
    		if(opt==3)printf("%d
    ",Query_Sum(1,l,r));
    		if(opt==4)printf("%d
    ",Query_One(1,l,r).mg1);
    	}
    	return 0;
    }
    
  • 相关阅读:
    查看线程
    shiro+多tomcat+redis实现session共享
    win11系统设置笔记本合盖上不休眠
    nvm切换node版本出现乱码 exit status 1:
    nvm安装vuecli
    SQL Server Management 2012 启动错误及解决:Cannot find one or more componets
    SQL Server 2012 连接 Oracle 11gR2 Database
    SQL Server 数据库跨区域时间问题
    SSIS 同步不同数据库的不同两张表
    Reporting Service 不能发送订阅报表的问题
  • 原文地址:https://www.cnblogs.com/cjyyb/p/8557184.html
Copyright © 2011-2022 走看看