Mayor's posters (线段树+离散化+加思维)

F280049C Crossbar X-BAR X-BAR不是一个功能模块,只起内部外设与外设之间,或内部外设与GPIO的连接作用。配置X-BAR的就是在配置信号之间的连接。确实如这一节的开头所言,X-BAR提供了以各种配置连接设备输入、输出和内部资源的灵活性。学完这一节也知道为什么crossbar(X-BAR)的中文翻译是交叉开关。 阅读详情

 

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 

  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.


They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

 

题意:给你一个长 10000000 的墙,让你在上面贴海报,输入t,表示有几组数据,下面有一个数字n,表示有n长海报,下面n行,表示每张海报的要贴的区间,从上到下按照顺序贴,看看到最后有几张海报能露出来(露出部分也算)

 

思路:线段树,既是这个区间的懒惰标记值,也就第几张海报,到最后查询一次就行了,只有查询到这个区间的懒惰标记值不为0,就可以返回了,由于给出的墙太长,但是给你的海报上数就10000,所以你可以离散化;

代码一:离散化时,搞错了,但poj上能过,数据太水了

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 10010

struct node
{
	int x,y;
}s[Max];
int stu[8*Max];
int n;
int book[Max];
int a[2*Max];
void build(int root,int star,int end)
{
	stu[root] = 0;
	if(star==end)
		return ;
	int mid = (star+end)/2;
	build(root*2,star,mid);
	build(root*2+1,mid+1,end);
}

void down(int root)
{
	stu[2*root] = stu[2*root+1] = stu[root];
	stu[root] = 0;
}
void updat(int root,int star,int end,int x,int y,int val)
{
	if(star>=x&&end<=y)
	{
		stu[root] = val;
		return ;
	}
	if(stu[root])
		down(root);
	int mid = (star+end)/2;
	if(x<=mid)
		updat(root*2,star,mid,x,y,val);
	if(y>mid) updat(root*2+1,mid+1,end,x,y,val);
}

void Qury(int root,int star,int end)
{
	if(star==end&&stu[root]==0)
		return ;
	if(stu[root])
	{
		book[stu[root]] = 1;
		return ;
	}
	int mid = (star+end)/2;
	Qury(root*2,star,mid);
	Qury(root*2+1,mid+1,end);
}
int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		int x,y;
		scanf("%d",&n);
		memset(book,0,sizeof(book));
		int ma = 0;
		int tt = 0;
		for(i = 1;i<=n;i++)
		{
			scanf("%d%d",&s[i].x,&s[i].y);
			a[tt++] = s[i].x;
			a[tt++] = s[i].y;	
		}
		sort(a,a+tt);
		for(i = 1;i<=n;i++)
		{
			s[i].x = (lower_bound(a,a+tt,s[i].x)-a) + 1;
			s[i].y = (lower_bound(a,a+tt,s[i].y)-a) + 1;
		}
		build(1,1,tt);
		for(i = 1;i<=n;i++)
			updat(1,1,tt,s[i].x,s[i].y,i);
		Qury(1,1,tt);
		int sum = 0;
		for(i = 1;i<=n;i++)
			if(book[i])
				sum++;
		printf("%d\n",sum);
	}
	return 0;
} 

修改后的代码:

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
#define Max 20010
vector<int> v;
struct node
{
	int x,y;
}s[Max];
int stu[10*Max];
int n;
int book[Max];
int a[Max];
void build(int root,int star,int end)
{
	stu[root] = 0;
	if(star==end)
		return ;
	int mid = (star+end)/2;
	build(root*2,star,mid);
	build(root*2+1,mid+1,end);
}
 
void down(int root)
{
	stu[2*root] = stu[2*root+1] = stu[root];
	stu[root] = 0;
}
void updat(int root,int star,int end,int x,int y,int val)
{
	if(star>=x&&end<=y)
	{
		stu[root] = val;
		return ;
	}
	if(stu[root])
		down(root);
	int mid = (star+end)/2;
	if(x<=mid)
		updat(root*2,star,mid,x,y,val);
	if(y>mid) 
	updat(root*2+1,mid+1,end,x,y,val);
}
 
