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

该博客介绍了如何解决一个关于选举墙海报覆盖问题的算法,其中涉及线段树和离散化的应用。给定一面墙和多张海报的覆盖范围,目标是计算最终能看见多少张海报。由于海报数量限制,可以采用离散化方法将墙的每个部分映射到较小的范围内,然后使用线段树进行区间更新和查询。博客中指出,离散化过程可能忽略某些颜色区域,但POJ题目似乎接受这种处理方式。

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

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

题意摘自:https://blog.youkuaiyun.com/winddreams/article/details/38443761

题意:给出一面墙,给出n张海报贴在墙上,每张海报都覆盖一个范围,问最后可以看到多少张海报

海报覆盖的范围很大,直接使用数组存不下,但是只有最多10000张海报,也就是说最多出现20000个点,所以可以使用离散化,将每个点离散后,重新对给出控制的区间,这样区间最大就是1到20000.可以直接使用线段树,成段更新,每次更新一个颜色,最后遍历所有的段,将还可以遍历的颜色储存,统计

将-1 定义为没有颜色,0代表有多种颜色,1到m代表各自的颜色,线段树只要在0时向下深入,其他的直接统计颜色。

但是这个题中的离散化有问题,会挤掉中间的颜色,不过poj好像也是这么做的,没有考虑中间的颜色。

如   1 ,10      1,4   7,10  这三组数的正确结果应该是还可以看到3种颜色,但是如果直接排列点的话就会挤掉5到6这一种颜色。

真正的这种成段的离散化,一定要将两个节点中还有值的话,要多加一个节点,代表两个节点中间的值。


 

#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<math.h>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define maxn 22000
const int inf=0x3f3f3f3f;
int v[maxn<<2],t,n,k,sum;
int a[11000],b[11000];
int uu[maxn<<1];
using namespace std;
void pushup(int rt)
{
    if(v[ls]&&v[rs])
        v[rt]=1;
}
void update(int L,int R,int l,int r,int rt)
{
    if(v[rt]) return;
    if(L<=l&&r<=R)
    {
        if(!v[rt])
        {
            v[rt]=1;
            k=1;
        }
        return;
    }
    int m=(l+r)>>1;
    if(L<=m&&!v[ls]) update(L,R,l,m,ls);
    if(R>m&&!v[rs])  update(L,R,m+1,r,rs);
    pushup(rt);
}
int main()
{
  scanf("%d",&t);
  while(t--)
  {
      int ma=1;
      scanf("%d",&n);
      memset(v,0,sizeof(v));
      sum=0;
      for(int i=0;i<n;i++)
      {
        scanf("%d%d",&a[i],&b[i]);
        uu[i]=a[i];
        uu[i+n]=b[i];
        uu[i+2*n]=a[i]+1;
        uu[i+3*n]=b[i]+1;
      }
      sort(uu,uu+4*n);
      int si=unique(uu,uu+4*n)-uu;
      for(int i=0;i<n;i++)
      {
          a[i]=upper_bound(uu,uu+si,a[i])-uu;
          b[i]=upper_bound(uu,uu+si,b[i])-uu;
          ma=max(ma,b[i]);
      }
      for(int i=n-1;i>=0;i--)
      {
          k=0;
          update(a[i],b[i],1,ma,1);
          if(k)  sum++;
      }
      printf("%d\n",sum);
  }
}
 
 
 
世界地图矢量数据可以通过多种网站进行下载。以下是一些提供免费下载世界地图矢量数据的网站: 1. Open Street Map (https://www.openstreetmap.org/): 这个网站可以根据输入的经纬度或手动选定范围来导出目标区域的矢量图。导出的数据格式为osm格式,但只支持矩形范围的地图下载。 2. Geofabrik (http://download.geofabrik.de/): Geofabrik提供按洲际和国家快速下载全国范围的地图数据数据格式支持shape文件格式,包含多个独立图层,如道路、建筑、水域、交通、土地利用分类、自然景观等。数据每天更新一次。 3. bbbike (https://download.bbbike.org/osm/): bbbike提供全球主要的200多个城市的地图数据下载,也可以按照bbox进行下载。该网站还提供全球数据数据格式种类齐全,包括geojson、shp等。 4. GADM (https://gadm.org/index.html): GADM提供按国家或全球下载地图数据的服务。该网站提供多种格式的数据下载。 5. L7 AntV (https://l7.antv.antgroup.com/custom/tools/worldmap): L7 AntV是一个提供标准世界地图矢量数据免费下载的网站。支持多种数据格式下载,包括GeoJSON、KML、JSON、TopJSON、CSV和高清SVG格式等。可以下载中国省、市、县的矢量边界和世界各个国家的矢量边界数据。 以上这些网站都提供了世界地图矢量数据免费下载服务,你可以根据自己的需求选择合适的网站进行下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值