HDU 3397 Sequence operation(线段树)

STM32F103 超声波风速风向仪实战:0.1m/s精度风洞标定与OLED显示(附源码) 本文详细介绍了基于STM32F103的超声波风速风向仪设计与实现,通过超声波时间差测量技术实现0.1m/s高精度测量。文章涵盖硬件设计、算法实现、风洞标定及OLED显示等关键环节,提供完整源码,适用于气象监测、风力发电等领域。 阅读详情

HDU 3397 Sequence operation

题目链接

题意:给定一个01序列,有5种操作
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最长的长度

思路:线段树线段合并,需要两个延迟标记一个置为01,一个翻转,然后由于4操作,需要记录左边最长0、1,右边最长0、1,区间最长0、1,然后区间合并去搞即可

代码:

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

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)
const int N = 100005;

int t, n, m;

struct Node {
	int l, r, sum[3][2], num;
	int setv, flip;
	Node() {
		setv = -1;
		flip = 0;
	}
	int size() {return r - l + 1;}
	void gao1(int v) {
		setv = v;
		num = v * (r - l + 1);
		for (int i = 0; i < 3; i++) {
			sum[i][v] = r - l + 1;
			sum[i][!v] = 0;
		}
		flip = 0;
	}
	void gao2() {
		flip ^= 1;
		for (int i = 0; i < 3; i++)
			swap(sum[i][0], sum[i][1]);
		num = r - l + 1 - num;
	}
} node[N * 4];

Node merge(Node lson, Node rson) {
	Node x;
	x.l = lson.l; x.r = rson.r;
	for (int i = 0; i < 2; i++) {
		x.sum[0][i] = lson.sum[0][i];
		x.sum[1][i] = rson.sum[1][i];
		x.sum[2][i] = max(lson.sum[2][i], rson.sum[2][i]);
		if (lson.sum[0][i] == lson.size())
			x.sum[0][i] += rson.sum[0][i];
		if (rson.sum[1][i] == rson.size())
			x.sum[1][i] += lson.sum[1][i];
		x.sum[2][i] = max(x.sum[2][i], lson.sum[1][i] + rson.sum[0][i]);
	}
	x.num = lson.num + rson.num;
	return x;
}

void pushup(int x) {
	node[x] = merge(node[lson(x)], node[rson(x)]);
}

void pushdown(int x) {
	if (node[x].setv != -1) {
		node[lson(x)].gao1(node[x].setv);
		node[rson(x)].gao1(node[x].setv);
		node[x].setv = -1;
	}
	if (node[x].flip) {
		node[lson(x)].gao2();
		node[rson(x)].gao2();
		node[x].flip = 0;
	}
}

void build(int l, int r, int x = 0) {
	node[x].l = l; node[x].r = r;
	node[x].setv = -1; node[x].flip = 0;
	if (l == r) {
		int tmp;
		scanf("%d", &tmp);
		for (int i = 0; i < 3; i++) {
			node[x].sum[i][tmp] = 1;
			node[x].sum[i][!tmp] = 0;
		}
		node[x].num = tmp;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
	pushup(x);
}

void add(int l, int r, int v, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		if (v == 2) node[x].gao2();
		else node[x].gao1(v);
		return;
	}
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	if (l <= mid) add(l, r, v, lson(x));
	if (r > mid) add(l, r, v, rson(x));
	pushup(x);
}

Node query(int l, int r, int x = 0) {
	if (node[x].l >= l && node[x].r <= r)
		return node[x];
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	Node ans;
	if (l <= mid && r > mid) ans = merge(query(l, r, lson(x)), query(l, r, rson(x)));
	else if (l <= mid) ans = query(l, r, lson(x));
	else if (r > mid) ans = query(l, r, rson(x));
	pushup(x);
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		build(0, n - 1);
		int op, a, b;
		while (m--) {
			scanf("%d%d%d", &op, &a, &b);
			if (op <= 2) add(a, b, op);
			else if (op == 3) printf("%d\n", query(a, b).num);
			else if (op == 4) printf("%d\n", query(a, b).sum[2][1]);
		}
	}
	return 0;
}


