poj2528 离散化+线段树

 

如题:http://poj.org/problem?id=2528

 

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 46280 Accepted: 13423

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

 

 

题目大意: 贴几张海报,给出海报左右端点坐标,问最后有几张海报可见。(注意:海报左右端点是闭的)

 

题目思路:非常经典的一道线段树题目。首先离散化,将坐标压缩,比如测试用例1,4,2,6,8,10,3,4,7,10 。先用l,r数组分别保存,将左右坐标全部存到一个数组X,然后对X从小到大排序,然后去掉相邻的重复,设置数组map,从1开始一个个往里面装,就变成了1,2,3,4,6,7,8,10是排序后的X数组,压缩后的map数组对应的下标1,2,3,4,5,6,7.8

于是原区间变成(map[1],map[4]),(map[2],map[5]),(map[7],map[8])...于是再建立线段树的范围大大缩小,撑死1-20001.因为n只到10000.取出的时候二分搜索原坐标就找到了离散后的区间。

下面要考虑模拟海报贴的时候区间的更新。设海报的编号1-n。

当i海报完全满足某一个区间,更新tree[p].cover=i。如果在它搜索这个区间前某区间被别的颜色覆盖,将这个大的区间的颜色转移到下面2个子区间,父区间的cover=0。这是因为在这个区间上继续贴海报原来海报颜色被覆盖,换成新的颜色,但是原来的海报还是要更新下去。

最后search,找到所有区间颜色>0的,用辅助数组记录这个海报是否已经可以看见,如果可以,置为1,下次搜索避免重复搜索到它的另一个子区间。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100010

int l[MAXN];
int r[MAXN];
int map[MAXN*2];
int flg[MAXN];
int n;
int res;
struct node
{
       int l,r;
       int cover;
};
node tree[4*MAXN];

void build(int pos,int l,int r)
{
 if(l==r)
 {
  tree[pos].l=l;
  tree[pos].r=r;
  tree[pos].cover=0;
  return;
 }
 tree[pos].l=l;
 tree[pos].r=r;
 tree[pos].cover=0;
 int mid=(l+r)/2;
 build(pos*2,l,mid)  ;
 build(pos*2+1,mid+1,r);
}

void insert(int pos,int l,int r,int c)
{
 if(tree[pos].l==l&&tree[pos].r==r)
 {
  tree[pos].cover=c;
  return;
 }
 if(tree[pos].cover>0&&tree[pos].cover!=c)
 {
  tree[pos*2].cover=tree[pos].cover;
  tree[pos*2+1].cover=tree[pos].cover;
  tree[pos].cover=0;
 }
 int mid=(tree[pos].l+tree[pos].r)/2;
 if(l>mid)
  insert(pos*2+1,l,r,c);
 else if(r<=mid)
  insert(pos*2,l,r,c);
 else
 {
  insert(pos*2,l,mid,c);
  insert(pos*2+1,mid+1,r,c);
 }
}

void search(int pos)
{
 if(tree[pos].cover>0)
 {
  if(flg[tree[pos].cover]==0)
  {
   res++;
   flg[tree[pos].cover]=1;
  }
  return;
 }
 if(tree[pos].l==tree[pos].r)
  return;
 search(pos*2);
 search(pos*2+1);
}
int main()
{
   // freopen("C:\\1.txt","r",stdin);
 int C;
 cin>>C;
 while(C--)
 {
  res=0;
  memset(tree,0,sizeof(tree));
  memset(map,0,sizeof(map));
  memset(l,0,sizeof(l));
  memset(r,0,sizeof(r));
  memset(flg,0,sizeof(flg));
    cin>>n;
    int cnt=0;
    int i;
    for(i=0;i<n;i++)
 {
    scanf("%d%d",&l[i],&r[i]);
 map[++cnt]=l[i];
 map[++cnt]=r[i];
 }
 sort(map+1,map+cnt+1);
 int m=1;
    for(i=2;i<=cnt;i++)
    {
        if(map[i]!=map[i-1])
  map[++m]=map[i];
    }
 build(1,1,m);
 for(i=0;i<n;i++)
 {
  int n_l=lower_bound(map+1,map+m+1,l[i])-map;
  int n_r=lower_bound(map+1,map+m+1,r[i])-map;
     insert(1,n_l,n_r,i+1);
 }
 search(1);
 cout<<res<<endl;
 }
}

在disscuss上看到一个30行的程序,用map,set.真是惊呆了。回头真得好好看STL了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值