1. 程式人生 > >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

題解:

基礎的線段樹區間更新查詢就不說了,主要考點是資料離散化。

離散化,把無限空間中有限的個體對映到有限的空間中去,以此提高演算法的時空效率。

通俗的說,離散化是在不改變資料相對大小的條件下,對資料進行相應的縮小。

主要用於所給資料的上限太大無法開陣列但只需要相對大小關係就可以做題的情況。

也就是說當資料只與它們之間的相對大小有關,而與具體是多少無關時,可以進行離散化。

一般離散化前都需要排序(sort)和去重(unique)兩步。(如果不知道unique函式的請進海克斯傳送門

程式碼:

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

using namespace std;

const int MAXN = 10005;

bool Tree[MAXN*4];
bool Change[MAXN*4];
struct D{
	int l,r;
}board[MAXN];
int post[MAXN*2];
int top;
int hash[10000005];

void Build(int temp,int left ,int right){
	Tree[temp] = false;
	if(left == right)return ;
	int mid = left + (right-left)/2;
	Build(temp<<1,left,mid);
	Build(temp<<1|1,mid+1,right);
}

bool Updata(int temp,int left,int right,int ql,int qr){//更新加查詢 
	if(Tree[temp])return false;
	if(ql>right || qr<left)return false;
	if(left == right){
		Tree[temp] = true;
		return true;
	}
	int mid = left + (right-left)/2;
	bool re1,re2;
	re1 = Updata(temp<<1,left,mid,ql,qr);
	re2 = Updata(temp<<1|1,mid+1,right,ql,qr);
	if(Tree[temp<<1] && Tree[temp<<1|1])Tree[temp] = true;
	return re1||re2;
}

int main(){
	
	int T,N,sum,len;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&N);
		sum = top = 0;
		memset(Change,false,sizeof Change);
		for(int _=0 ; _<N ; _++){
			scanf("%d %d",&board[_].l,&board[_].r);
			post[top++] = board[_].l;
			post[top++] = board[_].r;
		}
		sort(post,post+top);//排序 
		len = unique(post,post+top) - post;//去重 
		Build(1,0,len-1);
		for(int _=0 ; _<len ; _++)hash[post[_]] = _;//離散化 
		for(int _=N-1 ; _>=0 ; _--){
			if(Updata(1,0,len-1,hash[board[_].l],hash[board[_].r]))++sum;
		}
		printf("%d\n",sum);
	}
	
	return 0;
}