poj 2528 線段樹離散化+染色

題目鏈接
Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 92628 Accepted: 26452
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
Source

Alberta Collegiate Programming Contest 2003.10.18

由於題目的數據是達到了1e7,簡單的用線段樹大概率會超時,但是n只到1e4,所以這裏考慮用離散化來寫。
首先我們先來講一下什麼是離散化。
就拿上面的數據舉例:
把出現的下標從小到大排序,就有了:1 2 3 4 6 7 8 10。
所以我們定義其在數組中有 :
a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4, a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 10.
一下子數據從原來的10,變成了8,所以這裏最多隻有2e4個數據,對應這道題,離散化是一個完全合理的算法。

一開始不知道線段樹是如何離散化的,當我看完線段樹是如何離散之後,相信滿滿的去寫這道題,發現我還是太年輕了, 這道題目離散化的過程有個坑。
例如:
1
3
1 6
5 6
1 3
這組數據,離散化後a[1] = 1, a[2] = 3, a[3] = 5, a[4] = 6
進行1 6後所有顏色都是1,
進行5 6後1 2 是顏色1,3 4 是顏色2,
進行1 3後1 2 是顏色3,3 4 是顏色2,
最後我們得到答案是 2,
但是仔細想想這個答案對了嗎,顯然是錯的,對於原來的數據3 ~ 5之間還是顏色1,
因此我們的答案應該是3。
所以在離散化的時候我們應該對與座標間距離大於1的在中間插入一個數。這樣就可以保證顏色不會消失了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define mid ((l + r) >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
const int N = 1e4 + 10;
int visit[N << 3], pos[N << 3], li[N], ri[N], n, ans, color[N << 4];
void push_down(int rt) {
	if(color[rt]) {
		color[ls] = color[rs] = color[rt];
		color[rt] = 0;
	}
}
void update(int rt, int l, int  r, int L, int R, int k) {
	if(l >= L && r <= R) {
		color[rt] = k;
		return ;
	}
	push_down(rt);
	if(L <= mid)	update(lson, L, R, k);
	if(R > mid)	update(rson, L, R, k);
}
void query(int rt, int l, int r) {
	if(color[rt]) {
		if(!visit[color[rt]]) {
			visit[color[rt]]++;
			ans++;
		}
		return ;
	}
	if(l == r)	return ;
	query(lson);
	query(rson);
}
int main() {
	int m, t;
	scanf("%d", &t);
	while(t--) {
		memset(visit, 0, sizeof visit);
		memset(color, 0, sizeof color);
		scanf("%d", &m);
		n = 0;
		for(int i = 1; i <= m; i++) {
			scanf("%d %d", &li[i], &ri[i]);
			pos[++n] = li[i];
			pos[++n] = ri[i];
		}
		sort(pos + 1, pos + n + 1);
		int temp = n;
		n = 1;
		for(int i = 2; i <= temp; i++)	if(pos[i] != pos[i - 1])	pos[++n] = pos[i];//去重。
		for(int i = n ; i > 1; i--)	if(pos[i] - pos[i - 1] > 1)	pos[++n] = pos[i - 1] + 1;//插入中間值。
		sort(pos + 1, pos + n + 1);
		for(int i = 1; i <= m; i++) {
			int l = lower_bound(pos + 1, pos + n + 1, li[i]) - pos;
			int r = lower_bound(pos + 1, pos + n + 1, ri[i]) - pos;
			update(1, 1, n, l, r, i);
		}
		ans = 0;
		query(1, 1, n);
		printf("%d\n", ans);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章