POJ 2528 Mayor's posters 线段树+离散化

Mayor's posters

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

题意:市长竞选,每个市长都往墙上贴海报,海报彼此覆盖,给出粘贴顺序和每个海报的起点和长度,问最后又多少海报可见。海报数1<=n<=10000,每个海报i范围li,ri满足1<=li<=ri<=1000000。

分析:本题由于数据范围极大,直接采用线段树会超时,超内存,所以需要离散化。离散化就是压缩区间,使原有的长区间映射到新的短区间,但是区间压缩前后的覆盖关系不变,具体就是保存所有需要用到的值,排序后,分别映射到1~n,再构造线段树。但由于本题每个数字其实表示的是一个单位长度,而不是一个点,普通的离散化会造成许多错误。例如:

①:[1,10] [1,4] [5,10]
② : [1,10] [1,4] [6,10]

抽取①中3个区间的6个端点(1,10,1,4,5,10),去重排序后得到(1,4,5,10)与(1,2,3,4)建立映射后,3个新区间为[1,4],[1,2],[3,4],而②中离散化后结果相同,即显示第一个范围被完全覆盖。解决的办法是对于距离大于1的两相邻点,中间再插入一个点,即在②中端点集合变为(1,2,4,5,6,7,10)。

#include<iostream>
#include<cstdio> 
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn=10010;//端点最大数 
int tree[maxn*16];	//线段树,值为海报内容,0表示没有海报或不止一张海报,否则表示的是被第几张覆盖 
int lx[maxn],rx[maxn],lisan[maxn*4];//左右端点值的集合及离散后点的集合 
bool vis[maxn];		//标记海报是否被统计
int N,ans;

void push_down(int root)
{
	tree[root<<1]=tree[root<<1|1]=tree[root];
	tree[root]=0;
}

void update(int root,int l,int r,int L,int R,int cover)
{
	if(L<=l&&R>=r)	//当前区间能被海报完全覆盖,则更新此区间值
	{
		tree[root]=cover;
		return;
	}
	if(tree[root]) push_down(root);//不能覆盖则先将当前区间海报内容向下传递
	//分别更新左右半区间 
	int mid=(l+r)/2;
	if(L<=mid) update(root<<1,l,mid,L,R,cover);
	if(R>mid) update(root<<1|1,mid+1,r,L,R,cover);
	
}

void query(int root,int l,int r)
{
	if(tree[root])
	{//当前区间被某张海报完全覆盖,则不需向下查询 
		if(!vis[tree[root]])
		{
			vis[tree[root]]=true;
			ans++;
		}
		return;		
	}
	if(l==r) return;//叶节点 
	int mid=(l+r)/2;
	query(root<<1,l,mid);
	query(root<<1|1,mid+1,r);
	
}

int main(){
	int c,num;
	cin>>c;
	while(c--)
	{
		ans=0;
		memset(tree,0,sizeof(tree));
		memset(vis,false,sizeof(vis));
		cin>>N;//海报数目 
		num=0;	//端点数目 
		for(int i=1;i<=N;i++)
		{
			scanf("%d%d",&lx[i],&rx[i]);
			lisan[num++]=lx[i];
			lisan[num++]=rx[i];	
		}
		sort(lisan,lisan+num);
		num=unique(lisan,lisan+num)-lisan;//去重 
		for(int i=num-1;i>0;i--)
		{//若连续两个数间隔大于1则中间添加一个数
			if(lisan[i]>lisan[i-1]+1)
				lisan[num++]=lisan[i-1]+1;
		}
		sort(lisan,lisan+num);
		for(int i=1;i<=N;i++)
		{//将左右端点映射到0~num-1 
			int li=lower_bound(lisan,lisan+num,lx[i])-lisan;
			int ri=lower_bound(lisan,lisan+num,rx[i])-lisan;
			update(1,0,num-1,li,ri,i);//记录海报i覆盖范围 
		}
		query(1,0,num-1);//统计可见的海报数目 
		cout<<ans<<endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值