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

来源:互联网 发布:linux打包压缩命令zip 编辑:程序博客网 时间:2024/05/19 07:10

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-10 1-4 5-10
1-10 1-4 6-10
如果是普通的离散化,对于1,4,5,10,它们将会分别映射到1,2,3,4
然而对于1,4,6,10,他们也是分别映射到1,2,3,4
那么他们离散后就变成了相等的了,可实际上他们是不同的

为了解决这种缺陷呢,我们可以在排序后的数组上加上一些处理,比如说
有这样一组样例[1,2,6,10]

我们离散的时候,如果相邻数字间距大于1的话,就在其中加上任意一个数字,比如加成[1,2,3,6,7,10],这样的话离散就成功了

接下来,线段树写起就ok了(具体详见代码)

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 20010struct node{    int le,ri;} q[maxn];int record[maxn<<3],color[maxn<<3],hash[maxn<<1];int cnt;void Pushdown(int rt){    if(color[rt]!=-1)//如果有颜色    {        color[rt<<1]=color[rt<<1|1]=color[rt];        color[rt]=-1;    }}void Updata(int rt,int le,int ri,int co,int Left,int Right){    if(Left<=le&&ri<=Right)    {        color[rt]=co;        return ;    }    Pushdown(rt);    int mid=(le+ri)>>1;    if(Left<=mid)        Updata(rt<<1,le,mid,co,Left,Right);    if(Right>mid)        Updata(rt<<1|1,mid+1,ri,co,Left,Right);}void Query(int rt,int le,int ri){    if(color[rt]!=-1)    {        if(!record[color[rt]])        {            ++cnt;            record[color[rt]]=1;        }        return ;    }    if(le==ri)        return ;    int mid=(le+ri)>>1;    Query(rt<<1,le,mid);    Query(rt<<1|1,mid+1,ri);}int main(){    int t,x,y,n;    scanf("%d",&t);    while(t--)    {        memset(color,-1,sizeof(color));        memset(record,0,sizeof(record));        scanf("%d",&n);        cnt=1;        for(int i=0; i<n; ++i)//离散化        {            scanf("%d%d",&q[i].le,&q[i].ri);            hash[cnt++]=q[i].le,hash[cnt++]=q[i].ri;        }        sort(hash+1,hash+cnt+1);//排序        int len=unique(hash+1,hash+cnt+1)-hash;//去重        cnt=len;        for(int i=2; i<=len; ++i)//离散后再处理        {            if(hash[i]-hash[i-1]>1)                hash[cnt++]=hash[i-1]+1;        }        sort(hash+1,hash+cnt+1);//排序        len=cnt,cnt=0;        int left,right;        for(int i=0; i<n; ++i)        {            left=lower_bound(hash,hash+len,q[i].le)-hash;//二分查找对应的区间            right=lower_bound(hash,hash+len,q[i].ri)-hash;            Updata(1,1,len,i,left,right);        }        Query(1,1,len);        printf("%d\n",cnt);    }    return 0;}



总结:开始也想到了离散化,可是还是脑残地想写一发试一试,结果果断。。。
这是学到的第二种线段树的写法了,觉得这种方法好像要更快一些

1 0