void Qury(int root,int star,int end)
{
	if(star==end&&stu[root]==0)
		return ;
	if(stu[root])
	{
		book[stu[root]] = 1;
		return ;
	}
	int mid = (star+end)/2;
	Qury(root*2,star,mid);
	Qury(root*2+1,mid+1,end);
}
int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		v.clear();
		int x,y;
		scanf("%d",&n);
		memset(book,0,sizeof(book));
		int ma = 0;
		int tt = 0;
		for(i = 1;i<=n;i++)
		{
			scanf("%d%d",&s[i].x,&s[i].y);
			v.push_back(s[i].x);
			v.push_back(s[i].y);
		}
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end()); // 去掉重复元素;
		a[0] = 1;
		int kk = 1; 
        // 离散化时,相邻的元素还让它们相邻,不相邻的元素之间相差2,不让它们相邻
		for(int i = 1;i<v.size();i++)
		{
			if(v[i] ==v[i-1] + 1){
				kk ++;
				a[i] = kk;
			}
			else{
				kk +=2;
				a[i] = kk;
			}			
		}		
		for(i = 1;i<=n;i++){
			s[i].x = a[(lower_bound(v.begin(),v.end(),s[i].x)-v.begin())];
			s[i].y = a[(lower_bound(v.begin(),v.end(),s[i].y)-v.begin())];
			//printf("s[%d].x==%d  %d\n",i,s[i].x,s[i].y) 
		}		
		build(1,1,v.size()*2+10);
		for(i = 1;i<=n;i++)
			updat(1,1,v.size()*2+10,s[i].x,s[i].y,i);
		Qury(1,1,v.size()*2+10);
		int sum = 0;
		for(i = 1;i<=n;i++)
			if(book[i])
				sum++;
		printf("%d\n",sum);
	}
	return 0;
} 

 

Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知 Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知 文章目录Gitlab通过Webhook自动触发Jenkins构建任务并在更新完毕后发送钉钉消息通知1.实现思路2.新增钉钉机器人并测试消息的发送2.1.新增钉钉机器人2.2.测试发送消息到钉钉3.在Jenkins中新建一个构建任务3.1.新建任务3.2.配置任务时增加参数化构建过程3.3.配置任务添加git地址3.4.配置构建触发器生成gitlab webhook使用的token3.5.配置任务构建过程中执行的She 阅读详情

相关推荐

从零开始:IBM V3700控制器更换与机器代码升级全流程指南

本文详细介绍了IBM V3700存储系统控制器更换与机器代码升级的全流程指南。从前期准备、硬件更换到版本同步,逐步解析如何应对503错误等常见问题,确保系统稳定运行。通过实战案例和优化建议,帮助工程师高效完成维护任务,提升存储系统可用性。

weixin_28365523的博客 331

Mayor‘s posters(线段树染色)

题目链接: Mayor’s posters 大致题意 有n个人依次贴海报, 第i个海报的范围是[li, ri]. 后面贴的海报会覆盖掉之前贴的海报. 问: 最终还能看到多少张海报. 解题思路 这个题算是线段树染色里非常经典的题目了. 考察的是线段树的区间修改, 区间查询, 染色. 这类题目的一大特点就是: 线段树内不维护任何数值, 当前节点的值即为懒标记的值.(个人理解) 特别的, 本题的实际数据范围推荐为1E5, 题目中的1E4会报RE AC代码 #include <iostream> #in

Fau的博客 1632

Nvidia Jetson AGX Orin 如何进入 Recovery Mode(刷机注意事项)

Jetson Orin 如何进入Recovery mode + 刷机注意事项

Pandako's Blog 1万+

Mayor‘s posters (线段树+离散化)

