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 ...
随机推荐
- Jersey 入门与Javabean
Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service,它包含三个部分: 核心服务器(Core Server) 通过提供JSR 311中标准化的注释和AP ...
- JavaScript-this理解
javascript this可以绑定到:全局对象,自己定义的对象,用构造函数生成的对象,通过call或者apply更改绑定的对象 1.全局对象 function globalTest(nam ...
- 【RF库Collections测试】Keep In Dictionary
Name:Keep In DictionarySource:Collections <test library>Arguments:[ dictionary | *keys ]Keeps ...
- Jar命令
JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包;当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你打成JAR包时,它会自动生成. 一.ja ...
- Elasticseach的评分机制
lucene 的评分机制 elasticsearch是基于lucene的,所以他的评分机制也是基于lucene的.评分就是我们搜索的短语和索引中每篇文档的相关度打分. 如果没有干预评分算法的时候,每次 ...
- Python迭代器笔记
python中的三大器有迭代器,生成器,装饰器,本文重点讲解下迭代器的概念,使用,自定义迭代器等的介绍. 1.概念: 迭代器是一个对象,一个可以记住遍历位置的对象,迭代器对象从集合的第一个元素开始访问 ...
- TCPdump指定时间或者指定大小进行循环抓取报文
背景:我们用tcpdump工具循环抓取网卡上的报文,我们会遇到如下情况: 1. 抓取报文后隔指定的时间保存一次: 2. 抓取报文后达到指定的大小保存一次: 本文就这两种情况给出tcpdump的使用方法 ...
- React Native(五)——获取设备信息react-native-device-info
心酸史: 自从接触rn开始后,越来越多的引入第三方组件而开始的配置文件,让自己一再头疼: 明明是按照官方文档一步一步的配置,为什么别人可以做到的自己却屡屡出错,真是哭笑不得--从微信分享react-n ...
- 怎样在js中使用EL表达式
相信已经有很多人对如何在js中使用EL表达式存有困惑,各种引号的处理不胜其烦. 1.在js(嵌入jsp页面)中通过定义变量的方式使用EL表达式: 如:var url = '${param.url}'; ...
- input回车问题
今天有一个问题,就是input对象没有加任何事件自己回车导致跳到了404页面.处理的时候,并发现没找到回车事件的控制. 那么只有一种情况,就是自带的回车控制. 百度了一下,如下面博文里面的写法.我这边 ...