poj2528 贴海报

题意:贴海报,海报可以覆盖,会给出你每张海报的长宽,然后问你最后还能看到几张海报。

思路:不离散化,范围太大,时间复杂度和空间复杂度都太高,所以要进行离散化处理,此处用map优化。然后就是普通的线段树了。需要注意的是query函数中的一些处理细节。

#include<cstdio>
#include<cstring>
#include<map>
#include<iterator>
#include<iostream>
using namespace std;
struct node
{
	int l,r;
}p[20009];
map<int,int> s;
map<int,int>:: iterator it;
struct tree
{
	int l,r,c;
}tr[100009];
int used[20009],an;
void build(int rot,int l,int r)
{
	//cout<<l<<" "<<r<<endl;
	tr[rot].l=l;
	tr[rot].r=r;
	tr[rot].c=-1;
	if(l==r) return;
	build(2*rot,l,(l+r)/2);
	build(2*rot+1,(l+r)/2+1,r);
}
void place(int rot,int l,int r,int c)
{
	if(tr[rot].l==l&&tr[rot].r==r)
	{
		tr[rot].c=c;
		return;
	}
	int mid=(tr[rot].l+tr[rot].r)/2;
	if(tr[rot].c>0)
	{
	tr[2*rot+1].c=tr[2*rot].c=tr[rot].c;
	tr[rot].c=0;
	}
	if(r<=mid)
	{
		place(2*rot,l,r,c);
	}else if(l>mid)
	{
		place(2*rot+1,l,r,c);
	}else
	{
		place(2*rot,l,mid,c);
		place(2*rot+1,mid+1,r,c);
	}
}
void query(int rot)
{
	//cout<<tr[rot].l<<" "<<tr[rot].r<<" "<<tr[rot].c<<endl;
	if(tr[rot].c<1)
	{
		if(tr[rot].l!=tr[rot].r)
		{
		query(2*rot);
		query(2*rot+1);
		}	
	}else 
	{
		if(used[tr[rot].c]==0)
		{
			used[tr[rot].c]=1;
			an++;
		}
	}
}
int main()
{
	//FILE *fp=fopen("t.txt","r");
	int num,t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		s.clear();
		int id=1;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].l,&p[i].r);
			s[p[i].l]=1;
			s[p[i].r]=1;
		}
		num=s.size();
		for(it=s.begin();it!=s.end();it++)
		it->second=id++;
		build(1,1,num);
		for(int i=0;i<n;i++)
		{
			place(1,s[p[i].l],s[p[i].r],i+1);
		}
		memset(used,0,sizeof(used));
		an=0;
		query(1);
		printf("%d\n",an);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值