HDU5023:线段树应用于染色问题

Langchain分割方式 【代码】Langchain分割方式。 阅读详情

早听说过线段树,但一直没有学习过,昨天终于学习了一下,并且刷了一道线段树的题,记录一下。

线段树就是把一个区间分两半,左子树存左半段,右子树存右半段,然后重复这个步骤,直到叶子节点(即区间长度为1的节点),就像下面这个图

然后每个节点上可以记录当前节点存储区间的一些信息,比如和,最值,颜色,容易知道这些信息都可以从左右子树获得。

下面上题上代码:

Problem Description

Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money.

Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.

The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.

This was the secretary's idea:

The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.

But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?

 

 

Input

There are several test cases.

For each test case:

The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)

Then M lines follow, each representing an operation. There are two kinds of operations, as described below:

1) P a b c
a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).

2) Q a b
a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).

Please note that the operations are given in time sequence.

The input ends with M = 0 and N = 0.

 

 

Output

For each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must be in ascending order.

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int l;
	int r;
	int color;
	int lazy;
	Node(int l, int r, int c) {
		this->l = l;
		this->r = r;
		this->color = c;
		this->lazy = 0;
	};
}node_t; 

static node_t *tree;

void build(int id, int l, int r) {
	tree[id] = node_t(l, r, 1 << 2);//初始颜色为2
	if (l == r)
		return;
	int m = (l + r) >> 1;
	build(id << 1, l, m);
	build(id << 1 | 1, m + 1, r);
}

void pushDown(int id) {
	if (tree[id].lazy > 0) {
		tree[id << 1].lazy = tree[id].lazy;
		tree[id << 1 | 1].lazy = tree[id].lazy;
		tree[id << 1].color = 1 << tree[id].lazy;
		tree[id << 1 | 1].color = 1 << tree[id].lazy;
		tree[id].lazy = 0;
	}
}

void pushUp(int id) {
	tree[id].color = tree[id << 1].color | tree[id << 1 | 1].color;
}

void change(int l, int r, int c, int id) {
	if (tree[id].l == l && tree[id].r == r) {
		tree[id].color = 1 << c;
		tree[id].lazy = c;
		return;
	}
	pushDown(id);
	if (r <= tree[id << 1].r) {//目标线段在左子树
		change(l, r, c, id << 1);
	}
	else if (l >= tree[id << 1 | 1].l) {//目标线段在右子树
		change(l, r, c, id << 1 | 1);
	}
	else {//目标线段跨左右子树
		change(l, tree[id << 1].r, c, id << 1);
		change(tree[id << 1 | 1].l, r, c, id << 1 | 1);
	}
	pushUp(id);
}

int query(int l, int r, int id) {
	if (tree[id].l == l && tree[id].r == r)
		return tree[id].color;
	pushDown(id);
	if (r <= tree[id << 1].r) {//目标线段在左子树
		return query(l, r, id << 1);
	}
	else if (l >= tree[id << 1 | 1].l) {//目标线段在右子树
		return query(l, r, id << 1 | 1);
	}
	else {//目标线段跨左右子树
		return query(l, tree[id << 1].r, id << 1) | query(tree[id << 1 | 1].l, r, id << 1 | 1);
	}
}

void printRes(int res) {
	char flag = 0;
	for (int i = 1; i <= 30; i++) {
		if ((res&(1 << i)) > 0) {
			if (flag == 0) {
				printf("%d", i);
				flag = 1;
			}else 
				printf(" %d", i);
		}	
	}
	printf("\n");
}

int main()
{
	int N, M;
	while (1) {
		scanf("%d%d", &N, &M);//墙分N段,共有M个操作
		if (N == 0)
			break;
		tree = (node_t*)malloc(sizeof(node_t) * 4 * N);
		build(1, 1, N);//建树
		char op;
		int a, b, c;
		for (int i = 0; i < M; i++) {
			scanf("%c", &op);//回车符
			scanf("%c", &op);
			if (op=='P')//P a b c表示把a到b段涂成颜色c
			{
				scanf("%d%d%d", &a, &b, &c);
				change(a, b, c, 1);
			}
			else {//Q a b查询a到b段有哪些颜色
				scanf("%d%d", &a, &b);
				int res = query(a, b, 1);
				printRes(res);
			}
		}
		free(tree);
	}
	return 0;
}

 