题目链接:Mayor’s posters 思路:由于看到l,r的值最大可达到1e7,这时候如果强行build,那么大概率会出错,看到n的值只有1e4,这时候我们应该想到用离散化去解决这个问题。 而且,这里还有一个坑点,假如所给区间为[1,4],[6,7],[1,7],那么离散化之后1,4,6,7分别对应1,2,3,4这时候如果求出海报的种类,那么就错误的求出了[1,2],[3,4]的海报种类为2。所以我们判断当两个结点的值的差大于1时,那么插入一个中间结点,就可以避免此情况。 代码:(还是y总的代码风格看起

weixin_45489261的博客 360

保姆级教程:在Ubuntu 22.04上从零安装SUMO并运行第一个交通仿真

本教程详细介绍了在Ubuntu 22.04系统上从零安装SUMO交通仿真软件并运行第一个仿真的完整流程。涵盖环境准备、PPA源安装、核心组件验证、首次仿真案例运行以及进阶配置技巧,特别针对netedit路网编辑器和地图格式转换等实用工具进行重点讲解,帮助用户快速掌握SUMO在交通仿真领域的应用。

weixin_30546189的博客 426

YOLOV12融合MSDA多尺度空洞注意力:突破尺度限制的科研实践指南

MSDA通过多尺度空洞注意力的设计,为YOLOV12突破尺度检测瓶颈提供了有效方案。从原理理解到代码实现,再到集成调优,每一步都是科研中“解决实际问题”的具体实践。如果你在实验中发现某类尺度目标的检测仍有不足,可以尝试结合数据增强(如随机缩放、多尺度训练)来强化模型对尺度变化的鲁棒性。科研的价值在于不断解决真实难题,希望这份指南能真正帮你在目标检测的尺度感知领域找到新的突破点,祝你在科研探索中持续收获新的见解与成果。

FJN110的博客 249

Mayor‘s posters--线段树染色

Mayor’s posters #include<iostream> #include<algorithm> #include<set> #include<vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll>pll; typedef pair<int, int>pii; const

weach的博客 678

基于matlab的CNN食物识别分类系统,matlab深度学习分类,训练+数据集+界面

饮食在人们的日常生活、营养与医疗建议以及运动员等专业人士的训练上起着越来越重要的作用。随着互联网的发展和医学的进步,人们普遍上传分享和记录的食物图像形成了非常多的数据库,为了改善饮食结构,塑造更健康的生活方式,分析食物的种类、热量和进食时间成为了营养学上非常重要的研究方法,本文的使用深度学习算法可以根据用户上传的食物图像自动分析食物种类。深度学习是机器学习领域的一个研究方向。深度学习通过对数据特征的学习将原始数据转化为计算机可以理解的抽象数据,根据学习到的特征,可以对原始数据进行检测或分类。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 4737

Mayor's posters (线段树+离散化)

Mayor's posters (线段树+离散化) The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. T...

石旭先森的博客 333

Xilinx FPGA平台GTX简易使用教程(五)GTX收发测试及示例工程介绍

GTX IP配置完了,你不得搞个回环测试一番?

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

6.14华为OD机试真题 新系统 - 字符串格式调整 (Java/Py/C/C++/Js/Go)

# **字符串格式调整** > 2026 华为OD机试真题 6月14日华为OD上机新系统考试真题 100 分题型 点击查看华为 OD 机试真题完整目录:[2026最新华为OD机试新系统卷 + 双机位C卷 真题题库目录|全覆盖题库 + 逐点算法考点详解](https://blog.csdn.net/banxia_frontend/article/details/160346662) # 题目描述 输入一个字符串,字符串中只包含大写字母和数字。要求将字符串中的所有数字提取出来,按出现的顺序组

算法大师! 961

Mayor‘s posters--区间覆盖

题目链接: 2528 -- Mayor's posters 题意: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000)。求出最后还能看见多少张海报。 第一行:样例个数T 第二行:贴海报的人n 第三行:每个人贴海报的范围 接下来n行:每个人贴海报的范围 代码: #include <cstdio> #include <iostream> #include <algo...

weixin_45772744的博客 531

[系统安全] 四十七.恶意软件分析 (3)动态分析经典沙箱Cape的安装和基础用法详解

系统安全将更好地帮助初学者了解病毒逆向分析和系统安全。前文详细介绍恶意代码静态分析经典工具Capa的基础用法,以及批量提取静态特征和ATT&CK技战术,主要是从提取的静态特征Json文件中提取关键特征。这篇文章将详细讲解动态分析沙箱Cape,其是一个开源的自动恶意软件分析系统,通过自动运行和分析恶意软件,全面分析和提取恶意软件的关键特征。本文先介绍Cape沙箱的安装和基础用法,后续随着深入再分享。基础性文章,希望对您有帮助!

杨秀璋的专栏 6758

POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

POJ.2528 Mayor’s posters (线段树 区间更新 区间查询 离散化)题意分析贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报。最多有10000张海报,海报左右坐标范围不超过10000000。 一看见10000000肯定就要离散化了,因为建树肯定是建不下。离散化的方法是:先存到一个数组里面,然后sort,之后unique去重,最后查他离散化的坐标lower_bo

Pengwill's Blog 333

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

D - Mayor's posters(线段树+离散化)

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally d...

cherish_lin的博客 295

ssm框架搭建

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

POJ2528——Mayor's posters

题目链接:Mayor’s posters 推荐博客:【完全版】线段树(转载) 题目: 在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报(后面贴的海报覆盖前面的) 分析: 离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多 而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误 代码: #include<stdio.h...

浦柳人的博客 372

假人有限元模型

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

poj 2528 Mayor's posters

看到这道题就想起了牛插队的问题,如果没做过的同学可以去看看(poj 2182 Lost Cows)  ,肯定逆着来是肯定的, 不然无法进行判断。 还有一个问题便是这题的坐标范围实在太大了,如果真按照他的坐标来建立线段树,超时爆内存是肯定的事。  这题的点是突破口,  点的数目才10000,这时离散化便起作用了(我离散化也不大行,参考了下依然的),离散化了就变成了前面的奶牛问题了,这题很适合练习

purevegetable 731

【kuangbin带你飞】专题7 线段树 D - Mayor's posters

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally d...

qq_42711300的博客 339

Mayor's posters(线段树区间覆盖)

Time Limit:1000MS Memory Limit:65536K Total Submissions:87386 Accepted:25076 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral el...

ccsu_deer 509

POJ - 2528 Mayor's posters(线段树区间更新+离散化)

题目链接 题意: 给定一个高度为1,长度为$1e7$的黑板报,现在要做黑板报上依次贴广告,问最后能够看到多少张广告(漏出来一点都算看到)。 思路: 一开始一直正向的做,感觉特别的麻烦,于是可以倒着想,,从最后一张开始贴,若当前要贴的广告的区间已经被覆盖了,则这个广告对答案就没有贡献,否则就能看见。 当然离散化方法有很多种,这里只用了比较节约时间的一种。 1 /* 2...

173

poj 2528 Mayor‘s posters

线段树

weixin_46266058的博客 731

poj (2528) Mayor's posters (简单离散化线段树)

欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和图片上传 LaTex数学公式 UML序列图和流程图 离线写博客 导入导出Markdown文件 丰富的快捷键 快捷键 加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl

mas3399的博客 302

Mayor‘s posters(线段树+离散化)

题目:The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the pos

qq_61037155的博客 312

【hdu】Mayor's posters(线段树区间问题)

需要离散化处理,线段树的区间修改问题。 需要注意的就是离散化的时候,由于给的数字是一段单位长度,所以需要特殊处理(因为线段的覆盖和点的覆盖是不一样的) 比如:(1,10)(1,4) (6,10) 离散化之后是 1 , 4 , 6 , 10 分别离散为 1 2 3 4 覆盖的时候先覆盖1 4 之后覆盖了1 2 之后覆盖了 2 3,结果为2 但是实际上应该是3 134503

花月阁 930

POJ - 2528 Mayor‘s posters(set,线段树)

POJ - 2528 Mayor’s posters set做法 从后往前扫描,查询当前l-r区间是否已经被完全覆盖,然后将l~r覆盖一遍。 我们考虑只把一部分的点放到set中以起到跟上述做法同样的效果:类似于离散化,只把每张海报的l,r,r+1三个点插入set,当l-r区间内没有点留在set中时,l-r区间一定是被完全覆盖了,这样时间复杂度降到了O(nlogn),而且代码量比线段树小非常多。 #include<cstdio> #include<vector> #include

00后的博客 788
上一篇: CKOJ 1066: 地形编辑器(整个区间加减一个数,多次区间操作,处理端点,但这是二维的,骚操作(有dp思想))
下一篇: Fair CodeForces - 987D (bfs,设置超级源点,对每个物品进行暴力求解)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值