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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章