D - Mayor's posters

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.
输入:
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.
在这里插入图片描述
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10

Sample Output
4
题意:就是很明显的区间覆盖,但是要注意我们用线段树去表示区间[1,4],我们用点去直接存1,2,3,4这是表示四个距离单位,但是区间之间的间隔只有三个单位,所以我们要对此进行一些操作,在左区间边界加一或右区间边界减一,再去存起来,这样就是存三个点。
但是我们要注意如果我们直接对题目给出的区间进行操作的话区间就会爆掉,因为输入数据的大小是10000000,所以我们要对其进行映射。
就比如有 1、100、1000、10000、100000
我们对其映射后他们就变成了 1、2 、 3 、 4 、 5
这样在对他们进行线段树维护,区间大小就会缩小,因为输入数据是10000,因为一次输入两个数,所以maxn(maxn是我的代码中取得一个标准)取20000

对其进行映射后,对于两个区间[a,b],[c,d]只要|a-b|大于一就在他们中间多加一个数,这样就可以弥补线段树用点存区间的缺陷

对于我们这一道题,就是把区间修改的所有点存在一个数组里面,然后对这些点排序(从小到大),再去重,之后再判断两点之间的距离是否大于一,大于的话就在他们中间加一个点(这个点大于左小与右),之后把区间更新的每个点,先求出来他在这个数组的对应点的坐标,再对其进行线段树操作
上代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=20005;
int sum[(maxn*2)<<2],v[maxn*2],li1[maxn],li2[maxn],val[maxn*2],w[maxn*2],summ;
void pushdown(int rt)
{
    if(sum[rt]!=-1)
    {
        sum[rt<<1]=sum[rt<<1|1]=sum[rt];
        sum[rt]=-1;
    }
}
int searchs(int l,int r,int x)
{
    int m=0;
    while(l<=r)
    {
        m=(l+r)>>1;
        if(val[m]>x)
        {
            r=m-1;
        }
        else if(val[m]<x)
        {
            l=m+1;
        }
        else break;
    }
    return m;
}
void update(int L,int R,int C,int l,int r,int rt)
{
    if(l>=L && R>=r)
    {
        sum[rt]=C;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(m>=L) update(L,R,C,l,m,rt<<1);
    if(R>m) update(L,R,C,m+1,r,rt<<1|1);
}
void query(int L,int R,int l,int r,int rt)
{
    if(sum[rt]!=-1)
    {
        if(v[sum[rt]]==0)
            ++summ,v[sum[rt]]=1;//printf("%d**\n",sum[rt]);
        return;
    }
    //if(l==1 && r==1) printf("%d***\n",sum[rt]);
    if(l==r) return;
    int m=(l+r)>>1;
    if(m>=L) query(L,R,l,m,rt<<1);
    if(R>m) query(L,R,m+1,r,rt<<1|1);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        summ=0;
        memset(v,0,sizeof(v));
        memset(sum,-1,sizeof(sum));
        scanf("%d",&n);
        int cnt=0;
        for(int i=1;i<=n;++i)
            scanf("%d%d",&li1[i],&li2[i]),w[++cnt]=li1[i],w[++cnt]=li2[i];
        sort(w+1,w+1+cnt);
        //printf("%d ",w[ans]);
        int m=cnt;
        for(int i=2;i<=m;++i)
        {
            if(w[i]-w[i-1]>1)
            {
                w[++cnt]=w[i-1]++;
            }
        }
        sort(w+1,w+cnt+1);
        int ans=1;
        val[ans]=w[ans];
        //printf("%d ",w[ans]);
        for(int i=2;i<=cnt;++i)
        {
            if(w[i]!=w[i-1])
            {
                //printf("%d ",w[i]);
                val[++ans]=w[i];
            }
        }
        //printf("\n");
        for(int i=1;i<=n;++i)
        {
            int x=searchs(1,ans,li1[i]);
            int y=searchs(1,ans,li2[i]);
            //printf("%d %d\n",x,y);
            update(x,y,i,1,ans,1);
        }
//        for(int i=1;i<=ans;++i)
//            printf("%d ",sum[i]);
//        printf("\n");
        query(1,ans,1,ans,1);
        printf("%d\n",summ);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值