HDU 3911 Black And White 线段树

Black And White

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3328    Accepted Submission(s): 1020


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

传送门:HDU 3911 Black And White

题目大意:给你长度为n的一串01序列,m次操作。n,m<=10^5。


操作有两种:
0 L R 查询区间【L,R】内最长的连续1的长度。


1 L R 翻转区间【L,R】内的数(0变1,1变0)。

题目分析:
区间合并水题。
维护每一个区间的最长前缀。最长后缀,最长子序列即可了。



代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define rt l , r , o
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson l , m , ls
#define rson m + 1 , r , rs

const int maxN = 500000 ;

int lmax[2][maxN] , mmax[2][maxN] , rmax[2][maxN] ;
int cha[maxN] ;
int L , R ;

int max ( const int X , const int Y ) {
	return X > Y ?

X : Y ; } int min ( const int X , const int Y ) { return X < Y ? X : Y ; } void swap ( int &X , int &Y ) { int tmp ; tmp = X ; X = Y ; Y = tmp ; } void Init_PushUp ( int x , int l , int r , int o ) { int m = ( l + r ) >> 1 ; lmax[x][o] = lmax[x][ls] ; if ( lmax[x][ls] == m - l + 1 ) lmax[x][o] += lmax[x][rs] ; rmax[x][o] = rmax[x][rs] ; if ( rmax[x][rs] == r - m ) rmax[x][o] += rmax[x][ls] ; mmax[x][o] = max ( mmax[x][ls] , mmax[x][rs] ) ; mmax[x][o] = max ( mmax[x][o] , rmax[x][ls] + lmax[x][rs] ) ; } void PushUp ( int l , int r , int o ) { Init_PushUp ( 0 , rt ) ; Init_PushUp ( 1 , rt ) ; } void change ( int o ) { cha[o] ^= 1 ; swap ( lmax[0][o] , lmax[1][o] ) ; swap ( mmax[0][o] , mmax[1][o] ) ; swap ( rmax[0][o] , rmax[1][o] ) ; } void PushDown ( int o ) { if ( cha[o] ) { change ( ls ) ; change ( rs ) ; cha[o] = 0 ; } } void Build ( int l , int r , int o ) { cha[o] = 0 ; if ( l == r ) { int x ; scanf ( "%d" , &x ) ; lmax[x][o] = mmax[x][o] = rmax[x][o] = 1 ; lmax[x ^ 1][o] = mmax[x ^ 1][o] = rmax[x ^ 1][o] = 0 ; return ; } int m = ( l + r ) >> 1 ; Build ( lson ) ; Build ( rson ) ; PushUp ( rt ) ; } void Update ( int l , int r , int o ) { if ( L <= l && r <= R ) { change ( o ) ; return ; } PushDown ( o ) ; int m = ( l + r ) >> 1 ; if ( L <= m ) Update ( lson ) ; if ( m < R ) Update ( rson ) ; PushUp ( rt ) ; } int Query ( int l , int r , int o ) { if ( L <= l && r <= R ) return mmax[1][o] ; PushDown ( o ) ; int m = ( l + r ) >> 1 ; int tmp1 = 0 , tmp2 = 0 , tmp3 = 0 ; if ( L <= m ) tmp1 = Query ( lson ) ; if ( m < R ) tmp2 = Query ( rson ) ; tmp3 = min ( m - L + 1 , rmax[1][ls] ) + min ( R - m , lmax[1][rs] ) ; return max ( max ( tmp1 , tmp2 ) , tmp3 ) ; } void work () { int n , m , ch ; while ( ~scanf ( "%d" , &n ) ) { Build ( 1 , n , 1 ) ; scanf ( "%d" , &m ) ; while ( m -- ) { scanf ( "%d%d%d" , &ch , &L , &R ) ; if ( 0 == ch ) printf ( "%d\n" , Query ( 1 , n , 1 ) ) ; if ( 1 == ch ) Update ( 1 , n , 1 ) ; } } } int main () { work () ; return 0 ; }



 

转载于:https://www.cnblogs.com/zhchoutai/p/7256172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值