Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

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 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

Source


线段树区间更新+离散化。一共10000000段,普通线段树开不下,但最多10000张海报,也就是20000段,离散化后即可存下。但这题的离散化比较奇葩,比如下面这组数据:
1
3
1 10
1 4
6 10

离散化后:

1->1
4->2
6->3
10->4

本来答案是3,离散化后则变成2了,画图易得,中间有一段空白的被挤掉了。如何解决?在4和6中插一个5,把空白区间填上,这样就不会被挤掉。为了方便,可以在所有长度大于1的区间中都插入一个数再离散化。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

#define lson id << 1, l, mid
#define rson id << 1 | 1, mid + 1, r

using namespace std;
const int maxn = 10000 + 10;
int n, ans;
int L[maxn], R[maxn], pos[4 * maxn], col[maxn * 16];
//pos中会插入数,所以空间开大一倍 
bool vis[4 * maxn];
void input()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> L[i] >> R[i];
}
void pushdown(int id)
{
	if (col[id])
	{
		col[id * 2] = col[id * 2 + 1] = col[id];
		col[id] = 0;
	}
}
void update(int id, int l, int r, int ql, int qr, int val)
{
	if (l >= ql && r <= qr)
	{
		col[id] = val;
		return;
	}
	pushdown(id);
	int mid = (l + r) >> 1;
	if (ql <= mid)
		update(lson, ql, qr, val);
	if (qr > mid)
		update(rson, ql, qr, val);
}
void query(int id, int l, int r)
{
	if (l == r)
	{
		if (col[id] && !vis[col[id]])
		{
			vis[col[id]] = 1;
			ans++;
		}
		return;
	}
	pushdown(id);
	int mid = (l + r) >> 1;
	query(lson);
	query(rson);
}
void solve()
{
	memset(col, 0, sizeof(col));
	ans = 0;
	memset(vis, 0, sizeof(vis));
	memset(pos, 0, sizeof(pos));
	for (int i = 0; i < n; i++)//记录端点值 
	{
		pos[i] = L[i];
		pos[n + i] = R[i];
	}
	sort(pos, pos + n * 2 - 1);
	int cnt = 1;
	for (int i = 1; i < n * 2; i++)//去重 
		if (pos[i] != pos[i - 1])
			pos[cnt++] = pos[i];
	/*for (int i = 0; i < cnt; i++)
		cout << pos[i] << endl;
	cout << "-----------------\n";*/
	for (int i = cnt - 1; i >= 1; i--)//端点值相差大于1就插入一个数,由于cnt会变所以从后往前扫 
		if (pos[i] - pos[i - 1] > 1)
			pos[cnt++] = pos[i - 1] + 1;
	sort(pos, pos + cnt);
	/*for (int i = 0; i < cnt; i++)
		cout << pos[i] << endl;*/
	//cout << "--------\n";
	for (int i = 0; i < n; i++)
	{
		int l = lower_bound(pos, pos + cnt, L[i]) - pos + 1,
			r = lower_bound(pos, pos + cnt, R[i]) - pos + 1;
		//cout << l << " " << r << "\n--------\n";
		update(1, 1, cnt, l, r, i + 1);
	}
	query(1, 1, cnt);
	cout << ans << endl;
}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		input();
		solve();
	}
	return 0;
}

 

分类: ACM/ICPC

发表评论

电子邮件地址不会被公开。 必填项已用*标注