个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下

离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。例如:
原数据:1,999,100000,15;处理后:1,3,4,2;
原数据:{100,200},{20,50000},{1,400};
处理后:{3,4},{2,6},{1,5};
线段树还有一个重要点就是信息的更新和lazy标志的建立,因为每一个节点除了根节点外都有父节点,而如果俩个子节点都满足同样的情况是
就可以直接对父节点进行lazy标志,只要对最大的区间进行更新就好了,最后在下放下去,比如这一题,当俩个子节点都完全被覆盖时,
此时的父节点也就被覆盖了,所以要在程序最后进行更新。
本来这一题还有一个点不清楚,就是为什么离散化点时,不相邻的要建立个中间点,后面明白了。就好比如果1-10,1-4,6-10,你只分成
这三个区间1,4,6,10
则你最终只会得到2个可见,因为4-6这一段你并没有保存并且显示,这就是一些题目中卡在中间bug问题了吧,需要谨慎处理。
还有这题是后面的后贴,如果你先将前面的标志的话,后面的覆盖起来就会非常麻烦,所以选择从后面开始更新,这些都需要思维的转弯。

解法:离散化,如下面的例子(题目的样例),因为单位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。

看题目吧。

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • 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

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l 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

For each input data set print the number of visible posters after all the posters are placed.

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 (线段树加离散化)的更多相关文章

  1. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  3. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  4. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  5. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  6. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  7. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  8. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  9. POJ 2528 Mayor's posters (线段树+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:75394   Accepted: 21747 ...

随机推荐

  1. 026_默认的MapReduce Driver(最小驱动问题)

    1. 最小配置的MapReduce Driver 读取输入文件中的内容,输出到指定目录的输出文件中,此时文件中的内容为: Key---输入文件每行内容的起始位置. Value---输入文件每行的原始内 ...

  2. 09_Hadoop启动或停止的三种方式及启动脚本

    1.Hadoop启动或停止 1)第一种方式 分别启动 HDFS 和 MapReduce,命令如下: 启动: $ start-dfs.sh $ start-mapred.sh 停止: $ stop-ma ...

  3. Android开发BUG及解决方法2

    错误描述: 错误分析: 程序依赖的两个包冲突 解决方法: 在build.gradle文件中android节点下加packagingOptions节点

  4. mysql性能优化的一些建议

    mysql性能优化的一些建议 1.EXPLAIN 你的 SELECT 查询 查看rows列可以让我们找到潜在的性能问题. 2.为关键字段添加索引,比如:where, order by, group b ...

  5. C/C++ 字符串操作函数 思维导图梳理

    这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰

  6. Kubernetes Horizontal Pod Autoscaler

    非常牛逼的技术,目前最新的版本支持众多的Feature HPA功能需要Heapster收集的CPU.内存等数据作为支撑 配置示例: apiVersion: autoscaling/v2beta1 ki ...

  7. C++11 里lambda表达式的学习

    最近看到很多关于C++11的文档,有些是我不怎么用到,所以就略过去了,但是lambda表达式还是比较常用的,其实最开始学习python的时候就觉得lambda这个比较高级,为什么C++这么弱.果然C+ ...

  8. 安装MySQL5.7.18遇到的坑

    最近才注意到MySQL的各个版本之间差别还挺大的,比如5.5.x版本的timestamp类型列只能有一个设置为default CURRENT_TIMESTAMP的,于是尝试了换成一个新版本是mysql ...

  9. centos_mysql5.6.21_rpm安装

    1.查看操作系统相关信息.[root@linuxidc ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m [root@l ...

  10. Linux 修改DNS解决 Could not retrieve mirrorlist" 报错

    CentOS yum有时出现“Could not retrieve mirrorlist ”的解决办法——resolv.conf的配置 或者IP配置文件上写入 缺少DNS引起的问题1. 无法识别域名 ...