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

来源:互联网 发布:山东大学网络教育期末考试 编辑:程序博客网 时间:2024/06/07 07:46

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

题解:

这题做了我一个上午,不过是训练离散化的好题,就是在一个区间上贴海报,问最后能看到几张(后来的会覆盖掉前面的),这题由于数据范围太大,所以要离散化。。之前做过扫描线的离散化,运用上去就好了,思路是从后往前贴海报,如果区间被覆盖了就不计数

代码:

#include<iostream>#include<stdio.h>#include<map>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include<string>#include<cstring>using namespace std;int x[20005];//存端点信息用于离散化struct poster{    int l,r;}a[10005];//存海报信息struct node{    int l,r;    int s;//判断是否被完全覆盖}t[20005*4];void Build(int l,int r,int k){    t[k].s=0;    t[k].l=l;    t[k].r=r;    if(l==r)        return;    int mid=(l+r)/2;    Build(l,mid,k*2);    Build(mid+1,r,k*2+1);}int update(int l,int r,int k)//查询的同时更新,返回0则没被覆盖,1就是被覆盖了{    if(t[k].s)//如果完全被覆盖        return 0;    if(t[k].l==l&&t[k].r==r)    {        t[k].s=1;        return 1;    }    int mid=(t[k].l+t[k].r)/2;    int p;    if(r<=mid)        p=update(l,r,k*2);    else if(l>mid)        p=update(l,r,k*2+1);    else    {        int e=update(l,mid,k*2);        int q=update(mid+1,r,k*2+1);//用或的原因是只要不被完全覆盖就行了        p=e||q;//这里神坑,如果写成p=update(l,mid,k*2)||update(mid+1,r,k*2+1),最后返回p,莫名其妙不会执行后面那个,所以要写成这样    }    if(t[k*2].s&&t[k*2+1].s)//如果左右子树都被覆盖了        t[k].s=1;    return p;}int main(){    int test,i,j,k,n,tot,ans;    scanf("%d",&test);    while(test--)    {        scanf("%d",&n);        tot=0;        for(i=n-1;i>=0;i--)        {            scanf("%d%d",&x[tot],&x[tot+1]);            a[i].l=x[tot],a[i].r=x[tot+1];            tot+=2;        }        sort(x,x+tot);        k=unique(x,x+tot)-x;//排序后去重用于离散化        ans=0;        Build(0,k-1,1);        for(i=0;i<n;i++)        {            int l=lower_bound(x,x+k,a[i].l)-x;//找到海报端点对应的离散后的下标            int r=lower_bound(x,x+k,a[i].r)-x;            if(update(l,r,1))            {                ans++;            }        }        printf("%d\n",ans);    }    return 0;}