题目描述

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都需要给出回答,聪明的程序员们,你们能帮助他吗?

输入

输入数据第一行包括2个数,n和m,分别表示序列的长度和操作数目 第二行包括n个数,表示序列的初始状态 接下来m行,每行3个数,op, a, b,(0<=op<=4,0<=a<=b<n)表示对于区间[a, b]执行标号为op的操作="" <="" div="">

输出

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

样例输入

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

样例输出

5
2
6
5

提示

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


题解

线段树区间合并

裸题,重要的是两种标记冲突的处理:

pushdown时先rever,再change,在rever时把儿子的标记(pushdown时)和自己的标记(update时)也翻转了。

可以想到,如果题目中是先rever再change,则不会有影响。

如果题目中是先change再rever,则再update函数中直接将change标记改变,也相当于没有任何变化。

标记遇到标记等等,同理。

还有,(-1)^1!=-1。如果按照我的代码,tag数组里0是变成0,1是变成1,-1是不变,那么一定要在rever改变标记时注意是否有change标记。

然后这题就变成了水题。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
int sum[400001][2] , tag[400001] , rev[400001] , ln[400001][2] , rn[400001][2] , tn[400001][2];
void pushup(int l , int r , int x)
{
int mid = (l + r) >> 1 , i;
for(i = 0 ; i <= 1 ; i ++ )
{
sum[x][i] = sum[x << 1][i] + sum[x << 1 | 1][i];
tn[x][i] = max(rn[x << 1][i] + ln[x << 1 | 1][i] , max(tn[x << 1][i] , tn[x << 1 | 1][i]));
ln[x][i] = (ln[x << 1][i] == mid - l + 1) ? ln[x << 1][i] + ln[x << 1 | 1][i] : ln[x << 1][i];
rn[x][i] = (rn[x << 1 | 1][i] == r - mid) ? rn[x << 1 | 1][i] + rn[x << 1][i] : rn[x << 1 | 1][i];
}
}
void pushdown(int l , int r , int x)
{
int mid = (l + r) >> 1;
if(rev[x])
{
swap(sum[x << 1][0] , sum[x << 1][1]);
swap(sum[x << 1 | 1][0] , sum[x << 1 | 1][1]);
swap(ln[x << 1][0] , ln[x << 1][1]);
swap(ln[x << 1 | 1][0] , ln[x << 1 | 1][1]);
swap(rn[x << 1][0] , rn[x << 1][1]);
swap(rn[x << 1 | 1][0] , rn[x << 1 | 1][1]);
swap(tn[x << 1][0] , tn[x << 1][1]);
swap(tn[x << 1 | 1][0] , tn[x << 1 | 1][1]);
if(tag[x << 1] != -1) tag[x << 1] ^= 1;
if(tag[x << 1 | 1] != -1) tag[x << 1 | 1] ^= 1;
rev[x << 1] ^= 1;
rev[x << 1 | 1] ^= 1;
rev[x] = 0;
}
if(tag[x] != -1)
{
sum[x << 1][tag[x]] = ln[x << 1][tag[x]] = rn[x << 1][tag[x]] = tn[x << 1][tag[x]] = mid - l + 1;
sum[x << 1][tag[x] ^ 1] = ln[x << 1][tag[x] ^ 1] = rn[x << 1][tag[x] ^ 1] = tn[x << 1][tag[x] ^ 1] = 0;
sum[x << 1 | 1][tag[x]] = ln[x << 1 | 1][tag[x]] = rn[x << 1 | 1][tag[x]] = tn[x << 1 | 1][tag[x]] = r - mid;
sum[x << 1 | 1][tag[x] ^ 1] = ln[x << 1 | 1][tag[x] ^ 1] = rn[x << 1 | 1][tag[x] ^ 1] = tn[x << 1 | 1][tag[x] ^ 1] = 0;
tag[x << 1] = tag[x << 1 | 1] = tag[x];
tag[x] = -1;
}
}
void build(int l , int r , int x)
{
if(l == r)
{
int t;
scanf("%d" , &t);
sum[x][t] = ln[x][t] = rn[x][t] = tn[x][t] = 1;
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(l , r , x);
}
void update(int b , int e , int a , int l , int r , int x)
{
if(b <= l && r <= e)
{
if(a == 2)
{
swap(sum[x][0] , sum[x][1]);
swap(ln[x][0] , ln[x][1]);
swap(rn[x][0] , rn[x][1]);
swap(tn[x][0] , tn[x][1]);
if(tag[x] != -1) tag[x] ^= 1;
rev[x] ^= 1;
}
else
{
sum[x][a] = ln[x][a] = rn[x][a] = tn[x][a] = r - l + 1;
sum[x][a ^ 1] = ln[x][a ^ 1] = rn[x][a ^ 1] = tn[x][a ^ 1] = 0;
tag[x] = a;
}
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) update(b , e , a , lson);
if(e > mid) update(b , e , a , rson);
pushup(l , r , x);
}
int querysum(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e)
return sum[x][1];
pushdown(l , r , x);
int mid = (l + r) >> 1 , ans = 0;
if(b <= mid) ans += querysum(b , e , lson);
if(e > mid) ans += querysum(b , e , rson);
return ans;
}
int querytot(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e)
return tn[x][1];
pushdown(l , r , x);
int mid = (l + r) >> 1 , ansl , ansr , ansm;
if(e <= mid) return querytot(b , e , lson);
else if(b > mid) return querytot(b , e , rson);
else
{
ansl = querytot(b , e , lson);
ansr = querytot(b , e , rson);
ansm = min(mid - b + 1 , rn[x << 1][1]) + min(e - mid , ln[x << 1 | 1][1]);
return max(max(ansl , ansr) , ansm);
}
}
int main()
{
int n , m , p , x , y;
scanf("%d%d" , &n , &m);
memset(tag , -1 , sizeof(tag));
build(1 , n , 1);
while(m -- )
{
scanf("%d%d%d" , &p , &x , &y);
x ++ ;
y ++ ;
if(p <= 2) update(x , y , p , 1 , n , 1);
else if(p == 3) printf("%d\n" , querysum(x , y , 1 , n , 1));
else printf("%d\n" , querytot(x , y , 1 , n , 1));
}
return 0;
}

