标签:
4 1 0 1 0 5 0 1 4 1 2 3 0 1 4 1 3 3 0 4 4
1 2 0
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int mod = 1000000007;
const int maxn=1e5+10;
int lmax_1[maxn<<2],rmax_1[maxn<<2];
int lmax_0[maxn<<2],rmax_0[maxn<<2];
int max_1[maxn<<2],max_0[maxn<<2];
int col[maxn<<2];
int n,m,os;
void pushup(int rs,int m)
{
lmax_1[rs]=lmax_1[rs<<1];//处理左边一类前缀
lmax_0[rs]=lmax_0[rs<<1];
if(lmax_1[rs<<1]==m-(m>>1)) lmax_1[rs]+=lmax_1[rs<<1|1];
if(lmax_0[rs<<1]==m-(m>>1)) lmax_0[rs]+=lmax_0[rs<<1|1];
rmax_1[rs]=rmax_1[rs<<1|1];//处理右边一类后缀
rmax_0[rs]=rmax_0[rs<<1|1];
if(rmax_1[rs<<1|1]==(m>>1)) rmax_1[rs]+=rmax_1[rs<<1];
if(rmax_0[rs<<1|1]==(m>>1)) rmax_0[rs]+=rmax_0[rs<<1];
max_1[rs]=max(max_1[rs<<1],max_1[rs<<1|1]);//处理整个区间的值
max_1[rs]=max(max_1[rs],rmax_1[rs<<1]+lmax_1[rs<<1|1]);
max_0[rs]=max(max_0[rs<<1],max_0[rs<<1|1]);
max_0[rs]=max(max_0[rs],rmax_0[rs<<1]+lmax_0[rs<<1|1]);
}
void work(int rs)
{
swap(lmax_1[rs],lmax_0[rs]);
swap(rmax_1[rs],rmax_0[rs]);
swap(max_1[rs],max_0[rs]);
}
void push_down(int rs)
{
if(col[rs])
{
col[rs<<1]^=1;col[rs<<1|1]^=1;
col[rs]=0;work(rs<<1);work(rs<<1|1);
}
}
void build(int rs,int l,int r)
{
col[rs]=0;
if(l==r)
{
scanf("%d",&os);
if(os==1)
{
lmax_1[rs]=rmax_1[rs]=max_1[rs]=1;
lmax_0[rs]=rmax_0[rs]=max_0[rs]=0;
}
else
{
lmax_1[rs]=rmax_1[rs]=max_1[rs]=0;
lmax_0[rs]=rmax_0[rs]=max_0[rs]=1;
}
return ;
}
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
pushup(rs,r-l+1);
}
void update(int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
{
col[rs]^=1;
work(rs);//交换值
return ;
}
push_down(rs);
int mid=(l+r)>>1;
if(x<=mid) update(x,y,l,mid,rs<<1);
if(y>mid) update(x,y,mid+1,r,rs<<1|1);
pushup(rs,r-l+1);
}
int query(int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
return max_1[rs];
push_down(rs);
int mid=(l+r)>>1;
if(x>mid) return query(x,y,mid+1,r,rs<<1|1);
if(y<=mid) return query(x,y,l,mid,rs<<1);
int t1=query(x,y,l,mid,rs<<1);
int t2=query(x,y,mid+1,r,rs<<1|1);
int r1=min(mid-x+1,rmax_1[rs<<1]);//以mid为分割点
int r2=min(y-mid,lmax_1[rs<<1|1]);
int res=max(max(t1,t2),r1+r2);
return res;
}
int main()
{
int op,x,y;
while(~scanf("%d",&n))
{
build(1,1,n);
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d",&op,&x,&y);
if(op==1)
update(x,y,1,n,1);
else
printf("%d\n",query(x,y,1,n,1));
}
}
return 0;
}
HDU 3911 Black And White(线段树区间合并)
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/42563793