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

Mayor's postersPOJ - 2528

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 106,10
Sample Output
4
分析:
贴海报的顺序是按照输入倒着贴,如果贴某一块的时候发现要覆盖的地方都已经被贴过了,那么这一块不会露出;

把每一块瓷砖当作是一个单位区间,标记某一块瓷砖被贴过并查看瓷砖是否被贴上都是区间操作,但是瓷砖的数量太大,但实际上使用到的端点却很少,所以要离散化;

和一般的离散化不同,这里是区间的离散不是坐标的离散,比如a:[1,4],b:[1,10],c:[5,10],a和c就覆盖了b,但是a:[1,4],b:[1,10],c:[6,10],a和c就没有覆盖b;但是如果按照一般的离散策略就会得到两种情况都覆盖的错误结论(不信你试试)

所以正确的离散策略是:如果两个要离散的值是不是相邻的,那么他们的hash值应该相差2或更多,2就足够了;如果只设定相差1,那么就会出现上面所说的那种情况

因为是第一次用c++的unique,还是多说两句,在数组排序之后使用unique,可以把制定区间中的相邻相同元素移到最后,并且返回最后一个不重复元素的迭代器

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>   
#include<set>  
#include<bitset>  
#include<list>

#define UP(i,x,y) for(int i=x;i<=y;i++)  
#define DOWN(i,x,y) for(int i=x;i>=y;i--)  
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a) 
#define ll long long  
#define INF 0x3f3f3f3f  
#define EXP 1e-10  
#define lowbit(x) (x&-x)
 
using namespace std;
struct post{
	int l,r;
};
post poster[10010];
int x[20010];
struct node{
	int l,r;
	bool covered;
	int mid(){
		return (l+r)>>1;
	}
};
node tree[1000000];
int hash[10000010];
void build_tree(int root,int L,int R){
	tree[root].l=L;
	tree[root].r=R;
	tree[root].covered=false;
	if(L==R)return;
	int m=tree[root].mid();
	build_tree(root<<1,L,m);
	build_tree(root<<1|1,m+1,R);
} 
bool Post(int root,int s,int e){//返回true则可见
	if(tree[root].covered)return false;
	if(tree[root].l==s&&tree[root].r==e){
		tree[root].covered=true;
		return true;
	}
	int m=tree[root].mid();
	bool b;
	if(e<=m){
		b=Post(root<<1,s,e);
	}
	else if(s>m){
		b=Post(root<<1|1,s,e);
	}
	else{
		bool b1=Post(root<<1,s,m),b2=Post(root<<1|1,m+1,e);
		b=b1||b2;
	}
	if(tree[root<<1].covered&&tree[root<<1|1].covered)tree[root].covered=true;
	return b;
}
int main(){
	int t;
	int n;
	int num=0,hnum=0,sum=0;
	scanf("%d",&t);
	while(t--){
		num=0;hnum=0;sum=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d%d",&poster[i].l,&poster[i].r);
			x[num++]=poster[i].l;
			x[num++]=poster[i].r;
		}
		sort(x,x+num);
		num=unique(x,x+num)-x;
		//离散化
		for(int i=0;i<num;i++){
			hash[x[i]]=hnum;
			if(i<num-1){
				if(x[i+1]-x[i]==1){
					hnum++;
				}
				else{
					hnum+=2;
				}
			}
		}
		build_tree(1,0,hnum);
		for(int i=n-1;i>=0;i--){
			if(Post(1,hash[poster[i].l],hash[poster[i].r]))sum++;
		}
		printf("%d\n",sum); 
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值