<题目链接>

题目大意:

往一堵墙上贴海报,依次输出这些海报张贴的范围,这些海报能够相互覆盖,问最后能够看见几张海报?

解题分析:

由于是给出每张海报的区间,所以在这些区间内的很多点可能用不上,所以我们采用离散化,将这个大的区间映射到一个更小更紧凑的区间。

但是只是这样简单的离散化是错误的, 如三张海报为: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 <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std; #define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
const int maxn=+;
int x[maxn<<],tr[maxn<<],lx[maxn<<],rx[maxn<<];
set<int>s; void Pushdown(int rt){
tr[rt<<]=tr[rt<<|]=tr[rt];
tr[rt]=-;
} void update(int rt,int l,int r,int L,int R,int c){
if(L<=l&&r<=R){
tr[rt]=c;
return;
}
if(tr[rt]!=-)Pushdown(rt); //如果tr[rt]==-1,说明不需要将该点的值Pushdown
int mid=(l+r)>>;
if(L<=mid)update(Lson,L,R,c);
if(R>mid)update(Rson,L,R,c);
} void query(int rt,int l,int r){
if(tr[rt]!=-){ //因为update的时候,只要该节点的区间包含在要求修改的区间内,就直接将值赋给该节点了,不会继续向下更新,所以,不用一直查询到子节点
s.insert(tr[rt]); //用set来去掉重复的标号
return;
}
if(l==r)return;
if(tr[rt]!=-)Pushdown(rt);
int mid=(l+r)>>;
query(Lson);
query(Rson);
} int main(){
int T;scanf("%d",&T);
while(T--){
memset(tr,-,sizeof(tr));
int cnt=;
int n;scanf("%d",&n);
s.clear();
for(int i=;i<=n;i++){
scanf("%d%d",&lx[i],&rx[i]);
x[++cnt]=lx[i];
x[++cnt]=rx[i];
}
sort(x+,x++cnt);
int num=;
for(int i=;i<=cnt;i++){
if(x[i]!=x[i-])x[++num]=x[i]; //去重
}
for(int i=num;i>=;i--){
if(x[i]-x[i-]>)x[++num]=x[i]-; //如果两个点之间间距>1,那么在它们之间插入一个点
}
sort(x+,x++num);
for(int i=;i<=n;i++){
int le=lower_bound(x+,x++num,lx[i])-x; //找到该点离散化后的坐标
int ri=lower_bound(x+,x++num,rx[i])-x;
update(,,num,le,ri,i); //将这段区间染成 i
}
query(,,num); //查找整个离散化后的区域总共有多少种标号
printf("%d\n",s.size());
}
return ;
}

2018-09-22

poj 2528 Mayor’s posters 【离散化】+【线段树】的更多相关文章

  1. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  2. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  3. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

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

  4. POJ 2528 Mayor's posters(线段树/区间更新 离散化)

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

  5. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  6. POJ 2528 ——Mayor's posters(线段树+区间操作)

    Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...

  7. (中等) POJ 2528 Mayor's posters , 离散+线段树。

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  8. POJ 2528 Mayor's posters(线段树染色问题+离散化)

    http://poj.org/problem?id=2528 题意: 给出一面无限长的墙,现在往墙上依次贴海报,问最后还能看见多少张海报. 题意:这道题目就相当于对x轴染色,然后计算出最后还能看见多少 ...

  9. POJ 2528 Mayor's posters(线段树)

    点我看题目 题意 :建一堵墙粘贴海报,每个候选人只能贴一张海报,海报的高度与墙一样高,一张海报的宽度是整数个单位,墙被划分为若干个部分,每个部分的宽度为一个单位,每张海报完全的覆盖一段连续的墙体,墙体 ...

  10. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

随机推荐

  1. SpringMVC拦截器与异常处理

    点击查看上一章 在我们SpringMVC中也可以使用拦截器对用户的请求进行拦截,用户可以自定义拦截器来实现特定的功能.自定义拦截器必须要实现HandlerInterceptor接口 package c ...

  2. 【python】获取http响应

    一个相对完整的http请求,输入ip和端口,输出响应码,响应头,响应体,是否超时,以及出错时的错误信息 处理包括: 1.协议处理,如果是443用https,其他用http 2.HTTPError处理, ...

  3. ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mysql'

    原因:修改数据库账号时删除了默认的localhost root,  新建了% root 但没有赋予全部权限; 解决方法: 1.关闭数据库# mysqld stop 2.在my.cnf里加入skip-g ...

  4. 最小标示法模板 poj1509

    最小标示法:给定一个字符串,不断将其最后一个字符放到开头,最终会得到n个字符串,称这n个字符串循环同构,这些字符串中字典序最小的一个,就是最小表示法 #include<iostream> ...

  5. You are my brother

    问题 : You are my brother 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Little A gets to know a new friend, Little B, ...

  6. Python游戏编程(Pygame)

    安装Pygame pip install pygame C:\Users> pip install pygame Collecting pygame Downloading https://fi ...

  7. ActiveMQ使用的设计模式

    注:接收不需要连接池,而发送需要连接池,是因为,接收在启动项目时就要注册监听,数目是固定的,而发送则会随着时间数目不断在变动,需要连接池,性能更优. 重点代码: private static void ...

  8. js cookie 工具

    var CookieUtil = { get: function(name) { var cookieName = encodeURIComponent(name) + "=", ...

  9. python练习册0004题

    在任意一个英文文档中,统计单词出现的次数, 分析: 本题不是很难,单词通常以空格隔开,但是有些单词后面跟一些特殊符号,只需把这些特殊符号替换掉就可以了, 代码一 import re file_name ...

  10. Windows Azure 搭建网络代理 Proxy

    额 题目起的有点大 其实就是在 Linux 上使用代理 不过是用的 Azure 上的 Liunx 虚拟机而已 如何在 Azure 上搭建 VPN 见上篇:http://www.cnblogs.com/ ...