修改legged_gym地形后仍然是平地,如何解决? 🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。 阅读详情

相关推荐

详解如何用爬虫工具批量采集阿里巴巴批发网商品数据

本文主要介绍如何使用后羿采集器的智能模式,免费采集阿里巴巴批发网商品的批发价格、发货时间、是否代发等信息。 采集字段: 商品标题、商品链接、图片链接、标签1、标签2、标签3、价格、30天成交数、 评价、店铺 功能点目录: 如何对采集字段进行配置 如何采集列表+详情页类型网页 采集结果预览: 下面我们来详细介绍一下如何免费采集1688批发网的商品数据,我们以“羽绒服女”为例,具体步骤如下: 步骤一...

weixin_43777427的博客 1万+

树的遍历

树的中序遍历(前序、后序同理) 题目来源:leetcode 给定一个二叉树,返回它的中序 遍历。示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? # Definition for a binary tree node. # class TreeNode: # ...

黎先生的博客 211

Nangate 45nm FreePDK开源工艺库:从RTL到GDSII全流程实战

在数字IC设计中,标准单元库是连接逻辑综合与物理实现的基石,它定义了每个单元的逻辑功能、时序延迟与物理尺寸。开源EDA工具链的成熟,使得个人也能借助免费工艺套件完整跑通芯片设计流程。Nangate 45nm FreePDK作为一套经典的开源工艺库,凭借结构清晰、配套完善的特性,成为学习数字后端和时序收敛的理想训练场。从综合工具Yosys读取Liberty文件进行逻辑映射,到OpenROAD执行布局布线与时钟树综合,再到使用OpenSTA分析关键路径延迟,这套流程涵盖了数字设计最核心的环节。通过亲手操作,开发

weixin_29168393的博客 272

二叉树的遍历

一种 “颜色标记法” (瞎起的名字……),兼具栈迭代方法的高效,又像递归方法一样简洁易懂,更重要的是,这种方法对于前序、中序、后序遍历,能够写出完全一致的代码。其核心思想如下:使用颜色标记节点的状态,新节点为白色,已访问的节点为灰色。如果遇到的节点为白色,则将其标记为灰色,然后将其右子节点、自身、左子节点依次入栈。如果遇到的节点为灰色,则将节点的值输出。

霜叶的博客 236

PHP实战:5分钟搞定SM4国密加密算法集成(附完整代码)

本文详细介绍了如何在PHP项目中快速集成SM4国密加密算法,提供完整的代码实现和实际应用示例。从算法原理到核心代码解析,再到性能优化与安全实践,帮助开发者高效实现数据加密功能,满足金融、政务等领域的合规需求。

weixin_29315569的博客 354

YooAsset 学习

YooAssets使用教程

weixin_44884538的博客 1659

HDU 5023(线段树染色)(两种方法)(非状压,巧妙得到状态)(状压得到状态)(超详解)

思路:用线段树去维护,修改的区间,并染上色。 当询问某一个区间时,可以把当前区间内的所有色全存到 数组里,最后排序去重,输出。 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; typedef long long LL; const int N=1e6+10; struct

qq_51010640的博客 369

基于树莓派4B设计的厨房环境监测系统_368

本项目设计并实现了一种基于树莓派4B的厨房环境监测系统,提升厨房使用过程中的安全性与智能化水平。系统采用树莓派4B作为主控核心,集成DHT11温湿度传感器、MQ2烟雾传感器、火焰传感器、光敏传感器、水滴检测传感器等多种传感器,实现对厨房环境温湿度、烟雾、火焰、光照与溢水等参数的实时监测,并通过蜂鸣器进行本地报警。系统通过WIFI连接互联网,利用MQTT协议将环境数据上传至华为云物联网平台,实现远程可视化监控。

