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. Block的使用--初探Block

    看了两天的Block. 网上是有非常多解说与教程,也有讲得非常好的.这里就主要解说下我的理解与收获.欢迎拍砖. 所谓block.我觉得应该是一个闭包函数.而闭包,就是说block以及内部全部的变量生命 ...

  2. poj_2352树状数组

    因为y已经排好序了,用x坐标建立一维树状数组 #include<iostream> #include<cstdio> #include<cstring> using ...

  3. hdoj--5569--matrix(动态规划)

    matrix Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  4. 11.IntelliJ IDEA详细配置和使用教程(适用于Java开发人员)

    转自:https://blog.csdn.net/chssheng2007/article/details/79638076 前言 正所谓工欲善其事必先利其器,对开发人员而言若想提高编码效率,一款高效 ...

  5. 12.C语言控制窗口

    void main() { //创建一个窗口编号变量,寻找QQ的窗口 HWND win = FindWindowA("TXGuiFoundation", "QQ" ...

  6. java9新特性-22-总结

    1.在java 9 中看不到什么? 1.1 一个标准化和轻量级的JSON API 一个标准化和轻量级的JSON API被许多java开发人员所青睐.但是由于资金问题无法在Java 9中见到,但并不会削 ...

  7. java中移位操作

    /** * * @author SunRain *2013-10-14 8:09:50 *在最后一个移位运算中,结果没有直接付给b,而是直接打印出来,所以结果是正确的, *其他的是会被先转换成int型 ...

  8. HDU 3342 Legal or Not(判断环)

    Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so h ...

  9. OGG切换步骤

    步骤描述 提前准备好切换方案:以及其他相关人员的配合 切换至容灾数据库: (1)停止前端业务,确认目标端数据已经追平 (2)数据校验,确认数据一致 (3)停止生产库OGG进程(停止后可以直接删除) ( ...

  10. 软raid 实验

    RAID0 条带卷 2+ 100% 读写速度快,不容错 RAID1 镜像卷 2 50% 读写速度一般,容错 RAID5 带奇偶校验的条带卷 3+ (n-1)/n 读写速度快,容错,允许坏一块盘 RAI ...