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

本文介绍了一种通过线段树解决选举海报放置问题的方法。在一条长度为10000000的墙上,候选人可以放置宽度各异的海报,且允许重叠。任务是找出在所有海报放置完毕后,有多少张海报是可见的。文章详细解释了如何通过数据离散化处理和线段树来高效解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

Source

Alberta Collegiate Programming Contest 2003.10.18


题意:

给每个区间贴纸,后面贴的会覆盖前面贴的,问最后能看到的纸有几张;
思路:
一道线段树的查询与更新的题,由于每个数据组的l,r都是在10000000以内,不加处理用线段树肯定会超时;
我们在线段树之前先将数据离散化处理;
对于每个测试组给定的数据,最多为2*n个数,我们可以先将这些数用结构体记录数和区间的位置,然后从小到大排序,我们可以得到每个数在所有数中对应的第几大,并用二维map数组记录;这样就将数据离散化处理了;
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100005;
int map[2*N][2],vis[2*N],sum;
struct no{
    int point,num;
}a[4*N];
struct node{
    int l,r,w;
}str[3*N];
bool cmp(no x,no y)
{
    return x.point<y.point;
}
void build(int l,int r,int n)
{
    str[n].l=l;
    str[n].r=r;
    str[n].w=0;
    if(l==r)
        return;
    int temp=(l+r)/2;
    build(l,temp,2*n);
    build(temp+1,r,2*n+1);
}
void inser(int l,int r,int n,int w)
{
    if(str[n].l==l&&str[n].r==r)
    {
        str[n].w=w;
        return;
    }
    if(str[n].w)  //延迟标记
    {
        str[2*n].w=str[2*n+1].w=str[n].w;
        str[n].w=0;
    }
    int temp=(str[n].l+str[n].r)/2;
    if(r<=temp)
        inser(l,r,2*n,w);
    else if(l>temp)
        inser(l,r,2*n+1,w);
    else
    {
        inser(l,temp,2*n,w);
        inser(temp+1,r,2*n+1,w);
    }
}
void query(int n)
{
    if(str[n].w)
    {
        if(!vis[str[n].w])  //如果此颜色被计数了,就不要再重复了
        {
            sum++;
            vis[str[n].w]=1;
        }
        return;
    }
    query(2*n);
    query(2*n+1);
    return;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&map[i][0],&map[i][1]);
            a[2*i].point=map[i][0];    //a记录每个数和它对应的区间位置,左区间记为负;
            a[2*i+1].point=map[i][1];
            a[2*i].num=-(i+1);
            a[2*i+1].num=i+1;
        }
        sort(a,a+2*n,cmp);
        int temp=a[0].point;
        int cnt=1;
        for(int i=0;i<2*n;i++)
        {
            if(a[i].point!=temp)  //去重
            {
                temp=a[i].point;
                cnt++;
            }
            if(a[i].num<0)   //map记录每个数在数据中的大小
                map[-a[i].num-1][0]=cnt;
            else
                map[a[i].num-1][1]=cnt;
        }
        build(1,cnt,1);
        for(int i=0;i<n;i++)
        {
            inser(map[i][0],map[i][1],1,i+1);
        }
        memset(vis,0,sizeof(vis));
        sum=0;
        query(1);
        printf("%d\n",sum);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值