Mayor‘s posters(线段树+离散化)

Mayor’s posters(线段树+离散化)

Time limit:1000 ms
Memory limit:65536 kB
OS:Linux
judge:https://vjudge.net/contest/295911#problem/D

描述

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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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

题意

AB州拜托镇的公民无法忍受市长选举竞选中的候选人随心所欲地在各个地方张贴他们的选举海报。市议会最终决定修建一道选举墙,用于张贴海报,并引入以下规则:

每个候选人都可以在墙上贴一张海报。

所有海报的高度相同,等于墙的高度;海报的宽度可以是任意整数字节(字节是长度的单位,以字节为单位)。

墙被分成段,每个段的宽度为一个字节。

每幅海报必须完全覆盖相邻数量的墙段。

他们已经建造了一堵10000000字节长的墙(这样就有足够的地方容纳所有候选人)。竞选活动重新开始时,候选人把海报贴在墙上,海报的宽度差别很大。此外,候选人开始把他们的海报放在已经被其他海报占据的墙上。所有人都很好奇,在选举前的最后一天,谁的海报会清晰可见(全部或部分)。

你的任务是找出所有海报放置时可见海报的数量,给出海报的尺寸、位置和在选举墙上的放置顺序。

输入

输入的第一行包含一个数字C,给出后面的事例数。单个案例的第一行数据包含数字1<=n<=10000。接下来的n行描述了海报的放置顺序。n行中的i-th行包含两个整数l i和ri,分别是i-th海报左端和右端所占的墙段数。我们知道,每1<=i<=n,1<=l i<=ri<=10000000。第i张海报贴好后,它完全覆盖了编号为l i、l i+1、…,RI。

产量

对于每个输入数据集,在放置所有海报后打印可见海报的数量。

下图说明了示例输入的情况。

思路

一开始就想到了用线段树,但墙的长度太大内存会超限,于是就想到了离散化:
把所有用到的节点存下来,存完排序,然后把这段序列建树,就能大大缩减树的大小。通俗来说就是如果按照墙的真实长度建树,那么其中有些节点是压根用不到的,就像把1~3内的点变成0,那么我们只需要记录下两个端点1和3就可以了,2就是无用的

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#define maxn 100005
#define ll long long
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
struct {
	int l, r;
}tem[maxn];
int a[maxn * 4];
map<int, int> b;
int num[maxn * 2], tt = 0;
void upd(int node, int beg, int end, int l, int r, int val) {
	if (a[node]) {
		a[node * 2] = a[node];
		a[node * 2 + 1] = a[node];
		a[node] = 0;
	}
	if (beg == l && end == r) {
		a[node] = val;
		return;
	}
	int mid = (beg + end) / 2;
	if (mid >= r) upd(node * 2, beg, mid, l, r, val);
	else if (mid < l) upd(node * 2 + 1, mid + 1, end, l, r, val);
	else {
		upd(node * 2, beg, mid, l, mid, val);
		upd(node * 2 + 1, mid + 1, end, mid + 1, r, val);
	}
}
void query(int node, int beg, int end) {
	if (a[node]) {
		b[a[node]] += (end - beg + 1);
		return;
	}
	if (beg == end) return;
	int mid = (beg + end) / 2;
	query(node * 2, beg, mid);
	query(node * 2 + 1, mid + 1, end);
}
int main() {
	//freopen("in.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--) {
		memset(a, 0, sizeof(a));
		b.clear();
		int n;
		scanf("%d", &n);
		_for(i, n) {
			scanf("%d%d", &tem[i].l, &tem[i].r);
			num[tt++] = tem[i].l;
			num[tt++] = tem[i].r;
		}
		sort(num, num + tt);
		tt = unique(num, num + tt) - num;
		_for(i, n) {
			int l, r;
			l = lower_bound(num, num + tt, tem[i].l) - num;
			r = lower_bound(num, num + tt, tem[i].r) - num;
			upd(1, 1, tt, l + 1, r + 1, i + 1);
		}
		query(1, 1, tt);
		printf("%d\n", b.size());
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值