POJ - 2528 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

题意:

贴海报,后贴的可以覆盖前面的,问到最后一共可以看到多少张海报。

题解:

因为区间很大,但是需要操作的区间数很小,所以可以离散化。但是需要注意的是,这个题把区间离散化,可能会导致本不相邻的区间离散后变成相邻,求解时就会出现错误。解决方案是,离散前把区间排序,判断前后的区间端点是否相邻,不相邻就在中间插入一个数,这样就可以保证离散化区间的正确性。

#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
using namespace std;
const int maxn=1e4+5;
int lazy[maxn<<4];
bool flag[maxn<<4];
struct node
{
	int x,y;
	int val;
}p[maxn];
int s[maxn<<2];
void push_down(int rt)
{
	lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
	lazy[rt]=0;
}
void updata(int L,int R,int rt,int l,int r,int v)
{
	if(L>=l&&R<=r)
	{
		lazy[rt]=v;
		return ;
	}
	if(lazy[rt])
		push_down(rt);
	int mid=(L+R)>>1;
	if(l<=mid)
		updata(lson,l,r,v);
	if(r>mid)
		updata(rson,l,r,v);
}
int ans;
void query(int L,int R,int rt)
{
	if(lazy[rt])
	{
		if(!flag[lazy[rt]])
		{
			flag[lazy[rt]]=true;
			ans++;
		}
		return ;
	}
	if(L==R)
		return ;
	int mid=(L+R)>>1;
	query(lson);
	query(rson);
}
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{ 
		int k;
		cin>>k;
		memset(lazy,0,sizeof(lazy));
		memset(flag,false,sizeof(flag));
		for(int i=0;i<k;i++)
		{
			int a,b;
			cin>>a>>b;
			s[i<<1]=a,s[i<<1|1]=b;
			p[i].x=a;
			p[i].y=b;
			p[i].val=i+1;
		}
		sort(s,s+2*k);
		int cnt=1;
		for(int i=1;i<2*k;i++)//去重
			if(s[i]!=s[i-1])
				s[cnt++]=s[i];
		for(int i=cnt-1;i>=1;i--)//原本不相邻的两个数之间添加一个数隔开
			if(s[i]!=s[i-1]+1)
				s[cnt++]=s[i-1]+1;
		sort(s,s+cnt);//再次排序
		for(int i=0;i<k;i++)
		{
			int l=lower_bound(s,s+cnt,p[i].x)-s;//二分查找
			int r=lower_bound(s,s+cnt,p[i].y)-s;
			updata(0,cnt-1,1,l,r,p[i].val);
		}
		ans=0;
		query(0,cnt-1,1);
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2017-10-15 11:24  Zireael  阅读(110)  评论(0编辑  收藏  举报