POJ2528(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:
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
思路
线段树的区间覆盖,但是由于给的长度太大而开不了那么大的数组所以需要离散化来压缩空间。假设只有两块区域[1,2]和[1e7-1,1e7]。而中间的区域[2,1e7-1]没有使用这是非常严重的浪费。那么这种情况我们可以让这两块告示变成[1,2] 和 [3,4]所以这里需要引入离散化的概念。这样就可以将区间压缩下来然后用线段树维护即可。
但是做完看别人的题解的时候发现自己的做法还有不对的地方。
如三张海报为:[1,10] ,[1,4] ,[6,10]
离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的[1,4]被染为1;
第二张海报时:墙的[1,2]被染为2,[3,4]仍为1;
第三张海报时:墙的[3,4]被染为3,[1,2]仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显是第一张海报从非离散化的角度去看还是可以看到的,所以正确答案输出为3。这种方法可以理解为离散化过头了。
新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上[1,4],[4,6],[6,10]之间都新增了数)然后在重新排序一次即可。
之后序列就变成:X[1] = 1,X[2] = 2,X[3] = 4,X[4] = 5,X[5] = 6,X[6] = 7 ,X[7] = 10
这样之后,第一次是[1,7]被染成1;第二次[1,3]被染成2;第三次[5,7]被染成3。最终答案就是3如下图所示。
正确的AC姿势如下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson k<<1,l,m
#define rson k<<1|1,m+1,r
const int maxn = 10005;
struct info{
int l,r;
}a[maxn];
int ans;
int hash[maxn<<2];
bool visited[maxn<<2];
int s[maxn<<4];
int color[maxn<<4];
inline void push_down(int k)
{
if(color[k] != -1){
s[k<<1] = s[k<<1|1] = color[k];
color[k<<1] = color[k<<1|1] = color[k];
color[k] = -1;
}
}
inline void update(int k,int l,int r,int ql,int qr,int ans)
{
if(qr < l || r < ql){
return ;
}
if(ql <= l && r <= qr){
color[k] = ans;
s[k] = ans;
return ;
}
push_down(k);
int m = (l + r) >> 1;
update(lson,ql,qr,ans);
update(rson,ql,qr,ans);
}
inline void query(int k,int l,int r)
{
if(l == r){
if(s[k] != -1 && !visited[s[k]]){
ans++;
visited[s[k]] = true;
}
return ;
}
push_down(k);
int m = (l + r) >> 1;
query(lson);
query(rson);
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int cnt = 0;
ans = pre = 0;
memset(hash,0,sizeof(hash));
memset(visited,false,sizeof(visited));
memset(color,-1,sizeof(color));
memset(s,-1,sizeof(s));
for(int i = 0;i < n;i++){
scanf("%d%d",&a[i].l,&a[i].r);
hash[cnt++] = a[i].l;
hash[cnt++] = a[i].r;
}
sort(hash,hash+cnt);
cnt = unique(hash,hash+cnt) - hash;
int num = cnt;
for(int i = 1;i < num;i++){ //加入一些数字防止过度离散化
if(hash[i] - hash[i-1] > 1){
hash[cnt++] = hash[i-1] + 1;
}
}
sort(hash,hash+cnt);
for(int i = 0;i < n;i++){
int l = (int)(lower_bound(hash,hash+cnt,a[i].l) - hash)+1; //线段树从1开始所以要+1
int r = (int)(lower_bound(hash,hash+cnt,a[i].r) - hash)+1;
update(1,1,cnt,l,r,i);
}
query(1,1,cnt);
printf("%d\n",ans);
}
return 0;
}
愿你走出半生,归来仍是少年~
上一篇: hdu3265 Poster(扫描线)