POJ2528 Mayor's posters

本文探讨了一个关于在特定规则下放置选举海报的问题,包括如何计算最终可见的海报数量。通过构建特殊的墙结构来放置不同宽度的海报,并允许新海报覆盖旧海报的部分或全部区域。

摘要生成于 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 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

 

这个题目有问题,边界问题没有好的处理

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define M 100005

using namespace std;

struct node
{
    int l;//区间的左界限
    int r;//区间的又界限
    int rt;//下标
    int val;//这个区间是否是纯色,是则是0,否则为1
    int col;//这个区间的色彩
};

int n;
int sign;
int mark;//这个地方是之后的元素数量,去重之后的元素个数
int sum;
int x[M],y[M];
vector<int> p;
vector<int> ps;
vector<int>::iterator it;
set<int> v;
set<int>::iterator iset;
node map[M*4];

void out()
{
    for(it=p.begin(); it!=p.end(); it++)
        printf("%d ",*it);
    printf("\n");
}


void bulid(int l,int r,int rt)
{
    if(l==r)
    {
        map[rt].l=p[sign];
        map[rt].r=p[sign++];
        map[rt].rt=rt;
        map[rt].val=0;
        map[rt].col=-1;
//       printf("%d ",map[rt].r);
        return;
    }
    int m=(l+r)>>1;
    bulid(lson);
    bulid(rson);
    map[rt].l=map[rt<<1].l;
    map[rt].r=map[rt<<1|1].r;
    map[rt].rt=rt;
    map[rt].val=0;
    map[rt].col=-1;
}
void init()//初始化
{
    p.clear();
    int m=0;
    int k;
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&x[i],&y[i]);
        p.push_back(x[i]);
        p.push_back(y[i]);
    }
    sort(p.begin(),p.end());
//   out();
    it=unique(p.begin(),p.end());
    p.resize(it-p.begin());
//   out();
    int s=p.size();
    for(int i=1; i<s; i++)
    {
        if(p[i]!=p[i-1]+1)
            p.push_back(p[i-1]+1);
    }
    sort(p.begin(),p.end());
//   out();
    mark=p.size();
    sign=0;
    bulid(1,mark,1);
//   printf("\n");
}

void pushdown(int l,int r,int col,int rt)//如果这个区间不是纯色的,我们要进行更新,这个地方我们只向下更新一层
{
    if(l==r)
        return;
    map[rt<<1].col=map[rt].col;
    map[rt<<1].val=1;
    map[rt<<1|1].col=map[rt].col;
    map[rt<<1|1].val=1;
    return;
}
void update(int xl,int yr,int col,int l,int r,int rt)//这个地方,我们要注意,更新的时候我们更新的是一边,最后一位是不需要更新的
{
    if(l==r)
    {
        map[rt].col=col;
        map[rt].val=1;
        return ;
    }
    if(xl<=map[rt].l&&map[rt].r<=yr)//如果在区间内(正确的是右边是不需要更新的,所以这个地方的正确代码应该是:if(xl<=map[rt].l&&map[rt].r<yr))
    {
        map[rt].col=col;
        map[rt].val=1;//表示这个区间是纯色的,并且色彩是col,这个地方表示纯色
        /*if(map[rt].val)//如果这个区间不是纯色的,
            pushdown(l,r,col,rt);
        map[rt].col=col;//这个区间的颜色
        map[rt].val=0;//更新之后的区间是纯色的
        return;*/
        return;
    }
    //当这个区间原来应该是纯色的时候
    if(map[rt].val)
    {
        pushdown(l,r,col,rt);
    }
    int m=(l+r)>>1;
    if(xl<=map[rt<<1].r)//如果在左边
        update(xl,yr,col,lson);
    if(yr>=map[rt<<1|1].l)//如果在右边,原来正确的代码应该是if(yr>map[rt<<1|1].l),这个地方体现边界的问题
        update(xl,yr,col,rson);
    map[rt].val=0;//这个区间不是纯色的
}

void query()
{
    for(int i=0; i<n; i++)
    {
        update(x[i],y[i],i,1,mark,1);//这个地方的n要改变
    }
}

void get(int l,int r,int rt)
{
    if(l==r)
    {
        v.insert(map[rt].col);
        return;
    }
    if(map[rt].val)
    {
        v.insert(map[rt].col);
        return;
    }
    int m=(l+r)>>1;
    get(lson);
    get(rson);
}
void ask()//这个地方是用来询问不同的颜色
{
    sum=0;
    v.clear();
    get(1,mark,1);
    iset=v.find(-1);
    if(iset==v.end())
        printf("%d\n",v.size());
    else
        printf("%d\n",v.size()-1);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        query();
        ask();
    }
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值