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 ...
随机推荐
- dijstra+输出路径总结
#include<iostream> #include<math.h> #include<memory.h> using namespace std; #defin ...
- DCloud-5+Runtime:杂项
ylbtech-DCloud-5+Runtime:杂项 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 ...
- 任意输入3个数,判断能否组成三角形(python)
任意输入3个数,判断能否组成三角形. 三角形:两边之和大于第三边 直角三角形:勾股定理 代码如下: # 输入合法性检查,必须输入正数,不支持科学计数法'''try: <语句>except ...
- Spring学习五
1: servlet生命周期: Servlet加载 -> 实例化-> 服务 -> 销毁 2:Servlet重要函数: init():在Servlet的生命周期中,仅 ...
- CreateRemoteThread 远程注入
在release中可以成功,在debug中被注入的程序停止工作 #pragma once#include "stdafx.h"#include <windows.h># ...
- 问题:C# TrimEnd 去掉最后的逗号;结果: C#中 TrimEnd()用法
C#中 TrimEnd()用法 ①去除最后的逗号 string str=ab,cd,ef,; str=str.TrimEnd(new char[] { ',' }); 返回结果则是:ab,cd,ef ...
- xftp的简单使用
1.下载并安装Xftp工具.打开Xftp工具,点击“新建”. 2.在“新建会话属性”中选择“名称”为主机命名,在“主机”栏输入主机IP,“协议”和“端口号”使用sftp和22,在“用户名”和“密码“栏 ...
- ffmpeg一揽子
avformat_alloc_output_context2().在基于FFmpeg的视音频编码器程序中,该函数通常是第一个调用的函数(除了组件注册函数av_register_all()).avfor ...
- 新创建的maven项目,显示的jdk版本与使用的不一致
解决:是在安装的maven中的setting.xml配置文件中添加 在setting.xml配置文件中的<profiles></profiles>这个元素中加以下代码 如果加上 ...
- Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法
Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法(2011-02-23 19:37:32) 转载▼ 今天在惠普服务器上安装数据库2008时, ...