题意 : 在墙上贴海报, 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. 工作的时候用到spring返回xml view查到此文章亲测可用

    spring mvc就是好,特别是rest风格的话,一个 org.springframework.web.servlet.view.ContentNegotiatingViewResolver就可以根 ...

  2. Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))

    对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 参考: Pytho ...

  3. NoSQL概述

  4. IDEA创建MAVEN 无骨架WEB 项目

    Idea创建maven带有骨架的web项目的时候,会缺少必要文件夹,而且会多出来一些我们不需要的东西 详见:IDEA创建Maven Web项目 所以我们也可以创建无骨架项目: 创建maven项目 不选 ...

  5. 给Activity切换过程添加动画效果

    首先,在资源文件中定义一些动画效果 例如: <scale android:duration="@android:integer/config_mediumAnimTime" ...

  6. 如何判断一个字符串是否是UTF8编码

    UTF8是以8bits即1Bytes为编码的最基本单位,当然也可以有基于16bits和32bits的形式,分别称为UTF16和UTF32,但目前用得不多,而UTF8则被广泛应用在文件储存和网络传输中. ...

  7. ROS Learning-001 安装 ROS indigo

    如何在 Ubuntu14.04 上安装 ROS indigo 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ...

  8. oracle创建数据库的语句

    首先 oracle严格来说表空间的概念和数据库的概念很像,为了理解的方便我们,可以把表空间就先当成数据库 我们在安装oracle的服务端的时候默认会安装一些,默认实例 1.建立表空间,现在解释下面语句 ...

  9. Understanding the Effective Receptive Field in Deep Convolutional Neural Networks

    Understanding the Effective Receptive Field in Deep Convolutional Neural Networks 理解深度卷积神经网络中的有效感受野 ...

  10. java的get请求

    package com.huazhu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputS ...