略恶心的线段树...不过只要弄清楚了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. Mysql的四种分区

    mysql一共有四大分区分别为hash range list key 四个分区. 分区的字段需要时主键才可以成功 . 第一种 hash分区 第二张list分区 第三种 key分区 第四种 range分 ...

  2. 高性能javascript 学习笔记(1)

    加载和运行 管理浏览器中的javascript代码是个棘手的问题,因为代码运行阻塞了其他浏览器处理过程,诸如用户绘制,每次遇到<script>标签,页面必须停下来等待代码下载(如果是外部的 ...

  3. poj 2398 计算几何

    #include <iostream> #include<cstdio> #include<cstring> #include <algorithm> ...

  4. (iOS)viewController背景透明化

    #ifdef __IPHONE_8_0 ) { [UIApplication sharedApplication].keyWindow.rootViewController.providesPrese ...

  5. ISO/IEC 14443协议浅谈

    一. 非接触IC卡简介 非接触IC卡又称射频卡,是射频识别技术和IC卡技术有机结合的产物.它解决了无源(卡中无电源)和免接触这一难题,具有更加方便.快捷的特点,广泛用于电子支付.通道控制.公交收费.停 ...

  6. Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集

     Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集 继去年 10 月推出 Windows Azure HDInsight 之后,我们宣布 Windows Az ...

  7. [置顶] js操作iframe兼容各种浏览器

    在做项目时,遇到了操作iframe的相关问题.业务很简单,其实就是在操作iframe内部某个窗体时,调用父窗体的一个函数.于是就写了两个很简单的htm页面用来测试,使用网上流行的方法在谷歌浏览器中始终 ...

  8. android 中文 api (72) —— BluetoothSocket[蓝牙]

    前言 本章内容是   android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译.蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过 ...

  9. Ext JS学习第十五天 Ext基础之 Ext.DomQuery

    此文同来记录学习笔记 •Ext.dom.Query 嗯,这个类一看就是到时做什么事儿的,不用我去过多的解释了.这个类一共提供了8个方法供开发人员去使用. •要说最常用的方法,无非就是Ext.query ...

  10. 修改页面中所有TextBox控件的样式--CSS

    1.HTML <div> TextBox<br /> <input type="text" name="name" value=& ...