Black And White
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3971 Accepted Submission(s): 1183
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
Source
2011 Multi-University Training Contest 8 - Host by HUST
Recommend
lcy | We have carefully selected several similar problems for you: 3914 3913 3912 3919 3916
Statistic | Submit | Discuss | Note
水题线段树-区间合并
/*************************************************************************
> File Name: hdu3911.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年02月21日 星期六 13时33分06秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 100010;
int sta[N];
struct node
{
int l_1, r_1, m_1;
int l_0, r_0, m_0;
int l, r;
int add;
}tree[N << 2];
void pushup (int p)
{
tree[p].l_1 = tree[p << 1].l_1;
tree[p].r_1 = tree[p << 1 | 1].r_1;
if (tree[p << 1].l_1 == tree[p << 1].r - tree[p << 1].l + 1)
{
tree[p].l_1 += tree[p << 1 | 1].l_1;
}
if (tree[p << 1 | 1].r_1 == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1)
{
tree[p].r_1 += tree[p << 1].r_1;
}
tree[p].m_1 = max (tree[p << 1].r_1 + tree[p << 1 | 1].l_1, max(tree[p << 1].m_1, tree[p << 1 | 1].m_1));
tree[p].l_0 = tree[p << 1].l_0;
tree[p].r_0 = tree[p << 1 | 1].r_0;
if (tree[p << 1].l_0 == tree[p << 1].r - tree[p << 1].l + 1)
{
tree[p].l_0 += tree[p << 1 | 1].l_0;
}
if (tree[p << 1 | 1].r_0 == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1)
{
tree[p].r_0 += tree[p << 1].r_0;
}
tree[p].m_0 = max (tree[p << 1].r_0 + tree[p << 1 | 1].l_0, max(tree[p << 1].m_0, tree[p << 1 | 1].m_0));
}
void pushdown (int p)
{
if (tree[p].add)
{
swap (tree[p << 1].l_1, tree[p << 1].l_0);
swap (tree[p << 1].r_1, tree[p << 1].r_0);
swap (tree[p << 1].m_1, tree[p << 1].m_0);
tree[p << 1].add ^= 1;
swap (tree[p << 1 | 1].l_1, tree[p << 1 | 1].l_0);
swap (tree[p << 1 | 1].r_1, tree[p << 1 | 1].r_0);
swap (tree[p << 1 | 1].m_1, tree[p << 1 | 1].m_0);
tree[p << 1 | 1].add ^= 1;
tree[p].add = 0;
}
}
void build (int p, int l, int r)
{
tree[p].l = l;
tree[p].r = r;
tree[p].add = 0;
if (l == r)
{
if (sta[l])
{
tree[p].l_1 = tree[p].r_1 = tree[p].m_1 = 1;
tree[p].l_0 = tree[p].r_0 = tree[p].m_0 = 0;
}
else
{
tree[p].l_1 = tree[p].r_1 = tree[p].m_1 = 0;
tree[p].l_0 = tree[p].r_0 = tree[p].m_0 = 1;
}
return;
}
int mid = (l + r) >> 1;
build (p << 1, l, mid);
build (p << 1 | 1, mid + 1, r);
pushup (p);
}
void update (int p, int l, int r)
{
if (tree[p].l == l && tree[p].r == r)
{
swap (tree[p].l_1, tree[p].l_0);
swap (tree[p].r_1, tree[p].r_0);
swap (tree[p].m_1, tree[p].m_0);
tree[p].add ^= 1;
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
pushdown (p);
if (r <= mid)
{
update (p << 1, l, r);
}
else if (l > mid)
{
update (p << 1 | 1, l, r);
}
else
{
update (p << 1, l, mid);
update (p << 1 | 1, mid + 1, r);
}
pushup (p);
}
int query (int p, int l, int r)
{
if (l == tree[p].l && tree[p].r == r)
{
return tree[p].m_1;
}
pushdown (p);
int mid = (tree[p].l + tree[p].r) >> 1;
if (r <= mid)
{
return query (p << 1, l, r);
}
else if (l > mid)
{
return query (p << 1 | 1, l, r);
}
else
{
int s = mid - tree[p << 1].r_1 + 1;
int e = mid + tree[p << 1 | 1].l_1;
if (l >= s && r <= e)
{
return (r - l + 1);
}
else if (l >= s && r > e)
{
return max (e - l + 1, query (p << 1 | 1, mid + 1, r));
}
else if (l < s && r <= e)
{
return max (r - s + 1, query (p << 1, l, mid));
}
else
{
return max (e - s + 1, max (query (p << 1, l, mid), query (p << 1 | 1, mid + 1, r)));
}
}
}
int main ()
{
int n, m;
while (~scanf("%d", &n))
{
int x, l, r;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &sta[i]);
}
build (1, 1, n);
scanf("%d", &m);
while (m--)
{
scanf("%d%d%d", &x, &l, &r);
if (x)
{
update (1, l, r);
}
else
{
printf("%d\n", query (1, l, r));
}
}
}
return 0;
}