POJ-2528 Mayor's posters 线段树+离散

题目链接

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
思路:这题数据范围很大,直接搞超时+超内存,需要离散化:
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误(poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
线段树功能:update:成段替换 query:简单hash



#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 10005;
const int inf = 1<<30;//0x7f;
int n,d;
struct Node
{
	int x,y;
}pos[maxn];
struct node
{
	int x,y,cov;
}tree[maxn<<4];
int x[maxn<<2],vis[maxn<<2];
void PushDown( int rt )
{
	if( tree[rt].cov != -1 ){
		tree[rt<<1].cov = tree[rt<<1|1].cov = tree[rt].cov;
		tree[rt].cov = -1;
	}
}
void buildtree( int rt,int ld,int rd )
{
	tree[rt].cov = -1;
	tree[rt].x = x[ld];	tree[rt].y = x[rd];
	if( ld < rd ){
		int mid = (ld+rd)>>1;
		buildtree( rt<<1,ld,mid );
		buildtree( rt<<1|1,mid+1,rd );
	}
}
void updata( int rt,int ld,int rd,int l,int r,int id )
{
	if( l <= tree[rt].x && tree[rt].y <= r ){
		tree[rt].cov = id;
		return;
	}
	PushDown(rt);
	int mid = (ld+rd)>>1;
	if( l <= tree[rt<<1].y )	updata( rt<<1,ld,mid,l,r,id );
	if( r >= tree[rt<<1|1].x )	updata( rt<<1|1,mid+1,rd,l,r,id );
}
void query( int rt,int ld,int rd )
{
	if( tree[rt].cov != -1 ){
		vis[tree[rt].cov] = 1;
		return;
	}
	if( ld < rd ){
		int mid = (ld+rd)>>1;
		query( rt<<1,ld,mid );
		query( rt<<1|1,mid+1,rd );
	}
}
int main() 
{
	#ifndef ONLINE_JUDGE
    freopen("data.txt","r",stdin);
	#endif
	int cas,a,b,maxR;
	scanf("%d",&cas);
	while( cas -- )
	{
		maxR = 0;
		scanf("%d",&n);
		for( int i = 1; i <= n; i ++ ){
			scanf("%d%d",&pos[i].x,&pos[i].y);
			x[++maxR] = pos[i].x;
			x[++maxR] = pos[i].y;
		}
		sort( x+1,x+maxR+1 );
		maxR = unique( x+1,x+maxR+1 )-x-1;  //去重
		//个数字其实表示的是一个单位长度(并非一个点)
		for( int i = maxR; i > 1; i -- )
			if( x[i] - x[i-1] != 1 )
				x[++maxR] = x[i-1] + 1;

		sort( x+1,x+maxR+1 );
		buildtree( 1,1,maxR );
		for( int i = 1; i <= n; i ++ ){
			updata( 1,1,maxR,pos[i].x,pos[i].y,i );
		}
		memset(vis,0,sizeof(vis));
		query( 1,1,maxR );
		int ans = 0;
		for( int i = 1; i <= maxR; i ++ )
			ans += vis[i];
		printf("%d\n",ans);
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值