POJ - 2528Mayor's posters (离散化+线段树区间覆盖)
- 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
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
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是一个单位长度,将下面的
1 2 3 4 6 7 8 10
— — — — — — — —
1 2 3 4 5 6 7 8
离散化 X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[6] = 7; X[7] = 8,X[8] = 10;
于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),
最后统计不同颜色的段数。但是只是这样简单的离散化是错误的,
如三张海报为:1~10 1~4 6~10离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的1~4被染为1;
第二张海报时:墙的1~2被染为2,3~4仍为1
;第三张海报时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)
X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10
这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3最终,1~2为2,3为1,4~5为3,于是输出正确结果3。
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std; const int MAXN = ;
int T;
int N;
int x[MAXN] , y[MAXN];
int lisan[MAXN*];
bool vis[MAXN*];
int tree[MAXN*]; //离散化要开16倍空间
int size ;
int num = ;
int tmp;
int ans ;
void pushdown(int rt )
{
tree[rt*] = tree[rt];
tree[rt*+] = tree[rt];
tree[rt] = ;
}
void Update(int tpx ,int tpy ,int value, int l ,int r ,int rt)
{
if(l>=tpx&&r<=tpy)
{
tree[rt] = value;
return;
}
if(tree[rt]!=)
pushdown(rt);
int m = (l+r)/; if(tpx<=m)
{
Update(tpx,tpy,value,l,m,rt*);
}
if(tpy>m)
{
Update(tpx,tpy,value,m+,r,rt*+);
} }
void Query(int l ,int r ,int rt)
{
if(tree[rt]!=)
{
if(!vis[tree[rt]])
{
ans++;
vis[tree[rt]] = ;
}
return ;
}
if(l==r)
return;
if(tree[rt]!=)
{
pushdown(rt);
}
int m = (l+r)/;
Query(l,m,rt*);
Query(m+,r,rt*+);
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
num = ;
memset(tree,,sizeof(tree));
memset(vis,,sizeof(vis));
for(int i = ; i <= N ;i++)
{
scanf("%d%d",&x[i],&y[i]);
lisan[num++] = x[i];
lisan[num++] = y[i];
}
sort(lisan,lisan+num); //排序后离散化
size = unique(lisan,lisan+num)-lisan; //获得离散化后的长度
tmp = size;
for(int i = ; i < tmp ;i++)
{
if(lisan[i]-lisan[i-]>) //这样离散化才正确;见上面分析;
{
lisan[size++] = lisan[i-] + ;
}
}
sort(lisan,lisan+size); //再排序一次
for(int i = ; i <= N ;i++ )
{
int tpx = lower_bound(lisan,lisan+size,x[i])-lisan;//获取位置
int tpy = lower_bound(lisan,lisan+size,y[i])-lisan;
Update(tpx,tpy,i,,size-,);
}
ans = ;
Query(,size-,);
printf("%d\n",ans);
}
return ;
}
POJ - 2528Mayor's posters (离散化+线段树区间覆盖)的更多相关文章
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)
http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...
- [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】
Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...
- 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 (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- POJ 2528 ——Mayor's posters(线段树+区间操作)
Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
随机推荐
- GXT4.0 BorderLayoutContainer布局
T字型布局. @Override public void onModuleLoad() { BorderLayoutContainer con = new BorderLayoutContainer( ...
- JDK7和JDK8新特性
参考:http://www.cnblogs.com/langtianya/p/3757993.html JDK 1.7 新特性 1,switch中可以使用字串了 String s = "te ...
- HDU1560(迭代加深搜索)
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- mysql存储过程获取sqlstate message_text
群里有人询问,在mysql的proc中如何获取错误信息.错误编号呢?我们知道在oracle.mssql中比较简单: oracle中sqlcode,sqlerrm ;mssql中ERROR_PROCED ...
- ORA-00904: 标识符无效——解决方案
转自:https://blog.csdn.net/jajavaja/article/details/49122639 建表时列名用双引号引着(用Navicat工具建表默认是加上双引号的),java连接 ...
- DAY11-MYSQL多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...
- SSH简单搭建
本项目使用Struts2+spring3+hibernate3: 第一步:引入jar包,具体需要哪些包根据实际情况加入.注意:把jar包导入后需要对所有包Add to Build Path;然后对工程 ...
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...
- schedule与scheduleAtFixedRate比较
schedule与scheduleAtFixedRate: 不延时: schedule(TimerTask, Date runDate, long period)方法任务不延时----Date类型 i ...
- win10获取超级管理员权限脚本实现
建立一个TXT文件,把下面的脚本贴到里面,然后把后缀改成reg格式,双击添加到注册表就可以了, win10_1703版本亲测可用.... Windows Registry Editor Version ...