POJ2528Mayor's posters[线段树 离散化]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 59683 | Accepted: 17296 |
Description
- 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
Source
notonlysuccess大神的题解
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要的值来用,比如说区间[,],[,] 我们用不到[-∞,][,][,][,][,+∞]这些值,所以我只需要1000,,,2012就够了,将其分别映射到0,,,,在于复杂度就大大的降下来了 所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多 而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱) 给出下面两个简单的例子应该能体现普通离散化的缺陷: - - - - - - 为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[,,,] 如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[,,,,,],然后再做线段树就好了. 线段树功能:update:成段替换 query:简单hash
说一下离散化,用map完美TLE,改成1e7大数组因为memset太耗时,再改成二分搜索用下标快好多
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e4+,TREE=4e4+;
#define m (l+r)/2
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
//map<int,int> mp;
//int mp[10000009];
int mp[N<<];
int t[TREE<<];
int T,n,ll[N],rr[N],a[N<<];
inline void paint(int o,int v){
t[o]=v;
}
inline void pushDown(int o){
if(t[o]!=){
paint(lc,t[o]);
paint(rc,t[o]);
t[o]=;
}
}
void draw(int o,int l,int r,int ql,int qr,int v){//printf("draw %d %d %d\n",o,l,r);
if(ql<=l&&r<=qr) paint(o,v);
else{
pushDown(o);
if(ql<=m) draw(lson,ql,qr,v);
if(m<qr) draw(rson,ql,qr,v);
}
}
int vis[N],ans=;
void query(int o,int l,int r,int ql,int qr){//printf("quer %d %d %d\n",o,l,r);
if(t[o]){
if(!vis[t[o]]){ans++;vis[t[o]]=;}
}else{
if(l==r) return;
if(ql<=m) query(lson,ql,qr);
if(m<qr) query(rson,ql,qr);
}
}
int main(){
T=read();
while(T--){
n=read();
memset(t,,sizeof(t));
//mp.clear();
//memset(mp,0,sizeof(mp));
for(int i=;i<=n;i++){ll[i]=read();rr[i]=read();a[*i-]=ll[i];a[*i]=rr[i];}
sort(a+,a++*n);
int cnt=;
// mp[a[1]]=++cnt;
// for(int i=2;i<=2*n;i++)
// if(a[i]!=a[i-1]){
// //if(!mp.count(a[i]-1)) mp[a[i]-1]=++cnt;
// if(!mp[a[i]-1]) mp[a[i]-1]=++cnt;
// mp[a[i]]=++cnt;
// }
// for(int i=1;i<=n;i++){
// int ql=mp[ll[i]],qr=mp[rr[i]];//printf("hi %d %d\n",ql,qr);
// draw(1,1,cnt,ql,qr,i);
// }
//
for(int i=;i<=*n;i++) if(a[i]!=a[i-]) mp[++cnt]=a[i];
for(int i=cnt;i>=;i--) if(mp[i]!=mp[i-]+) mp[++cnt]=mp[i-]+;
sort(mp+,mp++cnt);
for(int i=;i<=n;i++){
int ql=lower_bound(mp+,mp++cnt,ll[i])-mp,qr=lower_bound(mp+,mp++cnt,rr[i])-mp;
draw(,,cnt,ql,qr,i);
}
ans=;
memset(vis,,sizeof(vis));
query(,,cnt,,cnt);
printf("%d\n",ans); //printf("\n\ncnt %d\n",cnt);
//for(int i=1;i<=10;i++) printf("mp %d %d\n",i,mp[i]);
}
}
POJ2528Mayor's posters[线段树 离散化]的更多相关文章
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ2528Mayor's posters 线段树,离散化技巧
题意:一个坐标轴从1~1e7,每次覆盖一个区间(li,ri),问最后可见区间有多少个(没有被其他区间挡住的) 线段树,按倒序考虑,贴上的地方记为1,每次看(li,ri)这个区间是否全是1,全是1就说明 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- 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(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
随机推荐
- 三分钟集成elmah xml 格式日志到mvc站点
1.通过nuget安装Elmah ELMAH on XML Log 会自动在web.config 文件中添加配置内容,默认不允许远程访问,日志访问路径是 Elmah.axd,不记录500错误 2.修改 ...
- c# asp.net4.0尚未在web服务器上注册
辗转无数论坛,感谢http://bbs.csdn.net/topics/390992746 microsoft官方文档: https://blogs.msdn.microsoft.com/webdev ...
- Oracle 数据库基础学习 (七) SQL语句综合练习
一.多表查询综合练习 1. 列出高于在30部门工作的所有人员的薪金的员工的姓名.部门名称.部门编号.部门人数 分析: 需要的员工信息: |-emp表:姓名.部门编号 |-dept表:部门名称.部门编 ...
- 【JAVA并发编程实战】5、构建高效且可伸缩的结果缓存
首先创建一个借口,用来表示耗费资源的计算 package cn.xf.cp.ch05; public interface Computable<A, V> { V compute(A ar ...
- float包裹性与破坏性及清除浮动几种方法
1. “清除浮动” ??准确的描述应该是“清除浮动造成的影响” 学习视频录制推荐的教程<CSS深入理解之float浮动> 2.如何清除浮动造成的影响??? 栗子 块级div元素包含一个内 ...
- 使用javascript生成的植物显示过程特效
查看效果:http://keleyi.com/keleyi/phtml/html5/33.htm .NET版本:http://keleyi.com/a/bjac/66mql4bc.htm 完整HTML ...
- JavaScript-数组去重由慢到快由繁到简
indexOf去重 Array.prototype.unique1 = function() { var arr = []; for (var i = 0; i < this.length; i ...
- jquery编写插件的方法
版权声明:作者原创,转载请注明出处! 编写插件的两种方式: 1.类级别开发插件(1%) 2.对象级别开发(99%) 类级别的静态开发就是给jquery添加静态方法,三种方式 1.添加新的全局函数 2 ...
- 使用 Jquery-UI 实现一次拖拽多个选中的元素操作
项目需要,实现一个拖放操作,要求每次可以拖拽选中的多个元素,释放到目标容器后可排序.考虑了一下,觉得jquery-ui比较合适,毕竟它提供了项目需要的交互性事件机制.拖拽.释放.排序.选择等效果.而在 ...
- 政府应急平台之GIS一张图-flex/java
开发语言是flex.java,开发平台是myeclise.eclise,后台数据库是oracel或sqlserver,开发接口是arcgis api for flex,提供以下的功能: 1.视频监控: ...