poj 2528 特殊离散化+线段树

来源:互联网 发布:课堂直播软件 编辑:程序博客网 时间:2024/06/08 18:30

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 65420 Accepted: 18911

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


题意:

给出n个横幅,按照顺序依次覆盖,求最后能看到的横幅数量

题解:

特殊离散化:::

如三张海报为: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之间,6,10之间都新增了数的)

X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10

这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3

最终,1~2为2,3为1,4~5为3,于是输出正确结果3。


然后线段树处理一下,最后整个区间查询一下得到结构


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 10010#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1int sum[MAXN<<4],lazy[MAXN<<4];int li[MAXN*2],ri[MAXN*2],x[MAXN<<4];void pushDown(int p){    if(lazy[p])    {        lazy[p<<1]=lazy[p];        lazy[p<<1|1]=lazy[p];        lazy[p]=0;    }}void update(int l,int r,int p,int L,int R,int num){    if(L<=l&&r<=R){        lazy[p]=num;        return;    }    pushDown(p);    int mid=(l+r)>>1;    if(mid>=L)        update(lson,L,R,num);    if(mid<R)        update(rson,L,R,num);}int ans;void query(int l,int r,int p){    if(lazy[p]){        if(sum[lazy[p]]==0){            sum[lazy[p]]=1;            ans++;        }        return ;    }    if(l==r)        return ;    pushDown(p);    int mid=(l+r)>>1;    query(rson);    query(lson);    return ;}int BSearch(int key,int left,int right){    int mid;    while(left<=right)    {        mid=(left+right)>>1;        if(x[mid]==key)            return mid;        else if(key>x[mid])            left=mid+1;        else            right=mid-1;    }    return left;}int main(){    int n,T;    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        int p=1;        memset(sum,0,sizeof(sum));        memset(lazy,0,sizeof(lazy));        scanf("%d",&n);        for(int i=1;i<=n;i++){            scanf("%d%d",&li[i],&ri[i]);            x[p++]=li[i];            x[p++]=ri[i];        }        sort(x+1,x+p);        p=unique(x,x+p)-x;        int m=p;        for(int i=2;i<p;i++){            if(x[i]-x[i-1]>1)                x[m++]=x[i-1]+1;        }        p=m;        sort(x+1,x+p);        int l,r;        for(int i=1;i<=n;i++){            l=BSearch(li[i],1,p-1);            r=BSearch(ri[i],1,p-1);            update(1,p-1,1,l,r,i);        }        ans=0;        query(1,p-1,1);        printf("%d\n",ans);    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 减肥掉头发很厉害怎么办 孕妇吃了金枪鱼罐头怎么办 怀孕吃了烂水果怎么办 快递水果压坏了怎么办 谈对象被骗了钱怎么办 如果遇到半夜坏人敲门怎么办 小孩晚上不进房间睡觉怎么办 房子照不到阳光潮湿怎么办 4个月婴儿不拉屎怎么办 小孩六个月奶不够吃怎么办 到晚上就没奶水怎么办 20多天奶水不够怎么办 四个月奶不够吃怎么办 7个月宝宝不肯吃东西怎么办 6个月宝宝不肯吃东西怎么办 十一个月宝宝不肯吃东西怎么办 九个月宝宝不肯吃辅食怎么办 八个月宝宝不肯吃辅食怎么办 七个月宝宝不肯吃辅食怎么办 7个月宝不吃辅食怎么办 在月子里奶不够怎么办 8月宝宝不吃辅食怎么办 8个月不吃辅食怎么办 宝宝不吃药怎么办一岁 奶水不足宝宝又不吃奶粉怎么办 一岁婴儿突然不吃饭怎么办 一岁宝宝突然不吃饭怎么办 一岁半宝宝突然不爱吃饭怎么办 1岁宝宝突然不爱吃饭怎么办 一岁多宝宝突然不爱吃饭怎么办 一岁半宝宝突然不吃饭怎么办 八个月宝宝不吃奶粉怎么办 小孩铅超标怎么办如何排铅 宝宝突然不吃辅食怎么办 6个月宝宝不喝奶怎么办 宝宝4月不吃母乳怎么办 不吃母乳怎么办 8个月 宝宝不吃奶粉母乳又不够怎么办 宝宝只吃母乳不吃奶粉怎么办 十个月宝宝不爱吃辅食怎么办 奶水不足宝宝不吃奶粉怎么办