poj 2528 Mayor's posters

原创 2016年05月31日 01:07:01

题目链接:poj2528

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 56457   Accepted: 16343

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

题意:有许多海报高度相同,但长度和位置不同,现给出这些海报的两边的位置和新旧次序,问最后有多少海报是露在外面的。

题目分析:处理这道题可以用线段树的成段更新,大体思想是按次序更新线段树,这样如果有重叠部分也会被覆盖。之后再对整个线段树遍历一遍,找出有多少不同的海报露在外面。

这里处理数据需要离散化,只储存边界的位置后进行排序,再根据排序的位置映射到从0开始的连续数组中。这题比较坑的一点是它的1234指的是一个个的线段而不是端点。这时使用普通的离散化会出现问题,比如这组数据:

1

3

5 6

4 5

6 8

按端点做答案是3,实际上答案为2。

还有

2

3

1 6

1 3

4 6

3

1 6

1 3

5 6

答案应为

2

3

为了避免冲突,需要在得到离散化数组后遍历一遍,遇到非连号的数在中间插入一个数,以此来区分大小线段的长度。

题目中的数据范围有问题,不过开到10w后就没有问题了。

//
//  main.cpp
//  POJ2528
//
//  Created by teddywang on 16/5/30.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 100010
int c,n;
int lazy[N<<2],num[N<<2],has[N<<2];
int li[N],ri[N];
int ans;

void pushdown(int rt)
{
    if(lazy[rt]!=-1)
    {
        lazy[rt<<1]=lazy[rt];
        lazy[rt<<1|1]=lazy[rt];
        lazy[rt]=-1;
    }
}

void query(int l,int r,int rt)
{
    if(lazy[rt]!=-1)
    {
        if(has[ lazy[rt] ]==0) {
           // cout<<lazy[rt]<<endl;
            ans++;
            has[ lazy[rt] ]=1;
        }
        return ;
    }
    if(l==r) return ;
    int m=(l+r)>>1;
    query(lson);
    query(rson);
}

void update(int p,int l1,int r1,int l,int r,int rt)
{
    if(l1<=l&&r<=r1)
    {
        lazy[rt]=p;
        //pushdown(rt);
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(m>=l1) update(p,l1,r1,lson);
    if(m<r1) update(p,l1,r1,rson);
}

int searchs(int p,int l,int r,int *x)
{
    
    while(l<=r)
    {
        int m=(l+r)>>1;
        if(x[m]==p) return m;
        else if(x[m]>p)
            r=m-1;
        else l=m+1;
    }
    return -1;
}

int main()
{
    cin>>c;
    while(c--)
    {
        cin>>n;
        int len=0,m=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&li[i],&ri[i]);
            num[len++]=li[i];
            num[len++]=ri[i];
        }
        sort(num,num+len);
        for(int i=1;i<len;i++)
        {
            if(num[i]!=num[i-1]) num[m++]=num[i];
        }
        for(int i=m-1;i>0;i--)
        {
            if(num[i]-num[i-1]>1) num[m++]=num[i-1]+1;
        }
        sort(num,num+m);
        memset(lazy,-1,sizeof(lazy));
        for(int i=0;i<n;i++)
        {
            int l=searchs(li[i],0,m-1,num);
            int r=searchs(ri[i],0,m-1,num);
            update(i,l,r,0,m-1,1);
        }
        ans=0;
        memset(has,0,sizeof(has));
        query(0,m-1,1);
        cout<<ans<<endl;
    }
}


【poj 2528】Mayor's posters 题意&题解&代码(C++)

poj 线段树
  • DERITt
  • DERITt
  • 2016年04月01日 15:42
  • 231

poj2528 Mayor's posters 线段树+离散化(经典)

题意: 市长竞选,每个市长都往墙上贴海报,海报之间彼此可以覆盖,给出粘贴顺序和每个海报的起点和长度,问最后有多少海报是可见的。...

poj2528Mayor's posters 离散化+线段树区间更新

线段树和离散化。 1e7

poj2528——Mayor's posters(离散化+区间覆盖)

DescriptionThe citizens of Bytetown, AB, could not stand that the candidates in the mayoral election...

POJ 2528 Mayor's posters (线段树 区间更新+离散化 经典染色问题)

题目链接POJ2528题目大意给定一条数轴,长度为10710^7,然后在数轴上的某些区间染色,第i次对区间染色为i,共染色n(n≤\le10510^5)次。给出每次染色的区间,问最后能看见多少种颜色....

poj2528Mayor's posters【离散化线段树区间更新】

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 54376   Accepted: 15796 Descripti...

POJ2528 Mayor's posters(线段树区间更新,离散化)

题目: Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 66425  ...

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

题意很清楚看

POJ 2528 Mayor's posters (线段树,扫描线)

#include #include #include #include #include #include #include #include #include #define LL...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2528 Mayor's posters
举报原因:
原因补充:

(最多只允许输入30个字)