poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters
题目链接:
http://poj.org/problem?id=2528
思路:
线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来)
假设给出:
1~10
1~4
7-10
最后可以看见三张海报
如果离散化的时候不注意,就会变成
1 4 7 10(原始)
1 2 3 4 (离散化)
转化为:
1~4
1~2
3~4
这样的话最后只能看见两张海报
解决办法,如果原数据去重排序后相互之间差值大于1,则在他们之间再插入一个数值,使得大于前者小于后者即可
我感觉这道题目用线段树还不够快,因为最后查询,遍历了整整一棵树...
代码:
#include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 10000005;
struct node {
int l,r,color,lazy;
inline void update(int val) {
color=val;
lazy=val;
}
} tr[maxn*4];
int a[maxn],b[maxn],c[maxn*2];
set<int> se;
inline void push_down(int s) {
int lazyval = tr[s].lazy;
if(!lazyval) return;
tr[s<<1].update(lazyval);
tr[s<<1|1].update(lazyval);
tr[s].lazy=0;
}
void build(int s, int l, int r) {
tr[s].l=l;tr[s].r=r;
tr[s].lazy=tr[s].color=0;
if(l==r) return;
int mid=(l+r)>>1;
build(s<<1,l,mid);
build(s<<1|1,mid+1,r);
}
void update(int s, int l, int r, int color) {
if(tr[s].l==l&&tr[s].r==r) {
tr[s].update(color);
return;
}
push_down(s);
int mid=(tr[s].l+tr[s].r)>>1;
if(r<=mid) update(s<<1,l,r,color);
else if(l>mid) update(s<<1|1,l,r,color);
else {
update(s<<1,l,mid,color);
update(s<<1|1,mid+1,r,color);
}
}
void query(int s) {
if(tr[s].l==tr[s].r) {
if(!tr[s].color) return;
se.insert(tr[s].color);
return;
}
push_down(s);
query(s<<1);
query(s<<1|1);
}
inline void init() {
se.clear();
}
int main() {
int t,m,tot,index;
scanf("%d",&t);
while(t--) {
init();
tot=1;
scanf("%d",&m);
for(int i=1;i<=m;++i) {
scanf("%d %d",&a[i],&b[i]);
c[tot]=a[i];++tot;
c[tot]=b[i];++tot;
}
/*离散化开始*/
index=tot;
--tot;
sort(c+1,c+index);
for(int i=2;i<=tot;++i) {
if(c[i]-c[i-1]>1) {
c[index]=c[i-1]+1;
index++;
}
}
sort(c+1,c+index);
tot=unique(c+1,c+index)-c;
--tot;
build(1,1,tot);
for(int i=1;i<=m;++i){
a[i]=lower_bound(c+1,c+tot+1,a[i])-(c+1)+1;
b[i]=lower_bound(c+1,c+tot+1,b[i])-(c+1)+1;
update(1,a[i],b[i],i);
}
/*离散化完成*/
query(1);
printf("%d\n",se.size());
}
return 0;
}
poj 2528 Mayor's posters 线段树+离散化技巧的更多相关文章
- 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 ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
随机推荐
- 开源纯C#工控网关+组态软件(四)上下位机通讯原理
一. 网关的功能:承上启下 最近有点忙,更新慢了.感谢园友们给予的支持,现在github上已经有.目标是最好的开源组态,看来又近一步^^ 之前有提到网关是物联网的关键环节,它的作用就是承上启下. ...
- 测试服务搭建之centos7下安装java
一 安装Java 1 创建普通用户 useradd wujian passwd wujian 提示输入密码:[输入wujian的密码] user:wujian group:root passwd:12 ...
- mysql中多个left join子查询写法以及别名用法
不多说 直接上语句 SELECT a.id, a.thumbNail, a. NAME, a.marketPrice, a.memberPrice, ...
- 根据文字计算出label的高度
ios7.0之前用: [strtestsizeWithFont:ContentFontconstrainedToSize:CGSizeMake(ScreenWeight -20, 1000) line ...
- Vue源码后记-钩子函数
vue源码的马拉松跑完了,可以放松一下写点小东西,其实源码讲20节都讲不完,跳了好多地方. 本人技术有限,无法跟大神一样,模拟vue手把手搭建一个MVVM框架,然后再分析原理,只能以门外汉的姿态简单过 ...
- Install a Jenkins on Ubuntu system
================================================================================ Jenkins Environment ...
- 2.安装Nginx
安装稳定版本的nginx 1.为CentOS系统安装yum仓库,创建文件 /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=htt ...
- 使用TP5创建一个REST API
原文在这里 : http://hmw.iteye.com/blog/1190827 tp自带的api,get请求接口 /** * 显示资源列表 * * @return \think\Response ...
- PaaS 调研:GAE与 AWS(上)
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:韩伟 起因 PaaS作为"云"的概念,已经流行了很久.从使用的角度上看,似乎就是:写一个PHP,然后可以直接传到服务 ...
- vue-cli 前端开发,后台接口跨域代理调试问题
使用 webpack的方式开发的时候,前台开发过程中需要调用很多后台的数据接口,但是通常前后台分离的开发方式,后台的接口数据很可能是不方便或者是不能在前端同学的电脑上运行的,也就出现了所谓的跨域问题. ...