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

poj-2528 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 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;
} 

 

POJ2528-Mayor's posters【区间映射压缩(离散化)+线段树】 POJ2528-Mayor's posters 【区间映射压缩(离散化)+线段树】 解题报告+AC代码 http://hi.csdn.net/!s/83XZJU ========> 我的所有POJ解题报告链接: http://blog.csdn.net/lyy289065406/article/details/6642573 立即下载

相关推荐

R语言vegan包实战:用varpart函数搞定生态学VPA分析(附完整代码与数据集)

本文详细介绍了如何使用R语言vegan包中的varpart函数进行生态学方差分解分析(VPA),包括数据准备、模型构建、结果解读和可视化呈现。通过完整的代码示例和数据集,帮助研究者理解不同环境因子对生物群落变化的相对贡献,并掌握显著性检验等高级技巧。

weixin_42525005的博客 456

Mayor‘s posters(线段树染色)

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

Fau的博客 1632

从零构建HFSS连接器仿真模型:以SMP接口的精准复现为例

本文详细介绍了如何从零构建HFSS连接器仿真模型,以SMP接口的精准复现为例。通过获取官方模型、导入技巧、信号路径提取和介质结构复现等步骤,帮助工程师避免常见错误,提升高频电路仿真的准确性。文章特别强调了模型精度对仿真结果的影响,并提供了实用的调试和管理建议。

weixin_29041767的博客 449

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

【数据分析生命周期全揭秘】从零开始打造你的数据科学项目 | 最全指南

在当今数据驱动的世界中,数据科学正在颠覆传统行业,成为企业决策和价值创造的重要引擎。然而,如何科学、系统地开展一个数据分析项目?这是很多人关心的核心问题。本篇文章基于经典数据分析课程内容,深入解读数据分析生命周期的每个阶段,让你快速掌握从“发现问题”到“落地应用”的完整流程。如果你正在学习或准备从事数据分析领域,这篇文章一定会对你大有帮助!数据分析生命周期的六大阶段数据分析生命周期的六个阶段相辅相成,每个阶段都至关重要:发现问题:明确需求,提出假设。数据准备:清洗、转换和可视化数据。

AI手套的博客 1750

arduino uno spi驱动12864

arduino uno spi驱动12864

yvee的博客 2066

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的博客 679

STM32+OOK通讯(OOK发送+OOK接收)+cubemx配置

关于OOK通信,我的理解就是将要发送的信息转换成高低电平(即0和1),然后接收方通过对01进行解码获得信息。于是我用了非常朴素的字符串转二进制+AM调制来完成通信链。话不多说我们直接开始!!!

qq_73856732的博客 2350

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

一套C#接口通吃安川/发那科/库卡:工业机器人统一控制框架设计与产线踩坑实录

我们把机器人的通用能力抽象为接口,涵盖连接、运动、IO、寄存器、状态、报警六大类能力,所有品牌适配器都必须实现这个接口。/// 工业机器人统一控制接口// 基础连接// 运动控制// IO与寄存器// 状态与报警// 事件工业机器人统一控制框架的本质,不是把所有品牌的功能都做平,而是找到通用能力的最大公约数,通过分层抽象把品牌差异隔离在底层。上层业务只关注工艺和流程,不需要关心机器人是什么品牌、用什么SDK、通信协议是什么。

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 418

计算机毕设:线性回归 Xgboost LSTM 预测黄金价格实战 完整代码数据视频讲解 可直接运行

线性回归 Xgboost LSTM 预测黄金价格实战 完整代码数据视频讲解 可直接运行

1776

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】高分辨率统计降尺度日值数据集NEX-GDDP的介绍与下载方法

对NEX-GDDP数据集进行介绍和下载说明

WW、forever的博客 2404

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

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

Pengwill's Blog 333

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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的博客 296

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

POJ2528——Mayor's posters

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

浦柳人的博客 372

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

poj 2528 Mayor's posters

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

purevegetable 732

【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的博客 340

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

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

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后的博客 789
上一篇: 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、付费专栏及课程。

余额充值
⌂ 换网址