Black And WhiteTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4340 Accepted Submission(s): 1266
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
Sample Output
|
一道较基础的区间处理问题,结果因为 ^= 写成 = WA三次 o(╯□╰)o
题意:有n个数和m此操作,操作分两种——
1,0 x y 表示把区间[x, y]里面的1改为0,0改为1;
2,1 x y 表示查询区间[x, y]最多的连续的1的个数。
节点信息 1 :记录区间从左开始的、从右开始的、整个区间 —— 最多的连续的1的个数。
节点信息 2 :记录区间从左开始的、从右开始的、整个区间 —— 最多的连续的0的个数。
修改区间时,只需交换节点信息1和信息2就可以了。
注意下传lazy标记的时候,用异或。因为连续两次对同一区间的修改是不会对该区间以及它的子区间造成影响的。至于其他的,就是lazy区间修改、合并、查询了。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 100000+10
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Tree
{
int l, r, len;
int l1, r1, s1;
//区间从左开始的、从右开始的、整个区间、最多的连续的1的个数
int l0, r0, s0;
//区间从左开始的、从右开始的、整个区间、最多的连续的0的个数
int lazy;
};
Tree tree[MAXN<<2];
void PushUp(int o)
{
tree[o].l1 = tree[ll].l1;
tree[o].r1 = tree[rr].r1;
if(tree[ll].l1 == tree[ll].len)
tree[o].l1 += tree[rr].l1;
if(tree[rr].r1 == tree[rr].len)
tree[o].r1 += tree[ll].r1;
tree[o].s1 = max(max(tree[ll].s1, tree[rr].s1), tree[ll].r1 + tree[rr].l1);
tree[o].l0 = tree[ll].l0;
tree[o].r0 = tree[rr].r0;
if(tree[ll].l0 == tree[ll].len)
tree[o].l0 += tree[rr].l0;
if(tree[rr].r0 == tree[rr].len)
tree[o].r0 += tree[ll].r0;
tree[o].s0 = max(max(tree[ll].s0, tree[rr].s0), tree[ll].r0 + tree[rr].l0);
}
void build(int o, int l, int r)
{
tree[o].l = l, tree[o].r = r;
tree[o].len = r - l + 1;
tree[o].lazy = 0;
if(l == r)
{
int x;
scanf("%d", &x);
tree[o].l1 = tree[o].r1 = tree[o].s1 = x ? 1 : 0;
tree[o].l0 = tree[o].r0 = tree[o].s0 = x ? 0 : 1;
return ;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
PushUp(o);
}
void change(int o)
{
swap(tree[o].l1, tree[o].l0);
swap(tree[o].r1, tree[o].r0);
swap(tree[o].s1, tree[o].s0);
tree[o].lazy ^= 1;//注意 是异或。。。 连续两次修改之后不能再把信息向下传了
}
void PushDown(int o)
{
if(tree[o].lazy)
{
//tree[ll].lazy = tree[rr].lazy = tree[o].lazy;
change(ll); change(rr);
tree[o].lazy = 0;
}
}
void update(int o, int L, int R)
{
if(L <= tree[o].l && R >= tree[o].r)
{
change(o);
//tree[o].lazy = 1;
return ;
}
PushDown(o);
int mid = (tree[o].l + tree[o].r) >> 1;
if(R <= mid)
update(ll, L, R);
else if(L > mid)
update(rr, L, R);
else
{
update(ll, L, mid);
update(rr, mid+1, R);
}
PushUp(o);
}
int query(int o, int L, int R)
{
if(L <= tree[o].l && R >= tree[o].r)
return tree[o].s1;
PushDown(o);
int mid = (tree[o].l + tree[o].r) >> 1;
if(R <= mid)
return query(ll, L, R);
else if(L > mid)
return query(rr, L, R);
else
{
int ans1 = query(ll, L, mid);
int ans2 = query(rr, mid+1, R);//查询时,拆分区间要注意
return max(max(ans1, ans2), min(mid-L+1, tree[ll].r1) + min(R-mid, tree[rr].l1));
}
//return max(query(ll, L, mid), query(rr, mid+1, R));
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
build(1, 1, n);
int q, op, x, y;
scanf("%d", &q);
while(q--)
{
scanf("%d%d%d", &op, &x, &y);
if(op)
update(1, x, y);
else
printf("%d\n", query(1, x, y));
}
}
return 0;
}