[SCOI2010]序列操作

题目链接

        比之前那个区间最多公约数简单,结构体里维护一下区间0,1的个数,区间最长0,1连续串长度,区间左右端点最长连续0,1串长度即可,0,1都维护是为了翻转操作。

        对于翻转标记的lz数组需要分类讨论,对于合并区间的时候跨区间要单独讨论。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
//#define int long long
//const ll P=2281701377;
const ll P=998244353;
const int mod=1e9+7;
#define fi first
#define se second


struct node{
	int cnt[2];
	int maxx[2];
	int pre[2],nex[2];
	
}tree[N*4];
int n,m;
int a[N];
int lz[N*4];

void pushup(int p,int l,int r){
	int mid=(l+r)>>1;
	tree[p].cnt[0]=tree[p<<1].cnt[0]+tree[p<<1|1].cnt[0];
	tree[p].cnt[1]=tree[p<<1].cnt[1]+tree[p<<1|1].cnt[1];
	
	tree[p].maxx[0]=max(tree[p<<1].maxx[0],tree[p<<1|1].maxx[0]);
	tree[p].maxx[0]=max(tree[p].maxx[0],tree[p<<1].nex[0]+tree[p<<1|1].pre[0]);
	tree[p].maxx[1]=max(tree[p<<1].maxx[1],tree[p<<1|1].maxx[1]);
	tree[p].maxx[1]=max(tree[p].maxx[1],tree[p<<1].nex[1]+tree[p<<1|1].pre[1]);
	
	tree[p].pre[0]=tree[p<<1].pre[0];
	if(tree[p<<1].pre[0]==mid-l+1)
		tree[p].pre[0]+=tree[p<<1|1].pre[0];
	tree[p].pre[1]=tree[p<<1].pre[1];
	if(tree[p<<1].pre[1]==mid-l+1)
		tree[p].pre[1]+=tree[p<<1|1].pre[1];
		
	tree[p].nex[0]=tree[p<<1|1].nex[0];
	if(tree[p<<1|1].nex[0]==r-mid)
		tree[p].nex[0]+=tree[p<<1].nex[0];
	tree[p].nex[1]=tree[p<<1|1].nex[1];
	if(tree[p<<1|1].nex[1]==r-mid)
		tree[p].nex[1]+=tree[p<<1].nex[1];
}

void pushdown(int p,int l,int r){
	int mid=(l+r)>>1;
	int op=lz[p];
	if(op==1){
		tree[p<<1].cnt[1]=tree[p<<1].maxx[1]=tree[p<<1].nex[1]=tree[p<<1].pre[1]=0;
		tree[p<<1].cnt[0]=tree[p<<1].maxx[0]=tree[p<<1].nex[0]=tree[p<<1].pre[0]=mid-l+1;
		
		tree[p<<1|1].cnt[1]=tree[p<<1|1].maxx[1]=tree[p<<1|1].nex[1]=tree[p<<1|1].pre[1]=0;
		tree[p<<1|1].cnt[0]=tree[p<<1|1].maxx[0]=tree[p<<1|1].nex[0]=tree[p<<1|1].pre[0]=r-mid;
		
		lz[p<<1]=lz[p<<1|1]=op;
	}
	else if(op==2){
		tree[p<<1].cnt[1]=tree[p<<1].maxx[1]=tree[p<<1].nex[1]=tree[p<<1].pre[1]=mid-l+1;
		tree[p<<1].cnt[0]=tree[p<<1].maxx[0]=tree[p<<1].nex[0]=tree[p<<1].pre[0]=0;
		
		tree[p<<1|1].cnt[1]=tree[p<<1|1].maxx[1]=tree[p<<1|1].nex[1]=tree[p<<1|1].pre[1]=r-mid;
		tree[p<<1|1].cnt[0]=tree[p<<1|1].maxx[0]=tree[p<<1|1].nex[0]=tree[p<<1|1].pre[0]=0;
		
		lz[p<<1]=lz[p<<1|1]=op;
	}
	else{
		swap(tree[p<<1].cnt[0],tree[p<<1].cnt[1]);
		swap(tree[p<<1].maxx[0],tree[p<<1].maxx[1]);
		swap(tree[p<<1].pre[0],tree[p<<1].pre[1]);
		swap(tree[p<<1].nex[0],tree[p<<1].nex[1]);
		
		swap(tree[p<<1|1].cnt[0],tree[p<<1|1].cnt[1]);
		swap(tree[p<<1|1].maxx[0],tree[p<<1|1].maxx[1]);
		swap(tree[p<<1|1].pre[0],tree[p<<1|1].pre[1]);
		swap(tree[p<<1|1].nex[0],tree[p<<1|1].nex[1]);
		
		if(lz[p<<1]==0)
		lz[p<<1]=op;
		else if(lz[p<<1]==1)
		lz[p<<1]=2;
		else if(lz[p<<1]==2)
		lz[p<<1]=1;
		else
		lz[p<<1]=0;
		
		if(lz[p<<1|1]==0)
		lz[p<<1|1]=op;
		else if(lz[p<<1|1]==1)
		lz[p<<1|1]=2;
		else if(lz[p<<1|1]==2)
		lz[p<<1|1]=1;
		else
		lz[p<<1|1]=0;
	}
	lz[p]=0;
}

