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

119 篇文章 1 订阅
113 篇文章 1 订阅
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

离散化:第一次遇到离散化的题,不过这也是最简单的离散化的题;

举个例子:

     3       离散化后   3
   1  10000          1    4
 200  50000          2    5
2000  100000         3    6

是不是有些理解了? 其实本题的离散化就是为了节省空间,避免不必要的浪费,很多区间内是不进行任何操作的,所以可有可无。

思路:按照上面的方法离散化后,再用线段树就行了。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 10009

bool tree[N*8];//判断这个区间有没有被完全覆盖;
struct node
{
    int s,e;//记录每个海报的起始位置;
} dis[N];
int a[N*2];
int b[10000005];

void build(int num,int l,int r)//建树
{
    tree[num]=false;//没有全部被覆盖,就是false
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
}
bool pan(int num,int l,int r,int x,int y)
{
    if(tree[num]) return false;//如果[l,r],区域被覆盖了,说明已经被记录过了。
    if(x<=l&&r<=y)
    {
        tree[num]=true;
        return true;
    }
    int mid=(l+r)>>1;
     bool b1=0,b2=0;
    if(x<=mid) b1=pan(num<<1,l,mid,x,y);
    if(y>mid) b2=pan(num<<1|1,mid+1,r,x,y);
    bool b3=b1||b2;
    if(tree[num<<1]&&tree[num<<1|1])//重要的一步,如果左右子树被覆盖了,那么“父亲”区间也别覆盖了。
        tree[num]=true;
    return b3;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        int n_n=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&dis[i].s,&dis[i].e);
            a[n_n++]=dis[i].s;//离散化,记录每一个起始位置
            a[n_n++]=dis[i].e;
        }
        sort(a,a+n_n);//从小到大排
        n_n=unique(a,a+n_n)-a;//删除其中重复的点
        for(int i=0; i<n_n; i++)
            b[a[i]]=i;//每一个起始位置,对应离散后的起始位置,如上面举例的一样;
        build(1,0,n_n-1);//建树
        int sum=0;
        for(int i=n-1; i>=0; i--)
            if(pan(1,0,n_n-1,b[dis[i].s],b[dis[i].e]))//判断是否有露出的部分
                sum++;
        printf("%d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值