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 ...
随机推荐
- mongodb中的__v字段
"__v"是"versionKey"的简写,当每一个文档由mongoose创建时就会自动添加,代表这该文档的版本,此属性可配置修改,默认为"__v&q ...
- Qt如何重写虚函数
eg:QWidget的有个虚函数,KeyPressEvent,当它的子类获得焦点的时候,如果有任何按键按下,就会触发这个虚函数. 1.在mainwindow.h中声明此虚函数 protected:vo ...
- CF703D Mishka and Interesting sum
题意:给定一个1e6长度的值域1e9的数组.每次给定询问,询问区间内出现偶数次的数的异或和. 题解:首先很显然,每一次询问的答案,等于这个区间所有不同元素异或和异或上区间异或和.(因为出现偶数次的对区 ...
- table-layout 属性
最近被测试提了一个bug,表单的某个字段有1300的字数限制,测试填了1300字,提交后,表格上的呈现丑爆了,那个字段的所在的列撑满了整个表格,其他列被压缩的很小. 后来知道了table-layout ...
- CSS3的transform属性
CSS3的一些属性可能比较新,有一些书从国外翻译到国内的时间上会延缓1-2年.所以有一些东西还需要及时整理. 下面说一下CSS3的一个属性:transform 其实字面上的意思已经很明显了就是变化,变 ...
- elasticsearch中如何手动控制全文检索结果的精准度
1.为帖子数据增加标题字段 POST /forum/article/_bulk{ "update": { "_id": "1"} }{ &q ...
- hdu 5663 Hillan and the girl 莫比乌斯反演
Hillan and the girl Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/O ...
- mybatis 的加载与缓存
在这里我要说一下 我没有亲自去敲案例只是看了 我兄弟的文档 在这里和大家说一下 延迟加载: 所谓延迟加载是什么? 从字面意思理解到的是等一会再加载 从行为分析,他主要是缓解数据库压力,提高性能的意义 ...
- review04
在编译源文件时,所用到的其他源文件也会被自动编译.这是因为它会使用到其他源文件产生的字节码文件. javac A.java A指的是源文件的名称 java B B指的是源文件中有main方法的类. 虽 ...
- linux(centos)安装Maven
搭建maven 下载jar包,将apache-maven-3.2.2-bin.tar.gz上传到服务器 1.解压 tar -zvxf apache-maven-3.2.2-bin.tar.gz 2.移 ...