poj 2528(线段树 + 离散化)

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 69189 Accepted: 19940

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

写在前面:

这道题不是原创  是 某种情况上来说的翻译!!!!


这道题 其实不是我独立做出来的 然后这道题是我看的线段树模板上讲的一个东西  因为是第一次接触离散化  还是有点方  感觉这个东西很难

学会之后才发现这个东西其实很简单的 离散化这种东西

题目意思:

这道题是很经典的区间染色问题  就是给你一些区间  然后给这些区间贴海报  问你最后能看到多少海报 


你可以理解为每次都给这个区间染一种不同的颜色

没错  我的意思是每次染色都是染了不同的颜色  虽然颜色没有这么多种但是海报有啊  所以这点是需要注意一下的  每次染色都是染了不同的颜色  有了这点的基础上我们

再来看这道题  数据范围非常大  如果直接用线段树 超时 超内存  所以我们需要一种叫做离散化的操作  首先别被这个名字吓到 ~~

离散化:

怎么理解离散化呢 就是给你打个比方  如果你有三个区间 分别是 【1000-2000】,【3000-5000】,【100000-20000000】  那么我们是不是真的需要开一个20000000这么大的数组然后一个区域一个区域的来给这些部分染色呢  你也发现了对吧 没有这种必要所以我们把1000对应为1 2000对应为2     就是把这些不同的n个数字对应为1-n这些数字就好   这时候有一个 小细节需要注意  就是如果你只用了非常裸的离散化 这样还不对    因为有些数据过不了  模板上给的例子就很好 1-10  1-4  5-10 你们可以自己对应这些数字来写写看 就会发现弊端在哪了 需要注意的是  这些编号不是节点号  而是 线段的号吗  别弄混了~~


#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#define lson l , m, rt << 1 
#define rson m + 1, r , rt << 1 | 1
using namespace std;
const int N = 100020;
int col[N << 2], cnt, length;
int Left[N], Right[N];
bool Hash[N];
int G[N << 3];
void Pushdown(int rt)//这是线段树延迟更新的函数 如果感觉看不懂这个函数的话  先学学lazy_tag吧~~
{
	if (col[rt] != -1) {
		col[rt << 1] = col[rt << 1 | 1] = col[rt];
		col[rt] = -1;
	}
	return ;
}

void Update(int L, int R, int value, int l, int r,  int rt)//普通的更新操作 每次都是区域赋值~
{
	if (L <= l && r <= R) {
		col[rt] = value;
		return ;
	}
	Pushdown(rt);
	int m = (l + r) >> 1;
	if (L <= m) Update(L , R, value , lson);
	if (R > m) Update(L , R, value, rson);
	return ;
}

void Query(int l, int r, int rt)//这个查询函数我看了好久~~  
{
	if (col[rt] != -1) {//不等于-1也就意味着这一个区域是有颜色的
		if (!Hash[col[rt]])cnt ++;//如果这种颜色还没有被统计过 那就cnt++
		Hash[col[rt]] = true;//因为每种颜色只会出现一次  所以不用担心 
		return ;	//两种海报拼到一起 然后被当成是一个海报的情况 
		
	}
	if (l == r)return ;//如果到了叶子节点  而且 没有颜色就return
	int m = (l + r) >> 1;
	Query(lson);
	Query(rson);
	return ;
}

int Find_Hash(int x)//二分来查找x在G这个数组中对应的下标是什么
{		    //G就是哈希之后的表 然后二分查找
	int l = 0, r = length;
	while (l <= r) {
		int m = (l + r )>> 1;
		if (G[m] == x)return m;
		if (x >= G[m])l = m  + 1;
		else r = m;
	}
	return -1;
}

int main()
{
	int _;
	scanf("%d",&_); 
	while ( _ --) {
		memset(col , -1, sizeof(col));
		memset(Hash , false,  sizeof(Hash));//初始化~
		int n, k = 0;;
		scanf("%d",&n);
		for (int i = 0; i < n; i ++) {
			scanf("%d%d",&Left[i],&Right[i]);
			G[k++] = Left[i];
			G[k++] = Right[i];//这一步是为了把有的点都放到G里
		}
		sort(G , G + k);//排序是为了去重更方便
		int p = 1;
		for (int i = 1; i < k; i++) {
			if (G[i] != G[i - 1])G[p++] = G[i];//去重~
		}
		int temp_length = p;
		for (int i = p - 1; i > 0; i --) {
			if (G[i] != G[i - 1] + 1)G[temp_length++] = G[i - 1] + 1;//这一步是为了
		}				//在两个距离大于2的数据中加一个任意的数字 为什么要加
		sort(G , G + temp_length); 	//在上面的解释中提过了
		length = temp_length;
		for (int i = 0; i < n; i ++) {
			int l = Find_Hash(Left[i]);
			int r = Find_Hash(Right[i]);
			printf("l == %d r == %d \n",l , r);
			Update(l , r , i , 0, length , 1);
			//printf("GG\n");
		}
		cnt = 0;
		Query(0 , length, 1);
		printf("%d\n",cnt);
	}
}
再次声明 本文是为了让更多人更容易地读懂这篇代码  不是我写的   如果有人想要这份pdf可以留言 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值