题意 : 在墙上贴海报, 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(线段树+离散化)的更多相关文章

  1. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  2. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

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

  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 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  5. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  6. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  7. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  8. POJ2528:Mayor's posters(线段树区间更新+离散化)

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

  9. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

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

  10. POJ 2528 Mayor's posters (线段树+离散化)

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

随机推荐

  1. 自定义Android Studio方法注释模板

    前言 你们从Eclipse转到Android Studio的时候,是不是会怀念Eclipse的方法注释模版? 敲/**加回车,模板就出来了,而Android Studio却不能自定义(或者我没有找到) ...

  2. eclipse 中文版 变成 英文版 方法

    找到目录运行命令 “eclipse.exe -nl en”

  3. apache重写规则详解

    RewriteEngine on 为重写引擎开关,如果设为off,则任何重写规则定义将不被应用,该开关的另一好处就是如果为了临时拿掉重写规则,则改为off再重启动Apache即可,不必将下面一条条的重 ...

  4. ThinkPHP的URL模式

    ThinkPHP的URL模式有四种,默认是PATHINFO模式,其他三种分别为:普通模式.REWRITE和兼容模式. 一.PATHINFO模式 浏览器输入格式为: http://localhost/d ...

  5. Express响应方法

    下表中响应对象(res)的方法向客户端返回响应,终结请求响应的循环.如果在路由句柄中一个方法也不调用,来自客户端的请求会一直挂起. 方法 描述 res.download() 提示下载文件. res.e ...

  6. C++输出斐波那契数列的几种方法

    定义: 斐波那契数列指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 这个数列从第三项开始,每一项都等于前两项之和. 以输出斐波那 ...

  7. MySql 5.7中添加用户,新建数据库,用户授权,删除用户,修改密码

    转自http://blog.csdn.net/w690333243/article/details/76576952 1.新建用户 创建test用户,密码是1234. MySQL -u root -p ...

  8. SQl Server 函数篇 数学函数,字符串函数,转换函数,时间日期函数

    数据库中的函数和c#中的函数很相似 按顺序来, 这里价格特别的 print  可以再消息栏里打印东西 数学函数 ceiling()  取上限   不在乎小数点后面有多大,直接忽略 floor()   ...

  9. VMware内部错误解决办法

    虚拟机内部错误,不要担心不是致命错误,往往是由于你的配置被禁用了或者VMware运行权限不够导致 检查你的VMware虚拟网卡是否被禁用 检查你的VMware的运行权限,直接管理员运行就够够的了

  10. R: 给 dataframe 的某列赋值、分组、因子化

    ################################################### 问题:给某一列赋值.分组.分类.因子化   18.4.24 如何把 data.frame 中的某 ...