POJ2528 Mayor's posters(线段树+离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000)。求出最后还能看见多少张海报。
分析 : 很容易想到利用线段树来成段置换,最后统计总区间不同数的个数。但是这里有一个问题,就是区间可以很大,线段树开不了那么大的空间,遂想能不能离散化。实际上只记录坐标的相对大小进行离散化最后是不影响我们计算的,但是光是普通的离散化是不行的,就是我们贴海报的实际意义是对(l, r)段进行添加,而不是对于这个区间的点进行添加,是段树不是点树,如果这样普通离散化的话就会出现一个问题,比如数据1-10、1-4、6-10 行的,我们离散化后是1-4、1-2、3-4 ,不离散的结果是 3 但是离散化后再计算就是 2 了!原因就是我们是成段更新而不是点更新,进行添加海报的(l, r)的意义是对这一段进行添加,而离散化之后将原本不相邻的点变成了相邻的点,就导致了上面例子 4 - 6被覆盖了!解决这个问题的方法就是,在离散化的时候,将原本不相邻的两个点中间添加一个数,来表示中间是有“缝隙”的。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1
const int maxn = ;
bool hash[maxn];
int li[maxn] , ri[maxn];
int X[maxn<<];
int col[maxn<<];
int cnt; void PushDown(int rt) { if (col[rt] != -) { col[rt<<] = col[rt<<|] = col[rt]; col[rt] = -; } }
void update(int L,int R,int c,int l,int r,int rt) { if (L <= l && r <= R) { col[rt] = c; return ; }
PushDown(rt); int m = (l + r) >> ; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); } void query(int l,int r,int rt) { if (col[rt] != -) { if (!hash[col[rt]]) cnt ++; hash[ col[rt] ] = true; return ;
}
if (l == r) return ;
int m = (l + r) >> ;
query(lson);
query(rson); }
int Bin(int key,int n,int X[]) { int l = , r = n - ; while (l <= r) { int m = (l + r) >> ; if (X[m] == key) return m; if (X[m] < key) l = m + ; else r = m - ; } return -; } int main() { int T , n; scanf("%d",&T); while (T --) {
// memset (col,0,sizeof(col));
scanf("%d",&n); int nn = ; for (int i = ; i < n ; i ++) { scanf("%d%d",&li[i] , &ri[i]);
X[nn++] = li[i];///记录所有出现的点
X[nn++] = ri[i]; }
sort(X , X + nn);
int m = unique(X,X+nn)-X;///去重
///在不相邻的点中间添加一个数
for (int i = m - ; i > ; i --) {
if (X[i] != X[i-] + ) X[m ++] = X[i-] + ; }
sort(X , X + m);
memset(col , - , sizeof(col));
for (int i = ; i < n ; i ++) {
int l = lower_bound(X,X+m,li[i])-X;////在离散化之后的坐标系arr中寻找左端点
int r = lower_bound(X,X+m,ri[i])-X;
// int l = Bin(li[i] , m , X);
//int r = Bin(ri[i] , m , X);
update(l , r , i , , m , );
}
cnt = ;
memset(hash , false , sizeof(hash));
query( , m , );
printf("%d\n",cnt); } return ; }
POJ2528 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(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- 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 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
随机推荐
- request 10.0 模块安装
https://pypi.python.org/packages/49/6f/183063f01aae1e025cf0130772b55848750a2f3a89bfa11b385b35d7329d ...
- VC++ 6.0 快捷键
多行注释的快捷键 详细步骤 工具栏上右键-〉Customize-〉“Add-ins and Macro Files”tab页,把SAMPLE前面打上钩-〉“Commands”tab页,Categ ...
- sqlplus--sqlldr基础运用
a.ctl load data infile * ...
- PCL—点云滤波(初步处理)
博客转载自:http://www.cnblogs.com/ironstark/p/4991232.html 点云滤波的概念 点云滤波是点云处理的基本步骤,也是进行 high level 三维图像处理之 ...
- c# 新中新二代身份证阅读,包含头像,支持华视
需要用到dll和文件: 其中3个dll文件是需要调用的dll,license.dat文件为解压图片的授权文件 以下是需要用到的dll里面的方法: /************************端口 ...
- 硬链接与软链接有什么不同(ln)
Linux建立的链接有两种方式 如Windows系统下的快捷方式(.lnk)相似的东东 分为硬链接(Hard Link)和软链接(Symbolic Link)也叫符号链接 默认情况下,ln命令产生硬链 ...
- 安装visual_Paradigm,时序图的应用
此安装包已经上传到sunny的百度网盘. 删除,即,右击别的地方,然后,选择delete即可. 拖箭头,拖到某个实体上,再松开,会自动连线. 很好的一款画图工具.
- java 使用simpleDateFormat格式化日期 时间.RP
首先了解一下格式化日志的所有表示. 时间日期标识符: yyyy:年 MM:月 dd:日 hh:1~12小时制(1-12) HH:24小时制(0-23) mm:分 ss:秒 S:毫秒 E:星期几 D:一 ...
- Mysql 大数据量导入程序
Mysql 大数据量导入程序<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...
- 用Pdg2.DLL解码PDG的境界
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 一.入门级原理:按照<用BCB实现超星格式转换为BMP格式>中说的方法调用Pdg2.DLL接 ...