Hdu3911 --- Black And White, blackandwhite

Source: Internet
Author: User

Hdu3911 --- Black And White, blackandwhite

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 1 2 3 0 1 1 3 3 0 4 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

Watermark line segment tree-interval merge

/*************************************** * *********************************> File Name: hdu3911.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstri Ng> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef 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 (tr Ee [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]. rochelle 0; tree [p]. r_0 = tree [p <1 | 1]. r_0; if (tree [p <1]. rochelle 0 = Tree [p <1]. r-tree [p <1]. l + 1) {tree [p]. rochelle 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]. rochelle 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]. rochelle 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]. rochelle 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]. rochelle 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 tr Ee [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", & x, & l, & r); if (x) {update (1, l, r);} else {printf ("% d \ n", query (1, l, r) ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.