POJ 2528 Mayor's posters(线段树/区间更新 离散化)

题目链接: 传送门

Mayor's posters

Time Limit: 1000MS     Memory Limit: 65536K

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

解题思路

数据范围很大,暴力做的话超时并且爆内存。考虑数据离散化,简单来说,数据离散化就是只取我们所需要的值,比如[1000,1020],[1500,2000],我们用不到[-∞,999],[1021,1499],[2001,+∞]这些区间值,我们只需要1000,1020,1500,2000这四个值,因此将它们映射到0,1,2,3,复杂度大大的减小了。
但是普通的离散化有很大的缺陷,比如如下两组数据:
Case 1:1-10 1-4 5-10
Case 2:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4],但是可以看出线段一[1,4]覆盖了[1,2]和[3,4],符合Case 1,但是不符合Case 2,因为[1,10]并没有覆盖[1,4],[6,10]的并集区间。
为了解决这种缺陷,我们在排序后加一些处理。比如有这么一个例子:[1,2,6,10],如果相邻两个数字相差大于1,那么在他们中间加一个数(这个数的数值为相邻两数字的中间随意一个值),例子中的区间数据可以处理为[1,2,3,6,7,10]。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 11111;
bool hash[maxn];
int li[maxn],ri[maxn],ans[maxn<<2],lazy[maxn<<4];
int res;

void PushDown(int rt)
{
	if (lazy[rt] != -1)
	{
		lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
		lazy[rt] = -1;
	}
}

void upd(int L,int R,int c,int l,int r,int rt)
{
	if (L <= l && r <= R)
	{
		lazy[rt] = c;
		return;
	}
	PushDown(rt);
	int m = (l + r)>>1;
	if (L <= m)	upd(L,R,c,lson);
	if (m < R)	upd(L,R,c,rson);
}

void qry(int l,int r,int rt)
{
	if (lazy[rt] != -1)
	{
		if (!hash[lazy[rt]])	res++;
		hash[lazy[rt]] = true;
		return ;
	}
	if (l == r)	return;
	int m = (l + r) >> 1;
	qry(lson);
	qry(rson);
}

int main()
{
	int T,n;
	scanf("%d",&T);
	while (T--)
	{
		memset(lazy,-1,sizeof(lazy));
		memset(hash,false,sizeof(hash));
		scanf("%d",&n);
		int cnt = 0;
		for (int i = 0;i < n;i++)
		{
			scanf("%d%d",&li[i],&ri[i]);
			ans[cnt++] = li[i];
			ans[cnt++] = ri[i];
		}
		sort(ans,ans+cnt);
		cnt = unique(ans,ans + cnt) - ans;
		for (int i = cnt - 1;i > 0;i--)
		{
			if (ans[i] != ans[i-1] + 1)
			{
				ans[cnt++] = ans[i - 1] + 1;
			}
		}
		sort(ans,ans + cnt);
		for (int i = 0;i < n;i++)
		{
			int l = lower_bound(ans,ans + cnt,li[i]) - ans;
			int r = lower_bound(ans,ans + cnt,ri[i]) - ans;
			upd(l,r,i,0,cnt,1);
		}
		res = 0;
		qry(0,cnt,1);
		printf("%d\n",res);
	}
	return 0;
}
posted @   zxzhang  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航