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 ...
随机推荐
- win10进入到安全模式的三种方法
这里介绍三种方法: 如果能够进入到系统 点击开始--设置--更新和安全--恢复,右侧点击高级启动中的立即重启 能够进入到登陆界面 进入到登录屏幕后,在按住 Shift 键的同时依次选择“电源” > ...
- HttpClinet学习笔记
本文为学习httpClient学习过程中转载的文章,若涉及版权请留言. ----------------------------- 前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议 ...
- 解决select2在modal中无法输入的问题
解决办法: 在js里加一句 $.fn.modal.Constructor.prototype.enforceFocus = function(){};
- iOS开发-UIImageView的contentMode属性
UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFillUIView ...
- Nginx 代理
如下,配置 Nginx 成为一台代理服务器 [root@localhost ~]$ cat /usr/local/nginx/conf/vhost/proxy.conf server { listen ...
- HipHop的原理
HipHop的原理 HipHop将PHP代码转换为高度优化的C++代码,然后再用g++编译器编译.它可以保持语义等效地执行源代码,但为了提高性能,牺牲了一些很少用到的特性,比如eval(). HipH ...
- PS合成以及分解GIF
http://jingyan.baidu.com/article/3052f5a1c91f0497f31f862a.html 百度上的这个说明很详细了 这里就简单注明一下: PS 时间轴:用来创建动画 ...
- 【MD5加密】MD5加密编码的坑
MD5 MD5即Message-Digest Algorithm (信息-摘要算法5),用于确保信息传输完整一致. 是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5 ...
- Material Design系列第三篇——Using the Material Theme
Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...
- 【前端积累】Awesome初识
前言 之所以要看这个,是因为在看到的一个网站里图表显示的全屏和缩小,anyway ,还是看一下咯~ 一.介绍 Font Awesome 字体为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何 ...