POJ2528:Mayor's posters 线段树+离散化

POJ2528:Mayor's posters 

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

题意:在一个10000000 bytes长的墙上贴海报,每次贴的时候给你海报的长度,左右区间,输出最后还能看见的海报。


思路:首先如果以【1,10000000】建立线段树的话,肯定会超时或超内存,这时需要一个离散化,通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。假设原来数据原数据:{100,200},{20,50000},{1,400};我们先排序1,20,100,200,400,50000,(注意如果有重复元素时一定要去重),然后我们建立一个映射,1->1,20->2,100->3,200->4,400->5,50000->6,这样在离散化之后原数据就变为了:{3,4},{2,6},{1,5};如果不离散化的话需要建立【1,10000000】的线段树,离散化之后只需要【1,6】,大大节省了内存空间


需要注意的是,如果只按照以上规则(排序,去重,映射)的话,还有一点小毛病,假设原数据1 10, 1 3, 6 10,如果不离散化的话最终我们可以看见3张海报,离散化之后的数据为1 4, 1,2   3,4 而此时最终我们可以看到2张海报,网上叫这种现象为离散失真,防止这种毛病出现的做法是排序之后如果相邻元素之差大于1,就在这两个元素之间插入一个数,具体原理我也没搞明白,例如原数据我们可排序去重之后为1,3,6,10,按照上述规则变为;1,2,3,5,6,9,10, 在映射之后就避免了上述错误的发生


好多博客上只进行的普通的离散化,虽然能ac,但是错误的,我以下的代码只使用了一个x数组使其下标与区间形成映射,在查找映射之后的数据使用二分,这样貌似快一些


然后就是线段树的区间问题了,



#include<stdio.h>
#include<algorithm>
const int MAXN=10005;
using namespace std;
struct NODE{
	int l,r;
	int cover;
}segTree[MAXN<<4];
int x[MAXN<<3];//为映射而使用的数组 
int l[MAXN],r[MAXN];
void build(int num,int l,int r)
{
    segTree[num].l=l;
    segTree[num].r=r;
    segTree[num].cover=false;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(num<<1,l,mid);
    build(num<<1|1,mid+1,r);
}
//二分搜素原区间端点对应离散化之后的值 
int b_search(int l,int r,int val)
{
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(x[mid]>val) r=mid-1;
		else if(x[mid]<val) l=mid+1;
		else return mid;
	}
	return -1;
}
//线段树区间问题 
bool que_upd(int i,int l,int r)
{
    if(segTree[i].cover)  return false;
    if(segTree[i].l==l&&segTree[i].r==r)
    {
        segTree[i].cover=true;
        return true;
    }    
    bool result;
    int mid=(segTree[i].l+segTree[i].r)>>1;
    if(r<=mid)  result=que_upd(i<<1,l,r);
    else if(l>mid)
       result=que_upd(i<<1|1,l,r);
    else
    {
        bool b1=que_upd(i<<1,l,mid);
        bool b2=que_upd(i<<1|1,mid+1,r);
        result=b1||b2;
    }
    //注意下面这句很关键,如果左右子树都被覆盖了,那么父亲对应的区间也被覆盖了 
    if(segTree[i<<1].cover&&segTree[i<<1|1].cover)
        segTree[i].cover=true;    
    return result;
}
int main(void)
{
	int test;
	scanf("%d",&test);
	while(test--)
	{
		int n,count;
		scanf("%d",&n);
		count=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&l[i],&r[i]);
			x[++count]=l[i];
			x[++count]=r[i];
		}
		//排序 
		sort(x+1,x+count+1);
		int m=1;
		//去重 
		for(int i=2;i<=count;i++)
		   if(x[i]!=x[i-1]) x[++m]=x[i];
		//防止离散失真那种错误的出现   
		for(int i=m;i>1;i--)
		    if(x[i]-x[i-1]>1) x[++m]=x[i]-1;
		//还需要在排序一次    
		sort(x+1,x+m+1);
		build(1,1,m);
		int ans=0;
		for(int i=n;i>=1;i--)
		{
			int L=b_search(1,m,l[i]);
			int R=b_search(1,m,r[i]);
			if(que_upd(1,L,R))
			   ans++;
		}   
		printf("%d\n",ans);   
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值