Mayor's posters POJ

来源:互联网 发布:ubuntu 查看显卡驱动 编辑:程序博客网 时间:2024/05/18 02:08
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 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
151 42 68 103 47 10
Sample Output

4


这道题也是纠结了很久了。这道题需要离散化,如果我们开一4*n的数组肯定会mle的。

如何离散化:每一幅画,都会占用一定的长度反应在数组里面。如果长度很长,那么会造成数组里面有

大量没有用过的点,所以我们要做的就是避免这一部分浪费现象。

让我们来考虑这样一问题,这幅画的长度重要还是与别的画的重叠关系重要?

答案显而易见,长度也是用来表达重叠关系的一种方式,如果有了重叠关系我们何必要用长度?

所以我们就来压缩这个长度。

具体做法就是吧所有的点都存在一个数组中,升序排序,并且去掉重复点,从小到大标号为1--------n,

离散化结束。

然后就是一涂色的问题了,假设我们有x条线段,我们就对这x条线段对应的点涂色,最后看一下有多少不同颜色的点就知道有多少是可以看到的了。这里要用一下延迟更新。

只要遍历线段树,一直到叶节点,判断一下叶节点的颜色有几种就可以AC了。

注意:不要用STL,STL确实可以很快速的实现我们的要求,但是直接超时。然而非STL仅仅跑了79ms。

POJ对于STL的限制比较严格。

下面AC代码

#include<cstdio>#include<map>#include<set>#include<string.h>#include<algorithm>using namespace std;const int M=2e4+10;const int N=1e4+10;int tree[4*M],tempx[N],tempy[N],temp[M];int dio[10000010];bool lazy[4*M],vis[M];int sum=0;void update(int now,int l,int r,int al,int ar,int val){    if(al>r||ar<l)    {        return ;    }    if(al<=l&&ar>=r)    {        lazy[now]=1;        tree[now]=val;        return ;    }    if(lazy[now])    {        lazy[now]=0;        lazy[now*2+1]=lazy[now*2+2]=1;        tree[now*2+1]=tree[now*2+2]=tree[now];    }    int mid=(l+r)/2;    update(now*2+1,l,mid,al,ar,val);    update(now*2+2,mid+1,r,al,ar,val);}void query(int now,int l,int r){    if(l==r)    {       // printf("%d %d\n",l,tree[now]);        if(!vis[tree[now]])        {            vis[tree[now]]=1;            sum++;        }        return ;    }    if(lazy[now])    {        lazy[now]=0;        lazy[now*2+1]=lazy[now*2+2]=1;        tree[now*2+1]=tree[now*2+2]=tree[now];    }    int mid=(r+l)/2;    query(now*2+1,l,mid);    query(now*2+2,mid+1,r);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        sum=0;        int n;        memset(lazy,0,sizeof(lazy));        memset(vis,0,sizeof(vis));        scanf("%d",&n);        int c=0;        for(int i=0; i<n; i++)        {            int a,b;            scanf("%d%d",&a,&b);            tempx[i]=a,tempy[i]=b;            temp[c++]=a,temp[c++]=b;        }        int k=1;        sort(temp,temp+c);        dio[temp[0]]=0;        for(int i=1;i<c;i++)        {            if(temp[i]!=temp[i-1])            {                dio[temp[i]]=k++;            }        }        for(int i=0; i<n; i++)        {            int a=dio[tempx[i]],b=dio[tempy[i]];           // printf("%d %d %d\n",a,b,i+1);            update(0,0,k-1,a,b,i+1);        }        query(0,0,k-1);        printf("%d\n",sum);    }}


原创粉丝点击