POJ 2528 离散化+线段树

http://poj.org/problem?id=2528

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

题目大意:给一面墙,有n个竞选者在墙上贴海报,后贴的海报可以覆盖以前贴的海报,问海报全部贴完以后,墙上有几张显露出来的海报。(露出一部分也算)

思路:看数据范围,ri可到10000000,直接建树绝对MLE+TLE,题目说了最多有10000张海报,因此我们考虑把每张海报的左右区间进行离散化处理。(保证相对大小关系不变就可以正确的处理)不熟悉离散化的可以看一下这篇博客:

https://blog.csdn.net/xiji333/article/details/88073714

离散化处理之后,就可以考虑线段树的操作了。我们用线段树维护这个区间范围内海报的编号id,若这个区间全部部分被一张海报覆盖,则id=该海报的编号,否则id=0。利用一个vis数组,在查询的时候若id==0,直接退出;否则我们令vis[id]=1;最后遍历vis数组统计总数即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;

const int maxn=20000;

struct node //海报左右端点 [l,r]
{
	int l,r;
};

struct tr//线段树节点
{
	int l,r,id;
};

tr tree[maxn<<2];//线段树
node a[10000+5];//存储海报信息
int temp[maxn<<1+5];//用于离散化
int vis[maxn+5];

void build(int i,int l,int r)
{
	tree[i].l=l;
	tree[i].r=r;
	tree[i].id=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
}

void down(int i)//id下推
{
	tree[i<<1].id=tree[i<<1|1].id=tree[i].id;
	tree[i].id=0;
}

void update(int i,int l,int r,int id)
{
	if(tree[i].l==l&&tree[i].r==r)//刚好覆盖
	{
		tree[i].id=id;
		return ;
	}
	if(tree[i].id)//覆盖了一部分 说明该区间显露的海报不止一种 进行down操作 id下传同时令该节点id=0
		down(i);
	int mid=(tree[i].l+tree[i].r)>>1;
	if(r<=mid)
		update(i<<1,l,r,id);
	else if(l>mid)
		update(i<<1|1,l,r,id);
	else
	{
		update(i<<1,l,mid,id);
		update(i<<1|1,mid+1,r,id);
	}
}

void query(int i,int l,int r)
{
	if(l==r)//叶子节点
	{
		if(tree[i].id)
			vis[tree[i].id]=1;
		return ;
	}
	if(tree[i].id)//某张海报包括了该区间的全部部分
	{
		vis[tree[i].id]=1;
		return ;
	}
	int mid=(tree[i].l+tree[i].r)>>1;
	if(r<=mid)
		query(i<<1,l,r);
	else if(l>mid)
		query(i<<1|1,l,r);
	else
	{
		query(i<<1,l,mid);
		query(i<<1|1,mid+1,r);
	}
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int k=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d %d",&a[i].l,&a[i].r);
			temp[k++]=a[i].l;
			temp[k++]=a[i].r;//用于离散化
		}
		sort(temp,temp+k);
		k=unique(temp,temp+k)-temp;//去重后元素个数
		for(int i=0;i<n;i++)//离散化 值->下标 范围为[1,k]
		{
			a[i].l=upper_bound(temp,temp+k,a[i].l)-temp;
			a[i].r=upper_bound(temp,temp+k,a[i].r)-temp;
		}
		build(1,1,k);
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)	//更新操作
			update(1,a[i].l,a[i].r,i+1);
		query(1,1,k);
		int sum=0;
		for(int i=1;i<=n;i++)
			if(vis[i])
				sum++;
		printf("%d\n",sum);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88075335