SAP OData 开发从入门到提高教程的目录 笔者 18 年的 SAP 技术开发生涯,对 SAP OData 服务的开发,测试,发布,部署,测试,及基于各种不同 SAP 技术实现的 OData 服务的幕后技术实现细节和使用场合,均有着深入的研究。 阅读详情

相关推荐

从零开始的计网学习——物理层

计算机网络对于IT人不论是就业还是升学都是核心的一门课程,一起从零开始,学习计算机网络!

Nimrod__的博客 2万+

HDU3397Sequence operation线段树解法

题:点击打开链接 分析:线段树区间更新。只不过掺杂了区间和、最大连续区间区间和。对于延迟标记在上一篇博客已经出现过:pojHelp with Intervals线段树解法 代码: #include #include #define lc (rt<<1) #define rc (rt<<1|1) #define lson l, m, lc #define rson m + 1, r, rc u

zxlove 1429

STM32测测速---编码电机读取速度的计算

速度计算:轮胎每转一圈的脉冲数取决于编码器的分辨率,可由下面公式进行计算:PPR是电机的线数最后可以计算轮胎每转一圈的脉冲数为:轮胎每转一圈的脉冲数 = 9.6 × 11 × 4 = 422.4最后的速度公式简化为:速度 = (0.01 秒内产生的脉冲数 ∗ 0.055 ∗ π )/(0.01 ∗422.4​。

weixin_69414084的博客 3819

hdu 3397 Sequence operation(线段树,lazy,区间合并)

hdu 3397 Sequence operation 线段树lazy和区间合并的

yjCola 577

若依框架二次开发——若依 Vue3 版本前端样式优化指南

若依框架二次开发——若依 Vue3 版本前端样式优化指南

bjzhang75的博客 1703

一台NAS两个系统:飞牛nas上完美运行群晖DSM?(飞牛nas+群晖nas套娃安装实操)

想在飞牛NAS上体验群晖DSM系统?💡 本文详细记录了飞牛nas上部署群晖的完整过程,从镜像转换到系统安装,每一步都有详细说明 🖥️,让你在不增加硬件成本的情况下,轻松实现一套硬件、多种系统的灵活部署 ✅

XiaoqiangClub的博客 2443

HDU 3397 Sequence operation 线段树

再做一道线段树。 http://acm.hdu.edu.cn/showproblem.php?pid=3397 这题还是很有意义的,其特殊之处在于操作2和操作4. 操作2是0变1,1变0,这样在pushdown的时候就不能简单覆盖下去,因为如果孩子节点有操作没有完成,会导致结果错误。但是又不能遇到操作2就把子节点也pushdown,这样会超时。。。需要想点办法。。。所以观察一下操作...

173

【软件系统架构】系列四:嵌入式技术

嵌入式系统是以应用为中心、以计算机技术为基础,并将可配置与可裁剪的软硬件集成于一体的专用计算机系统。它需要满足功能、可靠性、成本、体积、功耗等方面的严格要求。 嵌入式系统一般由以下组成部分构成:

moton2017的博客 1006

hdu3397Sequence operation--线段树

hdu3397Sequence operation:http://acm.hdu.edu.cn/showproblem.php?pid=3397 Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc

hahaAll的博客 580

在 SAP BTP 上为应用开启多租户能力:基于 SaaS Provisioning 和 XSUAA 的完整实践指南

本文介绍了在SAP BTP上配置多租户应用的实战指南。主要内容包括: 多租户架构概述:解释provider子账户、consumer子账户和tenant的概念,以及数据隔离机制。 安全配置:在xs-security.json中设置tenant-mode为shared,并为SaaS Provisioning服务添加回调scope授权。 路由配置:通过manifest.yml设置TENANT_HOST_PATTERN来定义多租户访问模式,确保不同租户通过不同子域名访问同一应用。 服务绑定:将XSUAA和SaaS

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 155

黑猫带你学UFS协议第17篇:UFS安全模式详解(Secure Mode)

本文详细介绍了UFS协议中的安全模式,包括Erase, Discard, Purge和Wipe Device操作。Erase用于永久删除数据并确保无法恢复;Discard不保证数据清除;Purge类似于Sanitize,清除内部未映射的物理块数据;Wipe Device则是格式化设备。这些操作涉及逻辑单元、擦除单位、相关寄存器配置等,确保数据安全。" 126036669,12409270,MySQL 8.0 开窗函数详解,"['数据库理论', 'SQL']

黑猫奥利奥的博客 2575

HDU3397(Sequence operation)线段树解法

                                       Sequence operation                        Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                  ...

snayf的博客 356

ADB加密实例

主要介绍如何给adb命令增加加密访问

四儿家的小祖宗的博客 2914

hdu 3397 线段树前后缀blahblah Sequence operation

题意: 给出一个长度为N(N 插入操作: 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串 解:       正解应该比较简单。然而我的解就有点Orz了。 每

Julyana_Lin_夜 1730

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

hdu 3397 Sequence operation 线段树 区间和 连续1的个数

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6032    Accepted Submission(s): 1784 Problem Description lxhgww got

596

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

HDU 3397 Sequence operation(线段树·成段更新·区间合并·混合操作)

题意  给你一个只有0, 1的数组  有这些操作   0. 将[a, b]区间的所有数都改为0   1. 将[a, b]区间的所有数都改为1   2. 将[a, b]区间的所有数都取反 即与1异或   3. 输出区间[a, b]中1的个数  即所有数的和   4. 输出区间[a, b]中最大连续1的长度 对于所有的3, 4操作输出对应的答案 单个的操作都很简单  但搞在一起就

Deng's site \# 970

HDU 3397 Sequence operation (线段树)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4357Accepted Submission(s): 1256 Problem Description lxhgww got ...

anvt77205的博客 168

HDU3397 Sequence operation(线段树)

点击打开题目链接,还有一个类似的题,只进行01翻转操作和最大连续查询,HDU3911 Black And White 对一个数列进行如下操作: Change operations: 0 a b change all characters into '0's in [a , b] 1 a b change all characters into '1's in [a , b] 2

新博客地址:sensirly.github.io 622

HDU3397 Sequence operation(线段树的区间合并)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7547    Accepted Submission(s): 2254 Problem Description lxhgww g

KeyboardPianist的博客 607

HDU 3397 Sequence operation (线段树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:。。。。。。 分析:线段树。难点是怎样处理好3个标记变量(fg0置0;fg1置1;fg2取反)相互之间的关系!其实,fg0和fg1基本是一样的,可以合二为一(fg01)。这样就变成了fg01与fg22(fg2)的关系的处理。 一个例子:先把[a,b]置0(或置1),再把[a,b]取反,此

cyhbyw的专栏 639

hdu 3397 Sequence operation 线段树

一看思路就比较清晰的线段树 5个操作,3个修改区间,2个询问 flag值 -1 , 1 , 0 -1表示,左孩子和右孩子的值不等 1 表示,左孩子和右孩子的值都等于1 0表示,左孩子和右孩子的值都等于0 这样就可以用flag值 进行 懒标记了,更新到 段。。 #include #include #include #include #include using namespace st

xiaoyu1_1的专栏 597

线段树 HDU 3397 Sequence operation

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 代码风格:http://www.notonlysuccess.com/index.php/segment-tree-complete/ 题目大意:操作说明:0——把[a,b]区间全部变成0;1——把[a, b]区间全部变成1;2——把[a,b]区间0,1取反;3——输出[a,b]区间

ACmood 532

hdu-3397 Sequence operation 线段树多种标记

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 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的长度 解题思路: 线段树多种标记。 需要处理的东西比较多: 做题的时...

weixin_34413802的博客 162

hdu 3397 Sequence operation 线段树

代码: #include #include #include #include #include #include #include #include #include #include using namespace std; const int maxn=1e5+10; struct node{ int l,r,len; int pre0,aft0,ma

冷月之殇 681

HDU - 3397 Sequence operation(线段树)

这个题确实不该说难,

Hardbird 771
上一篇: HDU 3308 LCIS(线段树)
下一篇: HDU 1540 Tunnel Warfare(线段树)
lab104_yifan
博客等级 码龄13年 410粉丝 1478原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值