void build(int p,int l,int r){
	tree[p]={0,0,0,0,0,0};
	lz[p]=0;
	if(l==r){
		if(a[l]){
			tree[p].cnt[1]=tree[p].maxx[1]=tree[p].pre[1]=tree[p].nex[1]=1;
		}
		else{
			tree[p].cnt[0]=tree[p].maxx[0]=tree[p].pre[0]=tree[p].nex[0]=1;
		}
		return;
	}
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	pushup(p,l,r);
	
}

void modify(int p,int l,int r,int x,int y,int op){
	if(x<=l&&y>=r){
		if(op==1){
			tree[p].cnt[1]=tree[p].maxx[1]=tree[p].pre[1]=tree[p].nex[1]=0;
			tree[p].cnt[0]=tree[p].maxx[0]=tree[p].pre[0]=tree[p].nex[0]=r-l+1;
			lz[p]=op;
		}
		else if(op==2){
			tree[p].cnt[1]=tree[p].maxx[1]=tree[p].pre[1]=tree[p].nex[1]=r-l+1;
			tree[p].cnt[0]=tree[p].maxx[0]=tree[p].pre[0]=tree[p].nex[0]=0;
			lz[p]=op;
		}
		else{
			swap(tree[p].cnt[0],tree[p].cnt[1]);
			swap(tree[p].maxx[0],tree[p].maxx[1]);
			swap(tree[p].pre[0],tree[p].pre[1]);
			swap(tree[p].nex[0],tree[p].nex[1]);
			if(lz[p]==0)
			lz[p]=op;
			else if(lz[p]==1)
			lz[p]=2;
			else if(lz[p]==2)
			lz[p]=1;
			else
			lz[p]=0;
		}
		return;
	}
	if(lz[p])
		pushdown(p,l,r);
	int mid=(l+r)>>1;
	if(x<=mid) modify(p<<1,l,mid,x,y,op);
	if(y>mid) modify(p<<1|1,mid+1,r,x,y,op);
	pushup(p,l,r);
}

int querycnt(int p,int l,int r,int x,int y){
	if(l>=x&&y>=r){
		return tree[p].cnt[1];
	}
	if(lz[p])
		pushdown(p,l,r);
	int mid=(l+r)>>1;
	int res=0;
	if(x<=mid) res+=querycnt(p<<1,l,mid,x,y);
	if(y>mid) res+=querycnt(p<<1|1,mid+1,r,x,y);
	pushup(p,l,r);
	return res;
}

