POJ 2528 - Mayor's posters - [离散化+区间修改线段树]
题目链接:http://poj.org/problem?id=2528
Time Limit: 1000MS Memory Limit: 65536K
Description
- 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
Output
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
题意:
现有一面墙,墙上有一些格子,从1~n标号,给出m张海报的左右边界值,表示从l到r的格子上贴了海报。
按顺序海报一层层贴,会有覆盖,求所有海报贴完后,整面墙上露出多少张海报(不一定要露出整张海报,露出一部分也算)。
题解:
首先想到的是,区间修改线段树,但是看到l与r的范围,显然不能直接建立那么大范围的线段树;
看到海报数量不超过10000,便考虑进行离散化;
离散化后,我们给树初始化建树为0,表示没有海报,后续按照海报的编号(1~m)进行区间修改;
最后查询每个格子,只要大于0,就代表这个格子上有海报,进行计数,最后即可得答案;
otherwise,就像discuss里说的那样,例如case:
1
3
1 10
1 3
6 10
如果进行普通的离散化,会出现问题:
原本3到6之间露出会露出海报1,但是普通离散化后,3被离散化为2,6被离散化为3,之间的间隔消失,就无法露出海报1,就会产生错误答案;
故进行离散化时,idx[]数组除了加入l与r值外,再加入l-1与r+1(当然,其实只加入l-1也是可以的),使得即使在离散化后,每个海报间有一定的间隙;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 40000+5
using namespace std;
typedef long long ll;
int m,a[MAXN];
int idx[MAXN];//离散化索引
struct Paint{int l,r;}paint[MAXN];
struct Node{
int l,r;
int val,lazy;
void update(ll x)
{
val=(r-l+)*x;
lazy=x;
}
}node[*MAXN];
void pushdown(int root)
{
if(node[root].lazy)
{
node[root*].update(node[root].lazy);
node[root*+].update(node[root].lazy);
node[root].lazy=;
}
}
void pushup(int root)
{
node[root].val=max(node[root*].val,node[root*+].val);
}
void build(int root,int l,int r)
{
node[root].l=l; node[root].r=r;
node[root].val=; node[root].lazy=;
if(l==r) node[root].val=a[l];
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
void update(int root,int st,int ed,int val)
{
if(st>node[root].r || ed<node[root].l) return;
if(st<=node[root].l && node[root].r<=ed) node[root].update(val);
else
{
pushdown(root);
update(root*,st,ed,val);
update(root*+,st,ed,val);
pushup(root);
}
}
int query(int root,int st,int ed)
{
if(ed<node[root].l || node[root].r<st) return ;
if(st<=node[root].l && node[root].r<=ed) return node[root].val;
else
{
pushdown(root);
ll a=query(root*,st,ed);
ll b=query(root*+,st,ed);
pushup(root);
return max(a,b);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
memset(a,,sizeof(a));
int _size=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&paint[i].l,&paint[i].r);
idx[_size++]=paint[i].l;
idx[_size++]=paint[i].l-;
idx[_size++]=paint[i].r;
idx[_size++]=paint[i].r+;
}
sort(idx,idx+_size);
_size=unique(idx,idx+_size)-idx;
build(,,_size);//线段树建树
for(int i=;i<=m;i++)
{
int l=lower_bound(idx,idx+_size,paint[i].l)-idx+;
int r=lower_bound(idx,idx+_size,paint[i].r)-idx+;
//得到paint[i].l和r对应的离散化后的值
update(,l,r,i);
}
int cnt=;
for(int i=;i<=_size;i++)//遍历墙上每个格子
{
int tmp=query(,i,i);
if(tmp>) idx[cnt++]=tmp;//如果墙上贴了海报,就进行记录
}
sort(idx,idx+cnt);//排序后进行去重,去重后的数组的size即答案
printf("%d\n",unique(idx,idx+cnt)-idx);
}
}
PS.代码里,idx[]数组在完成了离散化任务后,被我重复利用了一下,存了每个墙格子最后贴了海报几;
POJ 2528 - Mayor's posters - [离散化+区间修改线段树]的更多相关文章
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- POJ:2528(Mayor's posters)离散化成段更新+简单哈希
http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the ca ...
- POJ - 2528 Mayor's posters (离散化+线段树区间修改)
https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
随机推荐
- android中sharedPreferences的用法(转)
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
- C# 多线程 Parallel.ForEach 和 ForEach 效率问题研究及理解
from:https://blog.csdn.net/li315171406/article/details/78450534 最近要做一个大数据dataTable循环操作,开始发现 运用foreac ...
- 如何用一个for循环打印出一个二维数组
思路分析: 二维数组在内存中默认是按照行存储的,比如一个二维数组{{1,2,3,},{4,5,6}},它在内存中存储的顺序就是1.2.3.4.5.6,也就是说,对于这6个数组元素,按照从0到5给它们编 ...
- 修改Tomcat的默认访问目录
放在外网的应用,用户多是直接输入域名访问,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml就可以了. 具体是是在< ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 多注册中心
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多注册中心 可以自行扩展注册中心,参见:注册中心扩展 (1) 多注册中心注册 比如 ...
- MongoDB中常用的find
接着前一篇文章,下面主要介绍一下MongoDB中常用的find操作. 先打开MongoDB shell,通过下面一组命令插入一些数据. post1 = {} post2 = {} post3 = {} ...
- ajax做省市联动
原理: 当select.jsp页面打开时,向服务器发送异步请求,得到所有省份的名称(文本数据).然后使用每个省份名称创建<option>,添加到<select name=”provi ...
- Maven编译出现“[ERROR] java.lang.OutOfMemoryError: Java heap space”
Windows下添加环境变量MAVEN_OPTS的value为-Xms1024m -Xmx1024m -Xss1m Linux下可修改.profile或者.bash_profile文件,并做如下设置: ...
- TOMCAT可以稳定支持的最大并发用户数
转自:http://blog.sina.com.cn/s/blog_68b7d2f50101ann7.html 服务器配置: 单硬盘,SATA 8MB缓存 测试服务器和loadrunner运行服务 ...
- Mac终端解压命令集合
tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压1 ...