略恶心的线段树...不过只要弄清楚了AC应该不难....

----------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define L( x ) ( ( x ) << 1 )
#define R( x ) ( L( x ) ^ 1 )
#define LC L( x ) , l , m
#define RC R( x ) , m + 1 , r
#define X x , l , r
#define XX int x , int l , int r
#define M( l , r ) ( ( l + r ) >> 1 )
#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
 
using namespace std;
 
const int maxn = 100000 + 5;
const int maxnode = 270000;
 
int n , seq[ maxn ];
 
struct Node {
int sum , l_cnt[ 2 ] , r_cnt[ 2 ] , cnt[ 2 ] , set;
bool rev;
Node() {
set = -1;
rev = false;
}
};
 
Node tree[ maxnode ];
 
int L , R , op;
 
void maintain( XX ) {
Node &o = tree[ x ];
if( r > l ) {
Node &lc = tree[ L( x ) ] , &rc = tree[ R( x ) ];
int m = M( l , r );
   o.sum = lc.sum + rc.sum;
   rep( i , 2 ) {
       o.cnt[ i ] = max( max( lc.cnt[ i ] , rc.cnt[ i ] ) , lc.r_cnt[ i ] + rc.l_cnt[ i ] );
       if( lc.l_cnt[ i ] == m - l + 1 )
           o.l_cnt[ i ] = lc.l_cnt[ i ] + rc.l_cnt[ i ];
       else
   o.l_cnt[ i ] = lc.l_cnt[ i ];
       if( rc.r_cnt[ i ] == r - m )
           o.r_cnt[ i ] = rc.r_cnt[ i ] + lc.r_cnt[ i ];
       else
   o.r_cnt[ i ] = rc.r_cnt[ i ];
   }
}
if( o.set != -1 ) {
o.sum = o.set ? r - l + 1 : 0;
o.cnt[ 1 ] = o.l_cnt[ 1 ] = o.r_cnt[ 1 ] = o.sum;
o.cnt[ 0 ] = o.l_cnt[ 0 ] = o.r_cnt[ 0 ] = r - l + 1 - o.sum;
}
if( o.rev ) {
o.sum = r - l + 1 - o.sum;
swap( o.l_cnt[ 0 ] , o.l_cnt[ 1 ] );
swap( o.r_cnt[ 0 ] , o.r_cnt[ 1 ] );
swap( o.cnt[ 0 ] , o.cnt[ 1 ] );
}
}
 
void pushdown( XX ) {
Node &o = tree[ x ] , &lc = tree[ L( x ) ] , &rc = tree[ R( x ) ];
if( o.set != -1 ) {
lc.set = rc.set = o.set;
lc.rev = rc.rev = false;
o.set = -1;
}
if( o.rev ) {
lc.rev ^= 1;
rc.rev ^= 1;
o.rev =false;
}
}
 
void update( XX ) {
Node &o = tree[ x ];
if( L <= l && r <= R ) {
if( op == 2 ) {
if( o.set != -1 ) o.set ^= 1;
   else o.rev ^= 1;
} else {
   o.set = op;
   o.rev = false;
}  
} else {
int m = M( l , r );
   pushdown( X );
   maintain( LC );
   maintain( RC );
if( L <= m ) update( LC );
if( m < R ) update( RC );
}
maintain( X );
}
 
Node merge( int l , int r , Node lc , Node rc ) {
Node o;
int m = M( l , r );
o.sum = lc.sum + rc.sum;
rep( i , 2 ) {
   o.cnt[ i ] = max( max( lc.cnt[ i ] , rc.cnt[ i ] ) , lc.r_cnt[ i ] + rc.l_cnt[ i ] );
     if( lc.l_cnt[ i ] == m - l + 1 )
   o.l_cnt[ i ] = lc.l_cnt[ i ] + rc.l_cnt[ i ];
else
o.l_cnt[ i ] = lc.l_cnt[ i ];
if( rc.r_cnt[ i ] == r - m )
   o.r_cnt[ i ] = rc.r_cnt[ i ] + lc.r_cnt[ i ];
else
o.r_cnt[ i ] = rc.r_cnt[ i ];
}
return o;
}
 
