poj 2528 Mayor's posters(线段树 二分 大数据离散化 区间更新)

4 篇文章 0 订阅
1 篇文章 0 订阅

原题链接
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个人在按顺序上面贴海报,给出每张海报的起始位置l,和结束位置r,海报会相互覆盖,问最后一共可以看见多少张海报。

思路:如果不是题目给的墙的长度太长的其实是一道最基础的区间更新问题,但我们注意到人的数量最大为10000,所以可以进行离散化。例如:

1-6
1-3
5-6
更新
将案例中的数排序后,可以让一个数组表示它,即id[0]=1,id[1]=3,
id[2]=5,id[3]=6.对原先线段树点1 3 5 6的更新可以变为对其下标表示点的更新。
假如我们要更新1-6,只需找到1的下标0,6的下标3,接着在相应线段树中更新即可。
这里写图片描述

这样可以将线段树的大小控制在n*4以内。

注意:如果按上述所给的Id数组来计算,则发现结果为2,错误,因为4-4这一段其实是有海报的但是id数组中却没有4这个数据,所以在得到id数组的时候应该在两个相邻差大于1的两个值之间插入一个在两值之间的数代表这之间的一段,即id数组应为id0=1 id1=3 id2=4 id3=5 id4=6。

另外提供一组易错数据:
5
1 11
11 11
5 66
7 88
8 110
答案是4

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int inf=100000000;
const int maxn=20005;
int t,n;
int l[maxn*2],r[maxn*2];
int x1[maxn*2],x2[maxn*2];
int vis[maxn*2];
int id;
int ans;
struct node{
    int l,r,v;
}s[maxn*2*4];
void init()
{
    ans=0;
    memset(vis,0,sizeof(vis));
    sort(x1,x1+n*2);
        x2[0]=x1[0];
        id=1;
        for(int i=1;i<n*2;i++)
        {
            if(x1[i]==x1[i-1])
                continue;
            else if(x1[i]-x1[i-1]>1)
            {
                x2[id++]=x1[i-1]+1;
                x2[id++]=x1[i];
            }
            else if(x1[i]-x1[i-1]==1)
            {
                x2[id++]=x1[i];
            }
        }
}
void build(int i,int l,int r)
{
    s[i].l=l;
    s[i].r=r;
    int mid=(l+r)/2;
    if(l==r)
    {
        s[i].v=0;
        return;
    }
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    s[i].v=0;
}
int binarysearch(int v,int l,int r)
{
    int mid=(l+r)/2;
    if(x2[mid]==v)
    {
        return mid;
    }
    else if(l==r)
    {
        return -1;
    }
    else if(x2[mid]>v)
    {
        binarysearch(v,l,mid);
    }
    else if(x2[mid]<v)
    {
        binarysearch(v,mid+1,r);
    }
}
void update(int i,int l,int r,int v)
{
    int mid=(s[i].l+s[i].r)/2;
    if(s[i].l>=l&&s[i].r<=r)
    {
        s[i].v=v;
        return;
    }
    if(s[i].v)
    {
        s[i*2].v=s[i].v;
        s[i*2+1].v=s[i].v;
        s[i].v=0;
    }
    /*if(mid>=l)
        update(i*2,l,r,v);
    else if(mid<r)
        update(i*2+1,l,r,v);*/
    if(r<=mid)
    {
        update(2*i,l,r,v);
    }
    else if(l>mid)
    {
        update(2*i+1,l,r,v);
    }
    else
    {
        update(2*i,l,mid,v);
        update(2*i+1,mid+1,r,v);
    }

}
void get(int i)
{
    if(s[i].l==s[i].r)
        {
            if(vis[s[i].v]==0&&s[i].v)
            {
                ans++;
                vis[s[i].v]=1;
            }
            return;
        }
        if(s[i].v)
        {
            s[i*2].v=s[i*2+1].v=s[i].v;
            s[i].v=0;
        }
    get(i*2);
    get(i*2+1);
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
           scanf("%d%d",&l[i],&r[i]);
           x1[i*2]=l[i];
           x1[i*2+1]=r[i];
        }
        init();
        /*for(int i=0;i<id;i++)
            cout<<x2[i]<<" ";
        cout<<endl;*/
        build(1,0,id-1);
        //cout<<"yes"<<endl;
        for(int i=0;i<n;i++)
        {
            int l1=binarysearch(l[i],0,id-1);//cout<<l1;
            int r1=binarysearch(r[i],0,id-1);//cout<<" "<<r1<<endl;
            update(1,l1,r1,i+1);
        }
        //cout<<"yes"<<endl;
        //for(int i=0;i<100;i++)
            //cout<<s[i].v<<endl;
        get(1);

        cout<<ans<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值