Mayor's posters (线段树加离散化)
个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下
解法:离散化,如下面的例子(题目的样例),因为单位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[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。
看题目吧。
- 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
#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0xffffff0;
const int length=;
const int hl=;
struct tree
{
int l,r;
bool tcover;
int mid()
{
return (l+r)/;
}
tree *left,*right;
};
tree Tree[length];
struct poster
{
int l,r;
};
poster pos[];
int hashb[hl];
int x[length];
int ntree=;
void builttree(tree *t,int l,int r)
{
t->l=l;
t->r=r;
t->tcover=false;
if(l==r) return ;
ntree++;
t->left=Tree+ntree;
ntree++;
t->right=Tree+ntree;
builttree(t->left,l,(l+r)/);
builttree(t->right,(l+r)/+,r);
}
bool post(tree *t,int l,int r)
{
if(t->tcover==true) return false;
if(t->l==l&&t->r==r){
t->tcover=true;
return true;
}
bool result;
if(r<=t->mid())
result=post(t->left,l,r);
else if(l>=t->mid()+)
result=post(t->right,l,r);
else
{
bool b1=post(t->left,l,t->mid());
bool b2=post(t->right,t->mid()+,r);
result=b1||b2;
}
if(t->left->tcover==true&&t->right->tcover==true)
t->tcover=true;
return result; }
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
ntree=;
scanf("%d",&n);
int accout=;
for(int i=;i<n;i++)
{
scanf("%d%d",&pos[i].l,&pos[i].r);
x[accout++]=pos[i].l;
x[accout++]=pos[i].r;
}
sort(x,x+accout);
int m=unique(x,x+accout)-x;
int treel=;
for(int i=;i<m;i++)
{
hashb[x[i]]=treel;
if(i<m-){
if(x[i+]-x[i]==)
treel++;
else treel+=;
}
}
builttree(Tree,,treel);
int sum=;
for(int i=n-;i>=;i--)
if(post(Tree,hashb[pos[i].l],hashb[pos[i].r]))
sum++;
printf("%d\n",sum); }
return ;
}
Mayor's posters (线段树加离散化)的更多相关文章
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
随机推荐
- windows通过ftp下载linux文件
# windows 下载 linux的文件>> ftp <domain_or_ip>>> <input_username>>> <in ...
- 对”唯一键可以包含NULL值,并且每个NULL值都是唯一的(即NULL!=NULL)“理解
因为最近在写一篇关于字符串模糊检索的论文,开始比较细致的研究数据库(MySQL)中的index问题,变到图书馆借了本<Effective MySQL之SQL语句最优化>(Ronald Br ...
- zookeeper部署搭建
zookeeper教程 1.先在linux系统中安装jdk并配置环境变量,可以参考下面的链接1 2.下载安装zookeeper软件 教程参考: 链接1:http://www.linuxidc.com/ ...
- C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServic
C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider DESCryptoS ...
- Docker容器技术-命令进阶
一.基本命令 1.Docker布尔型选项 使用某选项但没有提供参数,等同于把选项设置为true,要改变它的值,唯一的方法是将其设置成false. 找出一个选项的默认值是true还是false: [ro ...
- 断点续传JAVA实现
支持H5 Video标签播放,迅雷下载 /** * 断点续传工具 * @author lxycx_xc * 时间:2017年11月30日 */ public class BreakpointResum ...
- 从引物序列出发查找pcr产物的内容和在基因组上的位置
1.利用primer_blast工具,找出这对引物序列在基因组上的位置: 结果大概会像这样: 2.这些结果都是根据hg38基因组来定位的,转换成hg19: 利用UCSCde hgLiftover 在线 ...
- js原生插件格式解析
一个合格的插件必须满足以下要求: 1.拥有独立作用域与用户作用域隔离,插件内的私有变量不可影响用户定义的变量 2.拥有默认参数 3.提供配置方法让用户可改变参数 4.提供监听接口,以监听页面操作 5. ...
- class_exists — 检查类是否已定义
class_exists — 检查类是否已定义 bool class_exists ( string $class_name [, bool $autoload = true ] ) 检查指定的类是否 ...
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
原文链接:http://www.cnblogs.com/zouzf/p/3984628.html C++和C#互调时经常会带一些参数过去例如最常见的字符串,如果字符串里有中文的话,会发现传递过去后变成 ...