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

 
解题:线段树啊。。。幸好只有一种操作,翻转啊。。。
 
就是给你两种操作,1 a b表示将a b区里面的01翻转,0 a b表示输出a b间连续的最长的1有多少个
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int lsum[],rsum[],mx[];
bool lazy;
} tree[maxn<<];
void pushup(int v,int k) {
for(int i = ; i < ; ++i) {
tree[v].lsum[i] = tree[v<<].lsum[i];
tree[v].rsum[i] = tree[v<<|].rsum[i];
if(tree[v].lsum[i] == k - (k>>))
tree[v].lsum[i] += tree[v<<|].lsum[i];
if(tree[v].rsum[i] == (k>>))
tree[v].rsum[i] += tree[v<<].rsum[i];
tree[v].mx[i] = max(tree[v<<].mx[i],tree[v<<|].mx[i]);
tree[v].mx[i] = max(tree[v].mx[i],tree[v<<].rsum[i]+tree[v<<|].lsum[i]);
}
} void change(int v) {
swap(tree[v].lsum[],tree[v].lsum[]);
swap(tree[v].rsum[],tree[v].rsum[]);
swap(tree[v].mx[],tree[v].mx[]);
tree[v].lazy = !tree[v].lazy;
}
void pushdown(int v) {
if(tree[v].lazy) {
change(v<<);
change(v<<|);
tree[v].lazy = false;
}
}
void build(int L,int R,int v) {
tree[v].lazy = false;
if(L == R) {
int tmp;
scanf("%d",&tmp);
tree[v].mx[] = tree[v].lsum[] = tree[v].rsum[] = !tmp;
tree[v].mx[] = tree[v].lsum[] = tree[v].rsum[] = tmp;
return;
}
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid+,R,v<<|);
pushup(v,R - L + );
}
void update(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) {
change(v);
return;
}
pushdown(v);
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,v<<);
if(rt > mid) update(mid+,R,lt,rt,v<<|);
pushup(v,R - L + );
}
int query(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) return tree[v].mx[];
pushdown(v);
int mid = (L + R)>>,ret = ;
if(lt <= mid) ret = max(ret,query(L,mid,lt,rt,v<<));
if(rt > mid) ret = max(ret,query(mid+,R,lt,rt,v<<|));
if(lt <= mid && rt > mid)
ret = max(ret,min(mid - lt + ,tree[v<<].rsum[]) + min(rt - mid,tree[v<<|].lsum[]));
pushup(v,R - L + );
return ret;
}
int main() {
int n,m,op,a,b;
while(~scanf("%d",&n)){
build(,n,);
scanf("%d",&m);
while(m--){
scanf("%d %d %d",&op,&a,&b);
if(op) update(,n,a,b,);
else printf("%d\n",query(,n,a,b,));
}
}
return ;
}

HDU 3911 Black And White的更多相关文章

  1. hdu 3911 Black And White(线段树)

    题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...

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

      [题目地址] vjudge HDU [题目大意] 海滩上有一堆石头. 石头的颜色是白色或黑色. 小肥羊拥有魔术刷,她可以改变连续石的颜色,从黑变白,从白变黑. 小肥羊非常喜欢黑色,因此她想知道范围 ...

  3. 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之间 ...

  4. HDU 3911 Black And White 分段树 题解

    Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little ...

  5. hdu 3911 Black And White (线段树 区间合并)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...

  6. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  7. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  8. HDU 5113 Black And White 回溯+剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...

  9. [HDU 5113] Black And White (dfs+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:给你N*M的棋盘,K种颜色,每种颜色有c[i]个(sigma(c[i]) = N*M) ...

随机推荐

  1. HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  2. 【SSH 基础】浅谈Hibernate关系映射(3)

    继上篇博客 一对多关联映射(单向) 上面我们介绍了多对一,我们反过来看一对多不就是多对一吗?那还用再进行不同的映射吗?有什么区别吗?一对多和多对一映射原理是一致的,存储是同样的.也就是生成的数据库的表 ...

  3. 【转载】eclipse中批量修改Java类文件中引入的package包路径

    原博客地址:http://my.oschina.net/leeoo/blog/37852 当复制其他工程中的包到新工程的目录中时,由于包路径不同,出现红叉,下面的类要一个一个修改包路径,类文件太多的话 ...

  4. PostgreSQL环境中查看SQL执行计划示例

    explain   analyze ,format,buffers, format :TEXT, XML, JSON, or YAML. EXPLAIN (ANALYZE,buffers,format ...

  5. Spring SpEL in JSP and Assign SpEL value to Java variable in JSP

    Spring SpEL in JSP and Assign SpEL value to Java variable in JSP method 1 use----ServletContextAttri ...

  6. BZOJ 3639: Query on a tree VII LCT_set维护子树信息

    用 set 维护子树信息,细节较多. Code: #include <cstring> #include <cstdio> #include <algorithm> ...

  7. free---显示内存

    free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区. 语法 free(选项) 选项 -b:以Byte为单位显示内存使用情况: -k:以KB为单位显示内存使用情况: ...

  8. Unity 给FindGameObjectsWithTag排序

    GameObject[] patrol = GameObject.FindGameObjectsWithTag ("Player").OrderBy (g => g.tran ...

  9. JAVA 不同类载入器命名空间的理解

                曾经一直有这样一个疑惑: 都说在JAVA中,由不同类载入器载入的类在虚拟机中位于不同的命名空间下,不同命名空间下的类相互不可见. 这让我产生了一个迷惑:假设有一个类A使用了ja ...

  10. DOM基础及DOM操作HTML

     文件对象模型(Document Object Model.简称fr=aladdin" target="_blank">DOM).是W3C组织推荐的处理可扩展标 ...