~by Wjvje 2019.5.7
题目描述:
Black And White
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6160 Accepted Submission(s): 1804
Problem Description
There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [i, j].
Input
There are multiple cases, the first line of each case is an integer n(1<= n <= 10^5), followed by n integer 1 or 0(1 indicates black stone and 0 indicates white stone), then is an integer M(1<=M<=10^5) followed by M operations formatted as x i j(x = 0 or 1) , x=1 means change the color of stones in range[i,j], and x=0 means ask the longest period of consecutive black stones in range[i,j]
Output
When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input
4
1 0 1 0
5
0 1 4
1 2 3
0 1 4
1 3 3
0 4 4
Sample Output
1
2
0
核心题意:
给你一段由0和1组成的序列,然后有两种操作:0 a b就是问从a到b最长的连续的1的长度为多少,
1 a b就是把从a到b的数据是一的更新为0,是零的更新为1。
思路:(线段树维护6个量)
从最左边数连续1的长度、从左边数连续0的长度、从右边数连续1的长度、从右边数连续0的长度、
连续最长的0的个数、连续最长的1的个数、flag区间更新Lazy标记。
代码实现:
#include<bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=4e5+100;
int Left[N],Right[N];
int sum0[N],sum1[N];//----------区间最长连续0/1;
int lmax0[N],rmax0[N],lmax1[N],rmax1[N];//---------最左边连续1/0,最右边...
int flag[N];//------------lazy标记,要不要取反,当取反两次,就返回自己了,就不必要取反了
int a[N];
void tree_swap(int p)
{
swap(lmax0[p],lmax1[p]);
swap(rmax0[p],rmax1[p]);
swap(sum0[p],sum1[p]);
}
void pushUp(int p) 合并
{
lmax0[p]=lmax0[2*p];//-------------首先无疑问直接等于他儿子最左边的0的连续个数
lmax1[p]=lmax1[p*2];//-------------首先无疑问直接等于他儿子最左边的1的连续个数
rmax0[p]=rmax0[2*p+1];//------同理
rmax1[p]=rmax1[2*p+1];//------同理
if(lmax0[p]==Right[2*p]-Left[2*p]+1)lmax0[p]+=lmax0[2*p+1];//---
//--当最左边的个数为满那么就得加上右子树最左边画个图就明白
if(lmax1[p]==Right[2*p]-Left[2*p]+1)lmax1[p]+=lmax1[2*p+1];
if(rmax0[p]==Right[2*p+1]-Left[2*p+1]+1)rmax0[p]+=rmax0[2*p];//---
//--当最右边1的个数为满就得加上左子树的最右边连续1的个数。
if(rmax1[p]==Right[2*p+1]-Left[2*p+1]+1)rmax1[p]+=rmax1[2*p];
sum0[p]=max(max(sum0[2*p],sum0[2*p+1]),rmax0[2*p]+lmax0[2*p+1]);
sum1[p]=max(max(sum1[2*p],sum1[2*p+1]),rmax1[2*p]+lmax1[2*p+1]);
//--最大值等于 左子树的最大,右字数的最大,左子树最右边连续1/0的个数
// +右字数最左边连续1/0的个数,三者去最大
}
void pushDown(int p)//--------------将lazy标记传递下去
{
if(flag[p])
{
flag[2*p]^=1;
flag[2*p+1]^=1;
flag[p]=0;
tree_swap(2*p);
tree_swap(2*p+1);
}
}
void build(int p,int l,int r)//--------平常线段树几乎一样
{
Left[p]=l;
Right[p]=r;
if(l==r)
{
sum0[p]=(a[l]==0);
sum1[p]=(a[l]==1);
lmax0[p]=(a[l]==0);
lmax1[p]=(a[l]==1);
rmax0[p]=(a[l]==0);
rmax1[p]=(a[l]==1);
flag[p]=0;
return ;
}
int mid=(l+r)/2;
build(2*p,l,mid);
build(2*p+1,mid+1,r);
pushUp(p);//---------回溯的时候合并
}
void change(int p,int l,int r)//------------日常更新,更新就交换区间里0和1的个数即可
{
if(l<=Left[p]&&r>=Right[p])
{
flag[p]^=1;
tree_swap(p);
return ;
}
pushDown(p);
int mid=(Left[p]+Right[p])/2;
if(l<=mid)change(2*p,l,r);
if(r>mid)change(2*p+1,l,r);
pushUp(p);
}
int query(int p,int l,int r)
{
if(l<=Left[p]&&r>=Right[p])return sum1[p];//------------查询到这个区间
int mid=(Left[p]+Right[p])/2;
pushDown(p);
if(r<=mid)return query(2*p,l,r);
else if(l>mid)return query(2*p+1,l,r);
//当要查询的区间既不全在左树,又不全在又树的时候,
//就得判断,左子树的大,还是右大,还是左子树的
else //最右边连续1的个数+上右子树最左边连续1的个数大,取三者最大
{
int tmpL=query(2*p,l,r);//-----------左子树最大连续1
int tmpR=query(2*p+1,l,r);//---------右子树最大连续一
// 中间连续1的个数 ↓
int mR=rmax1[2*p];
int mL=lmax1[2*p+1];
if(mR>mid-l+1)mR=mid-l+1;//--------左子树最右边最长连续1的个数,
//-----------不能大于他要查询的 左区间(a)到左子树最由端点的个数。
if(mL>r-mid)mL=r-mid;//-----------------------同理,,,
return max(max(tmpL,tmpR),mR+mL);
}
}
int main()
{
io;
int n,m;
while(cin>>n)
{
// memset(Left,0,sizeof(Left));
// memset(Right,0,sizeof(Right));
memset(sum0,0,sizeof(sum0)); //线段树中每次更新的只是叶子节点,so thereh is update.
memset(sum1,0,sizeof(sum1));memset(flag,0,sizeof(flag));
memset(rmax0,0,sizeof(rmax0));memset(rmax1,0,sizeof(rmax1));
memset(lmax0,0,sizeof(lmax0));memset(lmax1,0,sizeof(lmax1));
for(int i=1;i<=n;++i)cin>>a[i];
build(1,1,n);//-------------------no forget
cin>>m;
for(int i=1;i<=m;++i)
{
int x,y,z;
cin>>x>>y>>z;
if(x==0)cout<<query(1,y,z)<<endl;
else
change(1,y,z);
}
}
return 0;
}