int querymax(int p,int l,int r,int x,int y){
	if(l>=x&&y>=r){
		return tree[p].maxx[1];
	}
	if(lz[p])
		pushdown(p,l,r);
	int mid=(l+r)>>1;
	int res=0;
	if(y<=mid) res=max(res,querymax(p<<1,l,mid,x,y));
	else if(x>mid) res=max(res,querymax(p<<1|1,mid+1,r,x,y));
	else{
		int res1=querymax(p<<1,l,mid,x,y);
		int res2=querymax(p<<1|1,mid+1,r,x,y);
		res=max(res1,res2);
		res=max(res,min(mid-x+1,tree[p<<1].nex[1])+min(y-mid,tree[p<<1|1].pre[1]));
	}
	pushup(p,l,r);
	return res;
}

void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	build(1,1,n);
	while(m--){
		int op,a,b;
		cin>>op>>a>>b;
		a++,b++;
		op++;
		if(op==1||op==2||op==3){
			modify(1,1,n,a,b,op);
		}
		else if(op==4){
			cout<<querycnt(1,1,n,a,b)<<endl;
		}
		else{
			cout<<querymax(1,1,n,a,b)<<endl;
		}
	}
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/771796.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Zoom使用的基本步骤和注意事项

Zoom是一款功能强大的视频会议软件&#xff0c;广泛应用于远程办公、在线教育、团队协作等多个场景。以下是Zoom使用的基本步骤和注意事项&#xff1a; 一、注册与登录 注册Zoom账户&#xff1a; 访问Zoom官方网站&#xff08;如zoom.us&#xff09;&#xff0c;点击“注册”…

Games101学习笔记 Lecture16 Ray Tracing 4 (Monte Carlo Path Tracing)

