POJ 2558 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

题目大意

有n个等高的海报,依次往墙上贴,问最后贴完能看到多少张不同的海报。

解题思路

转化为覆盖染色问题,每张海报用不同的颜色值代表,然后对其对应的区间进行更新(线段树区间修改加懒惰标记)。不过在维护线段树时开数组大致需要1e7<<4,明显不可行,但是最多只有10000个区间,也就是最多有20000个点,所以可以先借助离散化将区间进行处理,离散化也就是说把无用的点舍弃,将大的区间映射为更小的区间,比如(1000,2000),(1500,3000)这样的两个区间,我们可以直接映射为(1,3)和(2,4)两个区间。
但是在离散化的过程中会存在一个问题,举两个例子:<1>(1,10),(1,4),(5,10);<2>(1,10),(1,4),(6,10),对于例子1,线段1被接下来的两个线段完全覆盖,最后只剩下两种颜色,但是例子2中的线段1并没有被完全覆盖,但是如果直接映射最后的结果都是(1,4),(1,2),(3,4),这样的结果明显对于例子2是不正确的。所以我们在离散化的过程中需要一点小技巧,就是如果两个相邻点大于1时,就在中间再插入一个点,比如我们在例子2中点4和点6之间加入一个点5,这样5位置的颜色就不会被覆盖掉了。其实在操作的过程中1和4,6和10之间也会分别加入一个点。

代码实现

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 10007
int color[maxn<<4],s[maxn],e[maxn],A[maxn<<3];
bool vis[maxn];
int ans;
void PushDown(int rt)
{
    color[rt<<1]=color[rt<<1|1]=color[rt];
    color[rt]=-1;
}
void Update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        color[rt]=c;
        return ;
    }
    if(color[rt]!=-1) PushDown(rt);
    int m=(l+r)/2;
    if(m>=L) Update(L,R,c,l,m,rt<<1);
    if(m<R) Update(L,R,c,m+1,r,rt<<1|1);
}

int binary_Search(int l,int r,int t)
{
    while(l<=r)
    {
        int m=(l+r)/2;
        if(A[m]==t) return m;
        if(A[m]>t)
            r=m-1;
        else
            l=m+1;
    }
    return -1;
}
void Query(int l,int r,int rt)
{
    if(color[rt]!=-1)     //此处有海报
    {
        if(!vis[color[rt]])   //该颜色还未出现过
        {
            ans++;
            vis[color[rt]]=1;
        }
        return;
    }
    if(l==r) return ;
    int m=(l+r)/2;
    Query(l,m,rt<<1);
    Query(m+1,r,rt<<1|1);
}
int main()
{
    int n,T;
    scanf("%d",&T);
    while(T--)
    {
        memset(color,-1,sizeof(color));
        memset(vis,0,sizeof(vis));
        int t=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&s[i],&e[i]);
            A[++t]=s[i];
            A[++t]=e[i];
        }
        sort(A+1,A+t+1);
        int tt=1;
        for(int i=2;i<=t;i++)
            if(A[i]!=A[i-1])
                A[++tt]=A[i];
        for(int i=t;i>1;i--)
            if(A[i]-A[i-1]>1)
                A[++tt]=A[i]-1;
        sort(A+1,A+tt+1);
        for(int i=1;i<=n;i++)
        {
            int l=binary_Search(1,tt,s[i]);
            int r=binary_Search(1,tt,e[i]);
            Update(l,r,i,1,tt,1);
        }
        ans=0;
        Query(1,tt,1);
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值