hdu3911---Black And White

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;
}
时间: 2024-10-14 07:03:04

hdu3911---Black And White的相关文章

hdu3911 Black And White(线段树区间合并)

题意:给一个由0,1组成的序列,有两种操作,一种是翻转给定区间的数(0->1,1->0),另一种是查询给定区间内由1组成的子串的最大长度.重点在区间合并和延迟标记. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #i

HDU 3911 Black And White(线段树区间合并)

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 wan

hdu 5113 Black And White (dfs回溯+剪枝)

Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 854    Accepted Submission(s): 218 Special Judge Problem Description In mathematics, the four color theorem, or the four color

imshow() displays a white image for a grey image

Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 range. You can convert the range yourself (but change values in the process), do an explicit cast (and potentially loose precision) or instruct Matla

hdu-5583 Kingdom of Black and White(数学,贪心,暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 65536/65536 K (Java/Others) Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frog

hdu 5583 Kingdom of Black and White(模拟,技巧)

Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog. Now N frogs are standing in a line, some of them are black, the others are white. The total strength of those frogs are calculated b

今天来说说白色white

今天来说说白色white 冬至到了,这是一个和白色有关的季节,今天来说说白色white.1:the white=the white people=whites,白人.2:egg white或者the white of egg=蛋白,蛋清.I don't like the egg white.我不喜欢吃蛋白.3:the whites of the eyes眼白,一般用作复数,因为咱们是有两只眼睛的.He stared at me ,the whites of his eyes gleaming i

xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理

异常如下: org.dom4j.DocumentException: Error on line 2 of document file:///D:/workspaces/struts2/lesson01/src/newfile.xml : White spaces are required between publicId and systemId. Nested exception: White spaces are required between publicId and systemId

白细胞white blood cell(leukocyte)

维基百科 http://zh.wikipedia.org/wiki/%E7%99%BD%E8%A1%80%E7%90%83 白细胞(拉丁语:leucocytus(从古希腊语leukós "白"和kýtos "中空"),德语:Leukozyt, 英语:white blood cell或leukocyte),是血液中一种重要的血细胞.除白细胞外,人体血液中还含有红血球.血小板和血浆. 白细胞作为免疫系统的一部分帮助身体抵抗传染病以及外来的东西.白细胞有核,能作变形运动,