POJ2528 Mayor's posters(线段树+离散化+染色)

来源:互联网 发布:路由器端口转发安全 编辑:程序博客网 时间:2024/05/16 15:37

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 68924 Accepted: 19861

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

151 42 68 103 47 10

Sample Output

4

题目大意:贴海报,每个海报能覆盖前一个海报,问最后能看见几个海报。

很容易看出来是一个染色问题,利用了线段树的区间更新,二分查找离散化,hash判断颜色。

代码如下:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define N 1000005int tree[N*4];int ha[N*4],num[N*4];int l[N],r[N];int ans;void pushdown(int i){    if(tree[i]!=0)    {        tree[i<<1]=tree[i<<1|1]=tree[i];        tree[i]=0;    }}void updata(int l,int r,int L,int R,int i,int cor)//线段树更新{    if(l>=L&&r<=R)    {        tree[i]=cor;        return ;    }    pushdown(i);    int mid=(l+r)/2;    if(L<=mid)        updata(l,mid,L,R,i<<1,cor);    if(R>mid)        updata(mid+1,r,L,R,i<<1|1,cor);}void query(int l,int r,int i){    if(l==r)    {        if(ha[tree[i]]==0&&tree[i]!=0)//查询利用hash判断颜色是否出现过        {            ans++;            ha[tree[i]]=1;        }        return ;    }    pushdown(i);    int mid=(l+r)/2;    query(l,mid,i*2);    query(mid+1,r,i*2+1);}int sear(int x,int y,int cnt)//利用二分查找到所对应数字的下标即为离散化后的结果{    int mid;    while(x<=y)    {        mid=(x+y)/2;        if(num[mid]==cnt)            return mid;        else if(num[mid]>cnt)            y=mid-1;        else            x=mid+1;    }    return -1;}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        memset(ha,0,sizeof(ha));        memset(tree,0,sizeof(tree));        scanf("%d",&n);        int j=0;        for(int i=1;i<=n;i++)        {            scanf("%d %d",&l[i],&r[i]);            num[++j]=l[i];            num[++j]=r[i];        }        sort(num+1,num+1+j);        int tmp=1;        for(int i=2;i<=j;i++)        {            if(num[i]!=num[i-1])                num[++tmp]=num[i];//去除重复元素        }        for(int i=tmp;i>1;i--)        {            if(num[i]-num[i-1]>1)                num[++tmp]=num[i]-1;//如果相邻的元素相差的超过1,离散化后可能会将1省去,故插入一个新的元素        }        sort(num+1,num+tmp+1);        for(int i=1;i<=n;i++)        {            int le=sear(1,tmp,l[i]);            int ri=sear(1,tmp,r[i]);            //printf("le=%d ri=%d\n",le,ri);            updata(1,tmp,le,ri,1,i);        }        ans=0;        query(1,tmp,1);        printf("%d\n",ans);    }    return 0;}


阅读全文
0 0
原创粉丝点击