题目链接
Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 92628 Accepted: 26452
Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
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

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,… , ri.
Output

For each input data set print the number of visible posters after all the posters are placed.

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

Alberta Collegiate Programming Contest 2003.10.18

由于题目的数据是达到了1e7,简单的用线段树大概率会超时,但是n只到1e4,所以这里考虑用离散化来写。
首先我们先来讲一下什么是离散化。
就拿上面的数据举例:
把出现的下标从小到大排序,就有了:1 2 3 4 6 7 8 10。
所以我们定义其在数组中有 :
a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4, a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 10.
一下子数据从原来的10,变成了8,所以这里最多只有2e4个数据,对应这道题,离散化是一个完全合理的算法。

一开始不知道线段树是如何离散化的,当我看完线段树是如何离散之后,相信满满的去写这道题,发现我还是太年轻了, 这道题目离散化的过程有个坑。
例如:
1
3
1 6
5 6
1 3
这组数据,离散化后a[1] = 1, a[2] = 3, a[3] = 5, a[4] = 6
进行1 6后所有颜色都是1,
进行5 6后1 2 是颜色1,3 4 是颜色2,
进行1 3后1 2 是颜色3,3 4 是颜色2,
最后我们得到答案是 2,
但是仔细想想这个答案对了吗,显然是错的,对于原来的数据3 ~ 5之间还是颜色1,
因此我们的答案应该是3。
所以在离散化的时候我们应该对与坐标间距离大于1的在中间插入一个数。这样就可以保证颜色不会消失了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mid ((l + r) >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
const int N = 1e4 + 10;
int visit[N << 3], pos[N << 3], li[N], ri[N], n, ans, color[N << 4];
void push_down(int rt) {if(color[rt]) {color[ls] = color[rs] = color[rt];color[rt] = 0;}
}
void update(int rt, int l, int  r, int L, int R, int k) {if(l >= L && r <= R) {color[rt] = k;return ;}push_down(rt);if(L <= mid)   update(lson, L, R, k);if(R > mid)    update(rson, L, R, k);
}
void query(int rt, int l, int r) {if(color[rt]) {if(!visit[color[rt]]) {visit[color[rt]]++;ans++;}return ;}if(l == r) return ;query(lson);query(rson);
}
int main() {int m, t;scanf("%d", &t);while(t--) {memset(visit, 0, sizeof visit);memset(color, 0, sizeof color);scanf("%d", &m);n = 0;for(int i = 1; i <= m; i++) {scanf("%d %d", &li[i], &ri[i]);pos[++n] = li[i];pos[++n] = ri[i];}sort(pos + 1, pos + n + 1);int temp = n;n = 1;for(int i = 2; i <= temp; i++)    if(pos[i] != pos[i - 1])   pos[++n] = pos[i];//去重。for(int i = n ; i > 1; i--)   if(pos[i] - pos[i - 1] > 1)  pos[++n] = pos[i - 1] + 1;//插入中间值。sort(pos + 1, pos + n + 1);for(int i = 1; i <= m; i++) {int l = lower_bound(pos + 1, pos + n + 1, li[i]) - pos;int r = lower_bound(pos + 1, pos + n + 1, ri[i]) - pos;update(1, 1, n, l, r, i);}ans = 0;query(1, 1, n);printf("%d\n", ans);}return 0;
}

poj 2528 线段树离散化+染色相关推荐

  1. POJ - 2528 线段树+离散化

    其实很早就在白书上的常用技巧上 看到离散化的操作,但是之前一直没遇到过需要离散化的题目(应该是我太菜的缘故),所以一直也没怎么重视,下面说说这道题目的考点,也就是离散化. 什么是离散化呢?请先自行百度 ...

  2. poj 1177 线段树+离散化+扫描线 求矩形并的轮廓长

    Picture 题意:求矩形面积并额轮廓长 解法:扫描线+离散化+线段树 做法:等于更新操作前后的tree[1].len差,做法这么巧妙实际我也不知道为什么.差不多的意思就是更新操作的cover变化长 ...

  3. poj 2777(线段树+区间染色)

    解题思路:这道题利用了线段树+位运算的思想,由于颜色的种类只有30种,所以int可以存下来,所以我们在线段树的节点里面加上status的状态信息,表示这段区间内的颜色信息,而且我们可以知道,父节点的s ...

  4. 线段树区间染色 浮水法 学习小记 Poj 2777 + Poj 2528

    复习过的东西必须时常拿来练练,虽说都是水题.... Poj 2777 典型的区间染色问题,貌似我的写法为了不数组越界必须多开一倍的数组空间.....还没有想到解决方法,有机会参考一下别人的写法. Po ...

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

    1 /* 2 poj 2528 Mayor's posters 3 线段树 + 离散化 4 5 离散化的理解: 6 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用 ...

  6. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  7. poj2528贴海报(线段树离散化)

    //poj2528贴海报(线段树离散化) #include<cstring> #include<iostream> #include<cstdio> #includ ...

  8. HDOJ 2492 Ping pong 线段树+离散化

    //2492 Ping pong 线段树+离散化 /* 题意: 有一陀人从左到右排成一排,每个人有一个唯一的技能值,每个人都找其他人比赛, 比赛前要再找一个人做裁判,裁判的技能值不能比这两个人都高,也 ...

  9. POJ - 2528 Mayor's posters (浮水法+线段树/离散化+线段树)

    题目链接 题意: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 分析1 离散 ...

最新文章

  1. 基于Android和SpringBoot的购物App
  2. Linux的watch命令--实时监测命令的运行结果
  3. 图片转字符 android,转字符图app下载-转字符图 安卓版v2.4-PC6安卓网
  4. 戴尔服务器改win7系统,戴尔dell预装win10怎么改win7系统
  5. SAP Cloud for Customer里的Sales Lead和Lead
  6. linux中memcpy实现分析,ARM64 的 memcpy 优化与实现
  7. 什么是Vim,Vim及其安装
  8. springboot application.properties server.port配置小问题
  9. Effective C# Item33:限制类型的可见性
  10. 并查集 | 1107
  11. MySQL 千万级 数据库或大表优化
  12. php导出excel出现乱码,完美解决phpexcel导出到xls文件出现乱码的问题
  13. Linux安装Nvidia显卡驱动
  14. maven本地仓库配置
  15. 百度地图点聚合仿链家定位点多级聚合,且滑动、刷新加载定位点
  16. 便捷开票二维码应用规范中的那些坑
  17. numpy 求向量夹角 区间 [-pi, +pi]
  18. LIM参数化景观施工图教程(1)- 工作环境和项目初始设置
  19. Web前端_邮箱的正则表达式
  20. 数据可视化方法:数据图表展示

热门文章

  1. encodingaeskey java,消息体签名与加解密-开发者QA
  2. 一个程序如何连接到外网_如何开发制作小程序?做一个电商带直播小程序
  3. 勾股定理的面积证明法,形象又好记!
  4. 别说了,叫爸爸吧! | 今日最佳
  5. 如何判断程序员是在装逼还是有真本事?
  6. 外国人最常说的100个“中国词”出炉,第一个你绝对想不到…
  7. 优雅的读懂支持向量机 SVM 算法
  8. 区块链浏览器_全球首款区块链浏览器是啥名堂?傲游6首发体验
  9. 华硕路由器 linux上不了网,华硕ASUS路由器连不上网怎么办?
  10. boostrap 鼠标滚轮滑动图片_16种基于Bootstrap的css3图片hover效果