码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】

时间:2014-07-13 14:02:48      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:poj2528

Mayor‘s posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39795   Accepted: 11552

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. 
bubuko.com,布布扣

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

大致题意:有n个连续的点,每次给给定区间的点涂上一种颜色,且每次涂的颜色都不同,后涂的颜色会覆盖先涂的颜色,问最后能看到几种颜色。


这题需要用到离散化来将数据映射到一段很小的范围以大幅减少时间空间复杂度,lazy区间不需要立即更新,但是在寻找lazy区间时要更新碰到的lazy父区间,提供一组测试数据:

6
5
1 4
2 6
8 10
3 4
7 10
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10
4
2 4
3 5
1 3
5 7
3
1 10
1 4 
5 10


ans:

4
2
3
4
3
2

有很多人包括我之前的代码答案都是4 2 2 4 3 2,(但是也能AC,POJ这题数据略渣),第三组数据错误的原因是忽略了位置相邻但区间不相邻的情况,解决方法是在间隔大于1的两点间插入一个中间值,这样映射的时候不该相邻的区间才不会相邻。findHash函数换成二分查找后时间从954ms减少到79ms.


//#define DEBUG
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define maxn 10002
using std::sort;

int hash[maxn << 2], vis[maxn << 2], visColor[maxn], ans;
int tree[maxn << 4], ori[maxn << 1], idHash, idVis, idOri;

int findHash(int n)
{		
	int left = 0, right = idHash - 1, mid;
	while(left <= right){
		mid = (left + right) >> 1;
		if(n < hash[mid]) right = mid - 1;
		else if(n > hash[mid]) left = mid + 1;
		else return mid;
	}
}

void pushDown(int rt)
{
	tree[rt << 1] = tree[rt << 1 | 1] = tree[rt];
	tree[rt] = -1;
}

void build(int l, int r, int rt)
{
	tree[rt] = -1;
	if(r == l) return;
	
	int mid = (l + r) >> 1;
	build(lson);
	build(rson);
}

void update(int left, int right, int val, int l, int r, int rt)
{
	if(left == l && right == r){
		tree[rt] = val; return;
	}
	
	if(tree[rt] != -1) pushDown(rt);
	
	int mid = (l + r) >> 1;
	if(right <= mid) update(left, right, val, lson);
	else if(left > mid) update(left, right, val, rson);
	else{
		update(left, mid, val, lson);
		update(mid + 1, right, val, rson);
	}
}

void query(int l, int r, int rt)
{
	if(tree[rt] != -1){
		if(!visColor[tree[rt]]){
			++ans; visColor[tree[rt]] = 1;
		}
		return;
	} //最后延迟段必定覆盖所有ori区间点,因为映射的每个点都被用到
	
	if(l == r) return;
	
	int mid = (l + r) >> 1;
	query(lson);
	query(rson);
}

int main()
{
	#ifdef DEBUG
		freopen("stdin.txt", "r", stdin);
		freopen("stdout.txt", "w", stdout);
	#endif
	
	int cas, n, a, b, i, temp;
	scanf("%d", &cas);
	
	while(cas--){
		scanf("%d", &n);
		for(i = idVis = idOri = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			ori[idOri++] = a; ori[idOri++] = b;
			vis[idVis++] = a; vis[idVis++] = b;			
		}
		
		sort(vis, vis + idVis); //temporary
		
		for(temp = idVis, i = 1; i < temp; ++i)
			if(vis[i] - vis[i - 1] > 1) vis[idVis++] = vis[i] - 1;
			
		sort(vis, vis + idVis);
		
		hash[0] = vis[0];
		for(i = idHash = 1; i < idVis; ++i)
			if(vis[i] != vis[i - 1]) hash[idHash++] = vis[i];
						
		for(i = 0; i < idOri; ++i)
			ori[i] = findHash(ori[i]);
			
		build(0, idHash - 1, 1); //映射区间
		memset(visColor, 0, sizeof(visColor));
		
		for(i = 0; i < n; ++i){
			update(ori[i << 1], ori[i << 1 | 1], i, 0, idHash - 1, 1);
		}
		
		ans = 0; query(0, idHash - 1, 1);
		printf("%d\n", ans);
	}
	return 0;
}


POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】,布布扣,bubuko.com

POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】

标签:poj2528

原文地址:http://blog.csdn.net/chang_mu/article/details/37727845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!