poj2528(线段树+离散化)

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 54681 Accepted: 15877

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
这个题自己想了好久,题意是说市长竞选然后贴海报宣传,最后问裸露在最外面的海报有几张,一道线段树问题,数据有点大,要离散一下,否则会超内存,最开始的时候离散化出了问题,后来找了一些大牛的博客看了看,学到了一些方法,把这道题A了。直接离散线段两面端点会错,看了一些博客说如果相邻数字间距大于1,在其中加数字的,表示没有理解,后来看到了现在这种把两边端点分别用正负数表示的方法终于把离散的问题解决了,再就是线段树,每次贴海报的时候,看他是否将之前覆盖,然后每张海报记一种颜色,重叠就把之前颜色改成新海报颜色,最后查询记录数组中的颜色数即可。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define lson l,mid,i*2
#define rson mid+1,r,i*2+1
#define mem(a,b) memset((a),(b),sizeof(a))
const int MAX=20010;
struct tree
{
	int l,r,c;
}tr[MAX<<2];
int visl[MAX];
int visr[MAX];
int rk[MAX];
int total;
struct sss
{
	int point;
	int num;
}con[MAX*2];
bool cmp(sss a, sss b)
{
	return a.point<b.point;
}
void build(int l,int r,int i)
{
	tr[i].l=l;
	tr[i].r=r;
	tr[i].c=0;
	if(l == r) return ;
	int mid=(l+r)/2;
	build(lson);
	build(rson);
}
void modify(int l,int r,int i,int pos)
{
	if(tr[i].l==l&&tr[i].r==r)
	{
		tr[i].c=pos;
		return ;
	}
	if(tr[i].c>0)
	{
		tr[i*2].c=tr[i].c;
		tr[i*2+1].c=tr[i].c;
		tr[i].c=0;
	}
	if(l>=tr[i*2+1].l)
		modify(l,r,i*2+1,pos);
	else if(r<=tr[i*2].r)
        modify(l,r,i*2,pos);
    else
		{
			modify(l,tr[i*2].r,i*2,pos);
			modify(tr[i*2+1].l,r,i*2+1,pos);
		}
}
void query(int i)
{
	if(tr[i].c!=0)
	{
		if(rk[tr[i].c]==0)
		{
			total++;
			rk[tr[i].c] = 1;
		}
		return ;
	}
	query(i*2);
	query(i*2+1);
	return ;
}
int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		mem(rk,0);
		mem(visl,0);
		mem(visr,0);
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&visl[i],&visr[i]);
			con[2*i].point=visl[i];
			con[2*i+1].point=visr[i];
			con[2*i].num=-(i+1);	//这里借鉴网上大牛方法,分别用正负数表示,感觉确实方便,线段第一个端点用负数记录
			con[2*i+1].num=i+1;	//线段第二个端点用正数记录
		}
		sort(con,con+2*n,cmp);
		int tmp=con[0].point;
		int cnt=1;
		for(int i=0;i<2*n;i++)
		{
			if(tmp!=con[i].point)
			{
				cnt++;
				tmp=con[i].point;
			}
			if(con[i].num<0)
				visl[-con[i].num-1]=cnt;
			else
				visr[con[i].num-1]=cnt;
		}
		build(1,cnt,1);
		for(int i=0;i<n;i++)
			modify(visl[i],visr[i],1,i+1);
		total=0;
		query(1);
		printf("%d\n",total);
	}
	return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值