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;
}


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

相关文章推荐

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

Mayor’s posters 题目链接:传送门DescriptionThe citizens of Bytetown, AB, could not stand that the candidat...

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

http://poj.org/problem?id=2528这题一定要写个题解。Orz。 不信看图,五月多做过一次,一直RE。现如今来做,仍然RE多发。题意:在墙壁上贴广告,广告的版面有大有小,并且...

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

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electi...

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

POJ 2528 Mayor's posters?(线段树成段更新+离散化) ACM 题目地址:POJ 2528 Mayor's posters? 题意:  在[1,1000000...
  • hcbbt
  • hcbbt
  • 2014-08-15 10:36
  • 787

POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】

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

poj2528 Mayor's posters 线段树离散化+lazy标记

线段树离散化+lazy标记 题意: 在墙上贴海报,海报可以相互覆盖,问最后可以看见几张海报 思路: 数据范围太大直接做超时超内存,需要离散化#include #include #include #i...

POJ-2528-Mayor's posters【线段树 & 离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52238 ...

POJ2528 Mayor's posters (线段树+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 66380 ...

POJ 2528 Mayor's posters 离散化+线段树+简单Hash

题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],...

poj 2528 Mayor's posters(线段树区间覆盖、离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49385   Accepted:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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