415

线段树进阶-染色问题 附poj-2777题解

区域染色覆盖问题 假设某大学有一面文化墙,各个学院都可以在上面涂色,要求涂色区域高必须和墙一样,宽度任意但必须是整数(以米为单位)。涂色可以覆盖其他学院的涂色。现在若干个学院涂色之后最终这面墙上能看见多少种颜色。 这就是简单的染色问题了。当然有很多种不同的说法,但整体上离不开这两个问题:1.区域覆盖。2.多种染色方式。既然是区域操作,那么用线段树是比较合理了。用...

iwts 5374

内模控制(IMC)——理论、应用与实践

内模控制方法的核心思想是利用被控对象的数学模型(即内模)构建控制器,使得控制器可以“预知”被控系统的动态响应,从而在控制输入中提前补偿模型的固有特性和扰动影响。常见的内模控制结构中,将系统分为无延迟部分和扰动部分,并利用内部模型预测输出值,再通过反馈回路进行误差修正。utuimctufbtutui​mctuf​bt其中,uimctu_imc(t)ui​mct为内模预测得到的预补偿控制量,ufbtu_fb(t)uf​bt。

闲人编程的博客 2632

把 NAST 变成你的遥控器:用 ABAP 程序精确触发与处理 Output Type

摘要 SAP中的Message Control机制通过NAST表记录输出状态,支持自动触发打印、邮件等操作。关键步骤包括: 构造NAST记录:定义输出类型、介质、处理状态等参数 调用处理函数:使用RV_MESSAGE_UPDATE_SINGLE更新表,通过WFMC_MESSAGES_PROCESS执行输出 典型应用场景: 单据补发(如PO邮件重发) 外部系统集成触发输出 故障恢复处理积压消息 注意事项: 需提交事务确保更新生效 区分S/4HANA新旧输出体系 正确处理派发时间(VSZTP)和状态(VSTAT

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

hdu 5023(线段树)

题意:有一个人要在墙上刷漆,先把墙上分为n块,全部刷成颜色2,然后两种操作P a b c表示把区域a到b刷成颜色c,Q a b表示问区域a到区域b有哪几种颜色。 题解:看大牛思路是用状态压缩,有哪种颜色就标记哪一位,然后用线段树区间修改就可以了,最后查询判断当前区间是的数字哪些位上是1,解中就有这种颜色。。 #include const int N = 4000000; int n,

俯瞰风景 651

[PCIE5.0] 2.2.8.3 Error Signaling Messages

错误信号消息用于在 PCI Express 设备之间传递错误信息,帮助系统发现并报告错误。每当发生某种错误时,检测到该错误的设备会通过发送这些消息来通知系统。• 没有数据负载:这些错误信号消息不包含任何数据(即 TLP Type 是 Msg),它们仅用于错误报告。• 长度字段保留:在这些消息中,长度字段是保留的,不使用。• 功能号(Function Number)字段:这个字段标识哪个设备发生了错误。

系统固件工程师 292

hdu 5023 线段树染色问题

题目链接 A Corrupt Mayor’s Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 4094 Accepted Submission(s): 1418 Problem Descripti...

lifehappy 336

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

hdu 5023 线段树染色+状压

题目链接:https://vjudge.net/problem/HDU-5023 转自:https://blog.youkuaiyun.com/hyczms/article/details/44929711 题意:初始所有点的颜色为2,P为区间修改操作,可以将一段连续区间内的颜色更改,Q为查询操作,按照升序输出区间内的颜色。 思路:一共有30种颜色,则可以用一个int整数表示最终答案,因为一个int有32位,...

A 205

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Hdu5023_A Corrupt Mayor's Performance Art(线段树区间染色+区间查询)

题意: 一块色板上只能做两件事:1. “P A B C” 指在A到 B 号方格中涂上颜色 C。2. “Q A B” 指老师的提问:A到 B号方格中有哪几种颜色。初始时全部涂成2号颜色。 思路: 常规的线段树区间染色。 #include <cstdio> #include <cstring> #include <algorithm> #define lson ro...

CY05627的博客 337

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

HDU 5023线段树区间染色,统计区间内颜色个数

这个也是一个线段树的模板 #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<set> using namespace std; const int maxx = 1000050; set&...

qq_40921866的博客 298

poj 2777,hdu 5023 线段树区间染色+lazy+位运算

题目: Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44465   Accepted: 13479 Description Chosen Problem Solving and Program design as an op

xiaoli_nu的博客 404

hdu 5023 A Corrupt Mayor's Performance Art

染色覆盖问题,有q次操作,每次操作可以把一个区间染成一种颜色或者把询问一个区间有多少中颜色。 把一个区间拆分成线段树中的一个一个的小区间,查询的时候也一样,当我们要染的区间是一个大区间的一部分且大区间有颜色时,把大区间的颜色pushdown。 查询时当线段树的区间包含了我们要查询的区间就返回这个区间的颜色,这和普通线段树的操作正好相反。 #include &lt...

ad4627的博客 124

HDU-5023 线段树染色问题+延时标记

题意给我们1-n的区间段 两种操作 P a b c 表示把a到b染成c颜色 Q a b 表示求a到b的颜色种类数量 注意初始全部先染成颜色2分析线段树经典染色问题 可以用二进制来表示颜色数量 父节点可以用”|”运算来统计两个子节点的信息CODE#include<bits/stdc++.h> #define lson l,mid,rt<<1 #define rson mid+1,r,rt<

qq_33859479的博客 302

hdu 5023 && poj 2777(线段染色)2014 ACM/ICPC Asia Regional 广州 Online

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5023 http://poj.org/problem?id=2777 题意:给出一个长度为N的线段,分成N段,每一段长度为1,每次操作时,把[L, R]区间染成颜色C,或者输出[L, R]区间里有几种不同的颜色。 分析:因为N比较大,如果模拟操作肯定会超时。这时我们就可以利用线段树的l

LYHVOYAGE的专栏 2608

hdu 5023(线段树区间染色,统计区间内颜色个数)

题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数(在查询或者修改的过程中才更新标记对节点val值的影响,每次更新val值只更新到当前要查询或者修改的节点,最底层的标记以下的节点的val值应该是没有被更新过) Pushdown向下传递标记和修...

weixin_30542079的博客 238

8月19日训练总结HDU5023 POJ2777 HDU1540POJ2892POJ3468

HDU5023A Corrupt Mayor's Performance Art 区间染色,然后区间查询出现的所有颜色。 线段树区间覆盖不用多说,主要是对于颜色如何查重。每一个区间加个set肯定不行,写着太麻烦,最后需要迭代器输出,而且向上转移的时候也需要迭代器循环放入,很可能会超时。 注意到只有30种颜色,联想到状压dp,可以用进制位表示该颜色是否被使用。第一个颜色就是1,第二个颜色就是2...

Luowaterbi的博客 257

[HDU5023]A Corrupt Mayor's Performance Art

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless p...

dxyinme-Zz 286

HDU5023 - A Corrupt Mayor's Performance Art(线段树)

题目链接: HDU5023 【题意】给出n段

转移到dawxy.com 708

HDU 5023 (2014广州网络赛 线段树)

解题思路:   有两种操作,第一种是将区间 a 到 b 染成颜色 c ,第二种是询问区间 a  到 b 内的颜色,按照顺序输出。 注意到颜色只有三十种,因此我们可以用二进制数表示颜色,通过位运算来操作。 #include #include #include #include #include #include #include #include #include #inclu

蘑菇--走在通往梦想的道路上。 947
上一篇: ganglia监控mongoDB
下一篇: 单词接龙的最短字符串长度:DFS加剪枝
热血大婶
博客等级 码龄16年 58粉丝 32原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血大婶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值