[区间离散化][区间覆盖]Mayor‘s posters POJ2528

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

 题意: 有n次操作,每次操作会在一串编号为[l, r]的方格上涂色,新涂的颜色会覆盖之前涂过的颜色,每次操作所涂的颜色都不相同,询问最终能看到多少种不同的颜色(没有被涂色并不是一种颜色)。

分析: 比较明显的区间覆盖问题,如果不考虑数据范围,本题只需要用线段树维护支持区间修改和区间查询的区间和即可。但是由于l和r可能取值非常大,且个数比较少,不能直接上线段树,因此就可以考虑先离散化。这里的离散化比较特殊,是对于区间离散化,实际上区间离散化和点的离散化一样,都需要保证离散化前后相对位置关系保持不变。不同之处在于这里的区间离散化要考虑两点间实际上是否相邻,如果相邻,那离散化后也要保持相邻,如果不相邻,那离散化后也要保持不相邻。分别给出两反例,前者的反例:第一次涂色[1, 4],第二次涂色[1, 2],第三次涂色[3, 4],错误离散化为[1, 7], [1, 3], [5, 7],后者的反例:第一次涂色[1, 6],第二次涂色[1, 2],第三次涂色[5, 6],错误离散化为[1, 4], [1, 2], [3, 4]。正确方法是:把所有点加入vector后再扫描一遍vector,如果挨着的两点是数值上是相邻的就不处理,如果是数值上是间隔开的就加入一个点,表示实际上两点是分开的。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
//所有点都加入vector之后,原本相邻的两点应继续相邻
//而原本隔开的两点应插入一点,这样lower_bound查找后下标也才是隔开的 
int n, sum[80005], lazy[80005];
vector<int> alls;

int find(int x)
{
	return lower_bound(alls.begin(), alls.end(), x)-alls.begin()+1;
} 

void push_down(int id, int ln, int rn)
{
	if(lazy[id])
	{
		lazy[id<<1] = lazy[id];
		lazy[id<<1|1] = lazy[id];
		sum[id<<1] = lazy[id]*ln;
		sum[id<<1|1] = lazy[id]*rn;
		lazy[id] = 0;
	}
}

void push_up(int id)
{
	sum[id] = sum[id<<1]+sum[id<<1|1];
}

void update(int L, int R, int C, int l, int r, int id)
{
	if(L == l && R == r)
	{
		sum[id] = C*(r-l+1);
		lazy[id] = C;
		return;
	}
	int m = l+r>>1;
	push_down(id, m-l+1, r-m);
	if(R <= m)
		update(L, R, C, l, m, id<<1);
	else if(L >= m+1)
		update(L, R, C, m+1, r, id<<1|1);
	else
		update(L, m, C, l, m, id<<1), update(m+1, R, C, m+1, r, id<<1|1);
	push_up(id);
}

int query(int L, int R, int l, int r, int id)
{
	if(L == l && R == r)
		return sum[id];
	int m = l+r>>1;
	push_down(id, m-l+1, r-m);
	if(R <= m)
		return query(L, R, l, m, id<<1);
	else if(L >= m+1)
		return query(L, R, m+1, r, id<<1|1);
	else
		return query(L, m, l, m, id<<1)+query(m+1, R, m+1, r, id<<1|1);

}

signed main()
{
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n;
		memset(lazy, 0, sizeof lazy);
		memset(sum, 0, sizeof sum);
		alls.clear();
		int l[10005], r[10005];
		int a[20005];//待离散化的点
		int cnt = 0; 
		for(int i = 1; i <= n; i++)
		{
			scanf("%d%d", &l[i], &r[i]);
			alls.push_back(l[i]);
			alls.push_back(r[i]);
		}	
		sort(alls.begin(), alls.end());
		alls.erase(unique(alls.begin(), alls.end()), alls.end());
		int len = alls.size();
		for(int i = 1; i < len; i++)
			if(alls[i-1]-alls[i] > 1)
				alls.push_back(a[i]-1);
		sort(alls.begin(), alls.end());
		for(int i = 1; i <= n; i++)
		{
			int ll = find(l[i]);
			int rr = find(r[i]);
			update(ll, rr, i, 1, alls.size(), 1); 
		}
		int ans = 0, vis[10005] = {0};//答案即为区间内不同数字个数 
		for(int i = 1; i <= alls.size(); i++)
		{
			int temp = query(i, i, 1, alls.size(), 1);
			if(temp == 0)
				continue;
			if(vis[temp] == 0)
			{
				vis[temp] = 1;
				ans++;
			}
		}
		cout << ans << endl;
	} 
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值