poj2528Mayor's posters【线段树+离散化】

原创 2015年11月17日 21:27:05

Language:
Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 52643   Accepted: 15337

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn=100010;
int num[maxn<<1];
int sum[maxn<<2];
int vis[maxn];
struct Node{
	int s,e;
}A[maxn];
int BS(int n,int k){
	int l=1,r=k;
	while(l<=r){
		int mid=(l+r)>>1;
		if(n==num[mid])return mid;
		else if(n<num[mid]){
			r=mid-1;
		}
		else {
			l=mid+1;
		}
	}
}
void pushdown(int rt){
	if(sum[rt]){
		sum[rt<<1]=sum[rt<<1|1]=sum[rt];
		sum[rt]=0;
	}
}
void update(int left,int right,int k,int l,int r,int rt){
	if(left<=l&&r<=right){
		sum[rt]=k;
		return;
	}
	int mid=(l+r)>>1;
	pushdown(rt);
	if(left<=mid)update(left,right,k,lson);
	if(right>mid)update(left,right,k,rson);
}
int ans;
void query(int l,int r,int rt){
	if(sum[rt]){
		if(!vis[sum[rt]]){
			vis[sum[rt]]=1;
			ans++;
		}
		return ;
	}
	if(l==r)return ;
	int mid=(l+r)>>1;
	query(lson);
	query(rson);
}
int main()
{
	int t,i,j,n;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		memset(vis,0,sizeof(vis));
		memset(sum,0,sizeof(sum));
		int m=0,k=1;
		for(i=1;i<=n;++i){
			scanf("%d%d",&A[i].s,&A[i].e);
			num[++m]=A[i].s;
			num[++m]=A[i].e;
		}
		sort(num,num+m+1);
		for(i=2;i<=m;++i){
			if(num[i]!=num[i-1]){
				num[++k]=num[i];
			}
		}
		for(i=k;i>1;--i){
			if(num[i]!=num[i-1]+1)num[++k]=num[i-1]+1;
		}
		sort(num,num+k+1);
		for(i=1;i<=n;++i){
			int l=BS(A[i].s,k);
			int r=BS(A[i].e,k);
			update(l,r,i,1,k,1);
		}
		ans=0;query(1,k,1);
		printf("%d\n",ans);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj2528 Mayor's posters 线段树+离散化(经典)

题意: 市长竞选,每个市长都往墙上贴海报,海报之间彼此可以覆盖,给出粘贴顺序和每个海报的起点和长度,问最后有多少海报是可见的。...

poj2528Mayor's posters 离散化+线段树区间更新

线段树和离散化。 1e7

POJ 2528 Mayor's posters (线段树 区间更新+离散化 经典染色问题)

题目链接POJ2528题目大意给定一条数轴,长度为10710^7,然后在数轴上的某些区间染色,第i次对区间染色为i,共染色n(n≤\le10510^5)次。给出每次染色的区间,问最后能看见多少种颜色....

poj2528Mayor's posters【离散化线段树区间更新】

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 54376   Accepted: 15796 Descripti...

POJ2528 Mayor's posters(线段树区间更新,离散化)

题目: Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 66425  ...

poj 2528 Mayor's posters(线段树,离散化)

题意很清楚看

POJ 2528 Mayor's posters (线段树区间更新 + 离散化)

POJ 2528 Mayor's posters (线段树区间更新 + 离散化)

POJ 2528 Mayor's posters (线段树成段更新+离散化)

Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35509 Accepted: 10306 D...

poj 2528 Mayor's posters【离散化+线段树】

题目:poj 2528 Mayor's posters 题意:给一个长度非常长的墙上贴长度为ai的海报,由于有的会覆盖掉,求最后能看见的海报个数。 分析:题目和POJ2777 一模...

Poj 2528 Mayor's posters (线段树区间更新+离散化)

题意向墙上按顺序贴n张海报,第i张海报覆盖范围为[li , ri], 求最后能看到几张海报。题解首先肯定是线段树的区间更新,但是由于 li 和 ri 的范围是 1e7, 直接存数会爆炸,所以需要离散化...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2528Mayor's posters【线段树+离散化】
举报原因:
原因补充:

(最多只允许输入30个字)