POJ - 2528 -- Mayor‘s posters【线段树区间最小值 + 离散化】

Mayor’s posters
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.在这里插入图片描述

题意:
给定一个1e7长度的墙,将一些长度为(r - l)的海报放到墙,问最后能看见的海报数是多少。
思路:
可以将问题转化为,求区间的最小值,让每个区间的海报标记为第i个放的,然后用线段树来做。 
查询每个海报区间的最小值是否等于i,如果等于i则没有被完全覆盖。本题的数据量太大,区间是[1, 1e7],
所以要用离散化,存取l和r的相对大小,因为n = 1e4,所以离散过后的区间为[1, 2 * n]。
时间复杂度O(nlogn) 
AC代码
#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>
#define ls 		rt << 1, l, m
#define rs 		rt << 1 | 1,  m + 1, r
using namespace std;
const int maxn = 2e4 + 5;
int mi[maxn << 2], a[maxn], add[maxn<<2];
struct node{
	int l, r, pos;
}arr[maxn];
int n;
void pushUp(int rt){
	mi[rt] = min(mi[rt << 1], mi[rt << 1 | 1]);
}
//下推标记 
void pushDown(int rt){
	add[rt << 1] = add[rt];
	add[rt << 1 | 1] = add[rt];
	mi[rt << 1] = add[rt];
	mi[rt << 1 | 1] = add[rt];
	add[rt] = 0;
}
//建树,此过程可用memset代替 
void build(int rt, int l, int r){
	if (l == r){
		mi[rt] = 0;
		return;
	}
	int m = (l + r) >> 1;
	build(ls);
	build(rs);
	pushUp(rt);
}
//区间修改 
void update(int rt, int l, int r, int L, int R, int C){
	if (L <= l && r <= R){
		mi[rt] = C;
		add[rt] = C;
		return;
	}
	if(add[rt] != 0)	pushDown(rt);
	int m = (l + r) >> 1;
	if (L <= m)		update(ls, L, R, C);
	if (m < R)		update(rs, L, R, C);
	pushUp(rt);
}
//查询区间最小值 
int query(int rt, int l, int r, int L, int R){
	if (L <= l && r <= R){
		return mi[rt];
	}
	if (add[rt] != 0)	pushDown(rt);
	int m = (l + r) >> 1;
	int res = maxn;
	if (L <= m)		res = min(res, query(ls, L, R));
	if (m < R)		res = min(res, query(rs, L, R));
	return res;
}
//离散化 
void disc(int cnt){
	sort(a, a + cnt);
	int lena = unique(a, a + cnt) - a;
	for (int i = 0; i < n; ++i){
		arr[i].l = lower_bound(a, a + lena, arr[i].l) - a + 1;
		arr[i].r = lower_bound(a, a + lena, arr[i].r) - a + 1;
	//	printf("l = %d   r = %d\n", arr[i].l, arr[i].r);
	}
}
void solve(){
	scanf("%d", &n);
	build(1, 1, n << 1);
	int cnt = 0;
	for (int i = 0; i < n; ++i){
		scanf("%d%d", &arr[i].l, &arr[i].r);
		arr[i].pos = i + 1;
		a[cnt++] = arr[i].l;
		a[cnt++] = arr[i].r;
	}
	disc(cnt);
	for (int i = 0; i < n; ++i){
		update(1, 1, n << 1, arr[i].l, arr[i].r, arr[i].pos);
	} 
	int ans = 0;
	for (int i = 0; i < n; ++i){
		int x = query(1, 1, n << 1, arr[i].l, arr[i].r);
		//printf("x = %d\n", x);
		if (x == arr[i].pos)	ans++;
	}
	printf("%d\n", ans);
}
int main(){
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; ++i) solve();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值