Lecture16 Ray Tracing 4 (Monte Carlo Path Tracing 一、蒙特卡洛积分 Monte Carlo Integration二、路径追踪 Path tracing1.Whitted-Style Ray Tracings Problems2.只考虑直接光照时3.考虑全局光照①考虑物体的反射光②俄罗斯轮盘赌 RR &#xff08;得到正确shade函数&#x…

精准畜牧业:多维传感监测及分析动物采食行为

全球畜牧业呈现出一个动态且复杂的挑战。近几十年来&#xff0c;它根据对动物产品需求的演变进行了适应&#xff0c;动物生产系统需要提高其效率和环境可持续性。在不同的畜牧系统中有效行动取决于科学技术的进步&#xff0c;这允许增加照顾动物健康和福祉的数量。精准畜牧业技…

JavaScript-WebAPI

文章目录 JS组成什么是 webApis 和APIDOM 简介document 对象 获取 DOM 对象利用css选择器来获取DOM元素选择指定css选择器的所有元素其他获取DOM元素方法&#xff08;了解&#xff09; 操作元素内容对象.innerText对象.innerHTML 操作元素属性操作元素常用属性操作元素样式属性…

pytorch中的contiguous()

官方文档&#xff1a;https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html 其描述contiguous为&#xff1a; Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory forma…

羊大师:羊奶养生,解锁健康之道的新密码

在探寻健康与养生的旅途中&#xff0c;我们总渴望找到那把开启健康之门的钥匙。而今&#xff0c;羊奶以其独特的营养价值和健康益处&#xff0c;正悄然成为那把解锁健康之道的新密码。 羊奶&#xff0c;自古以来便是自然赋予的珍贵礼物。它富含优质蛋白、多种维生素及矿物质&am…

pandas数据分析(6)

算数运算 和Numpy数组一样&#xff0c;DataFrame和Series也利用了向量化技术。例如&#xff1a; 不过pandas真正强大之初在于自动对齐机制&#xff1a;当对多个DataFrame使用算数运算符时&#xff0c;pandas会自动将它们按照列或行索引对齐。 结果DataFrame的索引和列是两个Da…

day02-统计数据

numpy统计学 1.求平均值[数组名.mean()/np.mean(数组名)] m1 np.arange(20).reshape((4,5))m1.mean() #9.5若想要求某一维的平均值&#xff0c;设置axis参数&#xff0c;多维数组元素指定&#xff1a; axis 0&#xff0c;将从上往下计算。axis 1&#xff0c;将从左往右计算…

VIM介绍

VIM&#xff08;Vi IMproved&#xff09;是一种高度可配置的文本编辑器&#xff0c;用于有效地创建和更改任何类型的文本。它是从 vi 编辑器发展而来的&#xff0c;后者最初是 UNIX 系统上的一个文本编辑器。VIM 以其键盘驱动的界面和强大的文本处理能力而闻名&#xff0c;是许…

拼接各列内容再分组统计

某个表格的第1列是人名&#xff0c;后面多列是此人某次采购的产品&#xff0c;一个人一次可以采购多个同样的产品&#xff0c;也可以多次采购。 ABCD1JohnAppleAppleOrange2PaulGrape3JohnPear4SteveLycheeGrape5JessicaApple 需要整理成交叉表&#xff0c;上表头是产品&…

透过 Go 语言探索 Linux 网络通信的本质

大家好&#xff0c;我是码农先森。 前言 各种编程语言百花齐放、百家争鸣&#xff0c;但是 “万变不离其中”。对于网络通信而言&#xff0c;每一种编程语言的实现方式都不一样&#xff1b;但其实&#xff0c;调用的底层逻辑都是一样的。linux 系统底层向上提供了统一的 Sock…

openlayers中区域掩膜的实现

概述 在前文完成了mapboxGL中区域掩膜的实现。近日有人问到说在openlayers中如何实现&#xff0c;本文就带大家看看如何在openlayers中实现区域掩膜。 实现效果 实现 1. 实现思路 在地图容器中添加一个canvas&#xff0c;设置其在map之上&#xff1b;监听map的postrender事…

Vue2-Vue Router前端路由实现思路

1.路由是什么&#xff1f; Router路由器&#xff1a;数据包转发设备&#xff0c;路由器通过转发数据包&#xff08;数据分组&#xff09;来实现网络互连 Route路由&#xff1a;数据分组从源到目的地时&#xff0c;决定端到端路径的网络范围的进程 | - 网络层 Distribute分发…

时空预测+特征分解!高性能!EMD-Transformer和Transformer多变量交通流量时空预测对比

时空预测特征分解&#xff01;高性能&#xff01;EMD-Transformer和Transformer多变量交通流量时空预测对比 目录 时空预测特征分解&#xff01;高性能&#xff01;EMD-Transformer和Transformer多变量交通流量时空预测对比效果一览基本介绍程序设计参考资料 效果一览 基本介绍…

顶级10大AI测试工具

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Oracle Database 23ai新特性:DB_DEVELOPER_ROLE角色

角色介绍 从 Oracle Database 23ai 开始&#xff0c;新角色“DB_DEVELOPER_ROLE”允许管理员快速分配开发人员为 Oracle 数据库设计、构建和部署应用程序所需的所有必要权限。&#xff08;包括构建数据模型所需的系统权限以及监视和调试应用程序所需的对象权限&#xff09;。通…

【数据结构】02.顺序表

一、顺序表的概念与结构 1.1线性表 线性表&#xff08;linear list&#xff09;是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中广泛使用的数据结构&#xff0c;常见的线性表&#xff1a;顺序表、链表、栈、队列、字符串… 线性表在逻辑上是线性结构&#xff0…

上位机图像处理和嵌入式模块部署(mcu项目1:实现协议)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 这种mcu的嵌入式模块理论上都是私有协议&#xff0c;因为上位机和下位机都是自己开发的&#xff0c;所以只需要自己保证上、下位机可以通讯上&…

ELK 企业实战7

ELKkafkafilebeat企业内部日志分析系统 1、组件介绍 1、Elasticsearch&#xff1a; 是一个基于Lucene的搜索服务器。提供搜集、分析、存储数据三大功能。它提供了一个分布式多用户能力的全文搜索引擎&#xff0c;基于RESTful web接口。Elasticsearch是用Java开发的&#xff…

[数据集][目标检测]刀具匕首持刀检测数据集VOC+YOLO格式8810张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;8810 标注数量(xml文件个数)&#xff1a;8810 标注数量(txt文件个数)&#xff1a;8810 标注…