Mayor's posters
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:
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 l
i 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 <= l
i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l
i, l
i+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 写在前面: 这道题不是原创 是 某种情况上来说的翻译!!!!
这道题 其实不是我独立做出来的 然后这道题是我看的线段树模板上讲的一个东西 因为是第一次接触离散化 还是有点方 感觉这个东西很难 学会之后才发现这个东西其实很简单的 离散化这种东西 题目意思: 这道题是很经典的区间染色问题 就是给你一些区间 然后给这些区间贴海报 问你最后能看到多少海报
你可以理解为每次都给这个区间染一种不同的颜色 没错 我的意思是每次染色都是染了不同的颜色 虽然颜色没有这么多种但是海报有啊 所以这点是需要注意一下的 每次染色都是染了不同的颜色 有了这点的基础上我们 再来看这道题 数据范围非常大 如果直接用线段树 超时 超内存 所以我们需要一种叫做离散化的操作 首先别被这个名字吓到 ~~ 离散化: 怎么理解离散化呢 就是给你打个比方 如果你有三个区间 分别是 【1000-2000】,【3000-5000】,【100000-20000000】 那么我们是不是真的需要开一个20000000这么大的数组然后一个区域一个区域的来给这些部分染色呢 你也发现了对吧 没有这种必要所以我们把1000对应为1 2000对应为2 就是把这些不同的n个数字对应为1-n这些数字就好 这时候有一个 小细节需要注意 就是如果你只用了非常裸的离散化 这样还不对 因为有些数据过不了 模板上给的例子就很好 1-10 1-4 5-10 你们可以自己对应这些数字来写写看 就会发现弊端在哪了 需要注意的是 这些编号不是节点号 而是 线段的号吗 别弄混了~~
再次声明 本文是为了让更多人更容易地读懂这篇代码 不是我写的 如果有人想要这份pdf可以留言
|
poj 2528(线段树 + 离散化)
最新推荐文章于 2018-08-03 16:26:00 发布