【bzoj1858】[Scoi2010]序列操作 线段树区间合并的更多相关文章

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

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

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

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

  3. 【BZOJ-1858】序列操作 线段树

    1858: [Scoi2010]序列操作 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1961  Solved: 991[Submit][Status ...

  4. BZOJ 1858: [Scoi2010]序列操作( 线段树 )

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

  5. 【BZOJ-2962】序列操作 线段树 + 区间卷积

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 678  Solved: 246[Submit][Status][Discuss] ...

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

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

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

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

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

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

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

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

随机推荐

  1. 20145234黄斐《Java程序设计》第十周学习总结

    教材学习内容总结 网络概述 概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出现,所以在实际的开发中网络编程技术获得了大量的使用. 计算机网络概述 IP地址: ...

  2. 6、Java并发编程:volatile关键字解析

    Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...

  3. Redis系列六 Redis事务

    Redis事务 1.介绍 在Redis事务中可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞. 2.事务的作用 一个队列中, ...

  4. python函数参数默认值及重要警告

    最有用的形式是对一个或多个参数指定一个默认值.这样创建的函数,可以用比定义时允许的更少的参数调用,比如: def ask_ok(prompt, retries=4, reminder='Please ...

  5. [CF294B]Shaass and Bookshelf

    问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀 ...

  6. Spring Boot下的lombok安装 (日志) 不能识别log变量问题

    参考地址:http://blog.csdn.net/blueheart20/article/details/52909775 ps:除了要加载依赖之外 还要安装lombok插件

  7. lintcode204 单例

    单例   单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们应 ...

  8. appium启动APP配置参数:

    一.Android启动app   python启动脚本如下:   from appium import webdriver   desired_caps = {} desired_caps['plat ...

  9. 【第一章】Shell 概述

    一.什么是Shell? shell是一个命令解释器,它不仅包含大量的命令以实现操作系统的对话,还可以实现定义变量.条件判断.循环控制.函数调用等功能. 作用:解释执行用户输入的命令及程序等. 从键盘输 ...

  10. [c++] Getting Started - CMake

    CMake is an open-source cross platform build system, according to CMake's creator, Kitware. But CMak ...