题意 : 在墙上贴海报, 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. JavaScript的流程控制语句

    JS的核心ECMAScript规定的流程控制语句和其他的程序设计语言还是蛮相似的.我们选择一些实用的例子来看一下这些语句.顺序结构我们在这里就不再提到,直接说条件和循环以及其他语句.一.条件选择结构  ...

  2. SNNU女装T台走秀(状压dp)

    呜啦啦啦啦啦啦~~!!SNNU首届女装T走秀大赛开始了! 本次比赛共有N名队员希望参加比赛:ddjing希望这次比赛尽可能的吸睛,因此他决定对N名队员进行一次海选: 多亏ddjing有一双发现美的眼睛 ...

  3. Codeforces #536 div2 E (1106E)Lunar New Year and Red Envelopes (DP)

    题意:过年了,Bob要抢红包.抢红包的时间段为1 - n,有m个红包,每个红包有三个属性:st(红包出现的时间), ed(红包消失的时间),d(如果抢了这个红包,能够抢下一个红包的时间),w(红包的收 ...

  4. Codeforces #345div1 C Table Compression (650C) 并查集

    题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小. 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立 ...

  5. Linux 使用静态库注意事项

    1. 静态库一定要放在生成文件后面 gcc main.c -o main libhello.a 2. 使用静态库时一定要连接所有用到的静态库 gcc main.c -o main liba.a lib ...

  6. noi.ac day1t3 Sort

    传送门 分析 快排的原理是以任意一个数为标准,然后把所有小于它的数换到它的左边,所有大于它的数换到它的右边.我们就使用快排的思路,分治整个区间.对于每个区间以排好序的这个数列的中间位置的值为标准,然后 ...

  7. noi.ac day1t1 candy

    传送门 分析 我们知道如果设A,B分别为将两家店从大到小排序之后各自的前缀和,则 Ans=Max{Min{A[i],B[j]}-W*(i+j)}. 为了得到这个Ans我们可以枚举两个数的Min,然后剩 ...

  8. Entity Framework Tutorial Basics(13):Database First

    Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...

  9. 并没有看起来那么简单leetcode Generate Parentheses

    问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...

  10. Algorithms - Bucket sort

    印象 图1 将元素分布在桶中 图2 元素在每个桶中排序 思想 桶排序将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序). 分析 时间复杂度: ...