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

离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。例如:
原数据: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. loadrunder之脚本篇——脚本基础知识和常用操作

    1)编码工具设置 自动补全输入Tools->General Options->Environment->Auto complete word 显示功能语法Tools->Genr ...

  2. iOS iPhone X 适配启动图片

    刚出了Xcode9正式版 迫不及待地下载 使用了 保密了这么久的iPhone X 模拟器 运行起来这个样子 上下都留白不正常 这必须匹配新的启动图才行,马上查苹果开发文档 get it!!!! 添加了 ...

  3. Python学习进程(15)常用内置函数

        本节介绍Python的一些常用的内置函数.     (1)cmp(x, y): cmp()函数比较 x 和 y 两个对象,并根据比较结果返回一个整数,如果 x<y,则返回-1:如果x&g ...

  4. php任务管理器 —— Jobby

    通过一个主crontab任务去维护别的任务 自定义的计划任务完全由PHP编写 任务的执行计划时间表设置与crontab的时间表设置语法一致 在指定的时间内只会运行一个任务 邮件告警异常退出任务 在ro ...

  5. 【Head First Servlets and JSP】笔记19:JavaBeans与JSP动作元素(<jsp:setProperty.....>、<jsp:getProperty.....>)

    内容来自imooc. 1.什么是JSP动作元素 2.在JSP页面中如何使用Javabeans <jsp:......>表示这是一个JSP动作元素 3.使用JSP动作元素创建JavaBean ...

  6. AngularJS 视图和路由

    在AngularJS之后引用angular-route  路由   ngRoute模块加载声明   AngularJS提供的when和otherwise两个方法来定义应用的路由   otherwise ...

  7. 一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

    一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

  8. linux下安装casperjs 开发运行环境

    casperjs是一个基于phantomjs的测试框架,使用python进行操作,所以一个完整的casperjs环境需要安装phantomjs和python. 1 phantomjs安装 到官网下载最 ...

  9. Kubernetes Resource Qoutas

    配置参数: spec.containers[].resources.limits.cpu spec.containers[].resources.limits.memory spec.containe ...

  10. 手写RateLimiter

    自定义注解 封装 如果需要让接口实现限流RateLimiter使用 网关:一般拦截所有的接口 实现限流 秒杀 抢购 或者大流量的接口才会实现限流.灵活 不是所有接口都需要限流  秒杀等接口需要限流 设 ...