Node query( XX ) {
Node &o = tree[ x ];
if( L <= l && r <= R )
return o;
int m = M( l , r );
pushdown( X );
maintain( LC );
maintain( RC );
if( R <= m ) return query( LC );
else if( m < L ) return query( RC );
else return merge( l , r , query( LC ) , query( RC ) );
 
void build( XX ) {
Node &o = tree[ x ];
if( l == r ) {
o.set =seq[ l ];
} else {
int m = M( l , r );
build( LC );
build( RC );
}
maintain( X );
}
 
int main() {
freopen( "test.in" , "r" , stdin );
int m;
cin >> n >> m;
Rep( i , n )
   scanf( "%d" , seq + i );
build( 1 , 1 , n );
    while( m-- ) {
    scanf( "%d%d%d" , &op , &L , &R );
    L++ , R++;
    if( op < 3 ) update( 1 , 1 , n );
     else {
    Node o = query( 1 , 1 , n );
    printf( "%d\n" , op == 3 ? o.sum : o.cnt[ 1 ] );
    }
 
    }
    
return 0;
}

----------------------------------------------------------------

1858: [Scoi2010]序列操作

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1425  Solved: 737
[Submit][Status][Discuss]

Description

lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[a,b]区间内的所有数全部取反,也就是说把所有的0变成1,把所有的1变成0 3 a b 询问[a, b]区间内总共有多少个1 4 a b 询问[a, b]区间内最多有多少个连续的1 对于每一种询问操作,lxhgww都需要给出回答,聪明的程序员们,你们能帮助他吗?

Input

输入数据第一行包括2个数,n和m,分别表示序列的长度和操作数目 第二行包括n个数,表示序列的初始状态 接下来m行,每行3个数,op, a, b,(0<=op<=4,0<=a<=b

Output

对于每一个询问操作,输出一行,包括1个数,表示其对应的答案

Sample Input

10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9

Sample Output

5
2
6
5

HINT

对于30%的数据,1<=n, m<=1000
对于100%的数据,1<=n, m<=100000

Source

BZOJ 1858: [Scoi2010]序列操作( 线段树 )的更多相关文章

  1. (WAWAWAWAWAWA) BZOJ 1858: [Scoi2010]序列操作

    二次联通门 : BZOJ 1858: [Scoi2010]序列操作 /* BZOJ 1858: [Scoi2010]序列操作 已经... 没有什么好怕的的了... 16K的代码... 调个MMP啊.. ...

  2. bzoj 1858: [Scoi2010]序列操作

    1858: [Scoi2010]序列操作 Time Limit: 10 Sec  Memory Limit: 64 MB 线段树,对于每个区间需要分别维护左右和中间的1和0连续个数,并在op=4时特殊 ...

  3. bzoj1858[Scoi2010]序列操作 线段树

    1858: [Scoi2010]序列操作 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 3079  Solved: 1475[Submit][Statu ...

  4. 【bzoj1858】[Scoi2010]序列操作 线段树区间合并

    题目描述 lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b ...

  5. Luogu P2572 [SCOI2010]序列操作 线段树。。

    咕咕了...于是借鉴了小粉兔的做法ORZ... 其实就是维护最大子段和的线段树,但上面又多了一些操作....QWQ 维护8个信息:1/0的个数(sum),左/右边起1/0的最长长度(ls,rs),整段 ...

  6. bzoj1858 [Scoi2010]序列操作——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 线段树...调了一个上午...(后面带 // 的都是改出来的) lazy 标记的下放好 ...

  7. bzoj 1858: [Scoi2010]序列操作 || 洛谷 P2572

    记一下:线段树占空间是$2^{ceil(log2(n))+1}$ 这个就是一个线段树区间操作题,各种标记的设置.转移都很明确,只要熟悉这类题应该说是没有什么难度的. 由于对某区间set之后该区间原先待 ...

  8. 洛谷$P2572\ [SCOI2010]$ 序列操作 线段树/珂朵莉树

    正解:线段树/珂朵莉树 解题报告: 传送门$w$ 本来是想写线段树的,,,然后神仙$tt$跟我港可以用珂朵莉所以决定顺便学下珂朵莉趴$QwQ$ 还是先写线段树做法$QwQ$? 操作一二三四都很$eas ...

  9. [SCOI2010]序列操作 线段树

    ---题面--- 题解: 在考场上打的这道题,出人意料的很快就打完了?! 直接用线段树,维护几个东西: 1,lazy标记 : 表示区间赋值 2,mark标记:表示区间翻转 3,l1:前缀最长连续的1的 ...

随机推荐

  1. jvm 调优 工具

    1.用于打开运行中的jvm进程的gc 监控日志以及查看相关参数设置:jinfo 2.其它工具如:jps.jstack.jstat.jmap

  2. Java中单态设计模式

    Java中单态设计模式 2011-09-23 16:38:46|  分类: Java |  标签:technology!  |举报|字号 订阅     此博文是转自新浪博客中一名叫做"俊俊的 ...

  3. 手动修改VisualStudio IISExpress的配置

    <VisualStudio> <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> ...

  4. web api 开发之 filter

     1.使用filter之前应该知道的(不知道也无所谓,哈哈!) 谈到filter 不得不先了解下aop(Aspect Oriented Programming)面向切面的编程.(度娘上关于aop一大堆 ...

  5. ASCII码图

    图片转ASCII码图   效果图 基本思路 把图片每个像素点的信息拿出来,最重要的是拿到rgb的值 把每个像素点由rgb转成灰度图像,即0-255 给0-255分级,把每个等级的像素点转换成ascii ...

  6. QT5程序发布dll依赖

    QT5 发布程序,太庞大了,QT4下,基本的也就20M左右 platforms目录是 对应X:\Qt\Qt5.3.2\5.3\mingw482_32\plugins\platforms 下的qmini ...

  7. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  8. C++使用之常量的定义

    在介绍C++的常前,先看下下面的代码. for (int i = 0; i < 512; ++i) { …; } 512是什么,它具有什么含义?在代码中若直接使用类似512这些“魔数”(magi ...

  9. HDU-4866-Shooting(函数式线段树)

    Problem Description In the shooting game, the player can choose to stand in the position of [1, X] t ...

  10. tomcat的集群配置

    配置环境需要:1.Apache服务器,下载地址:http://httpd.apache.org/download.cgi#apache22 2.tomcat6.0或者tomcat7.0,(集群中tom ...