HDU 3911 Black And White

Black And White

Time Limit: 3000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3911
64-bit integer IO format: %I64d      Java class name: Main

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

解题:线段树啊。。。幸好只有一种操作,翻转啊。。。

就是给你两种操作,1 a b表示将a b区里面的01翻转,0 a b表示输出a b间连续的最长的1有多少个

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 struct node {
 5     int lsum[2],rsum[2],mx[2];
 6     bool lazy;
 7 } tree[maxn<<2];
 8 void pushup(int v,int k) {
 9     for(int i = 0; i < 2; ++i) {
10         tree[v].lsum[i] = tree[v<<1].lsum[i];
11         tree[v].rsum[i] = tree[v<<1|1].rsum[i];
12         if(tree[v].lsum[i] == k - (k>>1))
13             tree[v].lsum[i] += tree[v<<1|1].lsum[i];
14         if(tree[v].rsum[i] == (k>>1))
15             tree[v].rsum[i] += tree[v<<1].rsum[i];
16         tree[v].mx[i] = max(tree[v<<1].mx[i],tree[v<<1|1].mx[i]);
17         tree[v].mx[i] = max(tree[v].mx[i],tree[v<<1].rsum[i]+tree[v<<1|1].lsum[i]);
18     }
19 }
20
21 void change(int v) {
22     swap(tree[v].lsum[0],tree[v].lsum[1]);
23     swap(tree[v].rsum[0],tree[v].rsum[1]);
24     swap(tree[v].mx[0],tree[v].mx[1]);
25     tree[v].lazy = !tree[v].lazy;
26 }
27 void pushdown(int v) {
28     if(tree[v].lazy) {
29         change(v<<1);
30         change(v<<1|1);
31         tree[v].lazy = false;
32     }
33 }
34 void build(int L,int R,int v) {
35     tree[v].lazy = false;
36     if(L == R) {
37         int tmp;
38         scanf("%d",&tmp);
39         tree[v].mx[0] = tree[v].lsum[0] = tree[v].rsum[0] = !tmp;
40         tree[v].mx[1] = tree[v].lsum[1] = tree[v].rsum[1] = tmp;
41         return;
42     }
43     int mid = (L + R)>>1;
44     build(L,mid,v<<1);
45     build(mid+1,R,v<<1|1);
46     pushup(v,R - L + 1);
47 }
48 void update(int L,int R,int lt,int rt,int v) {
49     if(lt <= L && rt >= R) {
50         change(v);
51         return;
52     }
53     pushdown(v);
54     int mid = (L + R)>>1;
55     if(lt <= mid) update(L,mid,lt,rt,v<<1);
56     if(rt > mid) update(mid+1,R,lt,rt,v<<1|1);
57     pushup(v,R - L + 1);
58 }
59 int query(int L,int R,int lt,int rt,int v) {
60     if(lt <= L && rt >= R) return tree[v].mx[1];
61     pushdown(v);
62     int mid = (L + R)>>1,ret = 0;
63     if(lt <= mid) ret = max(ret,query(L,mid,lt,rt,v<<1));
64     if(rt > mid) ret = max(ret,query(mid+1,R,lt,rt,v<<1|1));
65     if(lt <= mid && rt > mid)
66         ret = max(ret,min(mid - lt + 1,tree[v<<1].rsum[1]) + min(rt - mid,tree[v<<1|1].lsum[1]));
67     pushup(v,R - L + 1);
68     return ret;
69 }
70 int main() {
71     int n,m,op,a,b;
72     while(~scanf("%d",&n)){
73         build(1,n,1);
74         scanf("%d",&m);
75         while(m--){
76             scanf("%d %d %d",&op,&a,&b);
77             if(op) update(1,n,a,b,1);
78             else printf("%d\n",query(1,n,a,b,1));
79         }
80     }
81     return 0;
82 }

时间: 2024-11-23 14:11:18

HDU 3911 Black And White的相关文章

hust 1546 &amp;&amp; hdu 3911 Black And White

题目描述 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 l

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 3911 Black And White(线段树)

题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线段树的一种题型,区间合并,因为有一个取反的操作,所以对于每个节点要维护6个值,包括连续0,1最长序列的长度,左边和右边的最长连续长度.需要注意的是,如果询问的区间最大值是从R[lson] + L[rson]来到,要判断是否比长度大于r - l + 1.一开始没注意,所以WA了,上网搜了下别人的题解,

HDU 3911 Black and White (线段树,区间翻转)

  [题目地址] vjudge HDU [题目大意] 海滩上有一堆石头. 石头的颜色是白色或黑色. 小肥羊拥有魔术刷,她可以改变连续石的颜色,从黑变白,从白变黑. 小肥羊非常喜欢黑色,因此她想知道范围[i,j]中连续的黑色石头的最长时间. 有多种情况,每种情况的第一行是整数n(1 <= n <= 10 ^ 5),后跟n个整数1或0(1表示黑石头,0表示白石头),然后是整数 M(1 <= M <= 10 ^ 5)后跟M个运算,格式为xij(x = 0或1),x = 1表示更改范围[i

HDU 3911 Black And White (线段树区间合并 + lazy标记)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 给你n个数0和1,m个操作: 0操作  输出l到r之间最长的连续1的个数 1操作  将l到r之间的0变1,1变0 区间合并的模版题,结构体中的lsum1表示从此区间最左端开始连续1的个数,rsum1表示从此区间最右端开始连续1的个数,sum1表示此区间连续1的个数最长是多少.lsum0,rsum0,sum0也是如此.每一次1的操作将区间内lazy标记与1异或一次,异或两次就说明操作抵消了.然后

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 3911 区间合并求最大长度的问题

http://vjudge.net/problem/viewProblem.action?id=21557 题目大意: 每进行一次颜色改变都可以把一段区间内的黑石头变成白石头,白石头变成黑石头,最后问区间内黑石头连续的最大长度 这里我们可以用rev[]作为lazy标记,每次进行改变,rev[]^1 因为有黑白两种石头,我们求连续区间,需要维护黑,白两种石头的左侧最多,右侧最多和全部最多,所以我们这里可以用一个二维数组进行描述 每次做出改变,只要将黑白石头的值进行交换即可就方便了很多 对于最后访问

hdu 1802 Black and white painting(置换群)

题目链接:hdu 1802 Black and white painting 题意: 有一个n*n的格子,然后用c种颜色去涂,问你有多少种方案. 能旋转,反射的算一种方案. 题解: polya定理的经典运用 旋转只有 0,90,180,270度三种旋法.旋0度,则置换的轮换数为n*n旋90度,n为偶数时,则置换的轮换数为n*n/4,n为奇数,则置换的轮换数为(n*n-1)/4+1旋180度,n为偶数时,则置换的轮换数为n*n/2,n为奇数,则置换的轮换数为(n*n-1)/2+1旋270度,n为偶

HDU 5113 Black And White(DFS+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题面: Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 350 Special Judge Problem Description I