POJ2528--Mayor's posters

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

/*
总思路,有1W根棍子。但是区间达到了1QW。
显然得将数据离散化,可以离散到2W。
用个结构体,就可以实现
结构体有个num值,存坐标。还有个bool型分辨起点终点
然后就是循环将所有棍子的信息输入到结构体中。
然后再循环结构体一遍,将棍子的端点坐标进行修改
如何用这些离散后的棍子建树,现在最左边的坐标和最右边的
坐标都可以通过结构体得到。
每个节点我们都设置一个标记。
初始化都标记为-1.当整段被更新,就更新为该棍子的i值
最后查询有多少个棍子可以被看到。
离散化的时候需要注意一点:
比如有数据 (1,10) (1,4) (6,10)
还有数据 (1,10) (1,4) (5,10)
第一组数据离散化后是(1,4) (1,2) (3,4)
第二组数据离散化后是(1,4) (1,2) (3,4)
问题出来了,第二组数据明明是可以看到3张海报的。这样只能看到2张海报了
看了大牛的博客。才找到解决方法。对坐标排序好后,如果前后两个坐标
的差值大于1,就再插入一个点
比如1 4 6 10
就是1 2 4 5 6 7 10
离散1 2 3 4 5 6 7
那么就是(1,7) (1,3) (5,7)
嘿嘿。三段出来了。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 10008
int sum;//用来记录总共有多少种海报能够看见
bool flag[4*maxn];
struct Point
{
	int num;//用来存坐标
	int id;//用来存这是第几根棍子的坐标
	bool is;
}point[2*maxn];//至多2W个点
bool cmp(Point a,Point b)
{
	return a.num<b.num;
}
struct ST
{
	int l,r,col;//col用来标记棍子的i值
}st[16*maxn];//
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	st[id].col=-1;
	if(l==r)
	{
		return;
	}
	int mid=(l+r)>>1;
	buildtree(2*id,l,mid);
	buildtree(2*id+1,mid+1,r);
}
void update(int id,int l,int r,int col)
{
	if(st[id].l==l&&st[id].r==r)
	{
		st[id].col=col;
		return;
	}
	if(st[id].col!=-1)
	{
		st[2*id].col=st[2*id+1].col=st[id].col;
		st[id].col=-1;
	}
	if(r<=st[2*id].r)
	{
		update(2*id,l,r,col);
		return;
	}
	if(l>=st[2*id+1].l)
	{
		update(2*id+1,l,r,col);
		return;
	}
	update(2*id,l,st[2*id].r,col);
	update(2*id+1,st[2*id+1].l,r,col);
}
void query(int id,int l,int r)
{
	if(st[id].col!=-1&&st[id].l==l&&st[id].r==r)
	{
		if(!flag[st[id].col])
		{
			sum++;
			flag[st[id].col]=1;
			return;
		}
		return;//就因为这个return。我狂WA20次。后来还是作灌给我看的~~~
	}
	if(l==r)return;
	int mid=(st[id].l+st[id].r)>>1;
	if(r<=mid)query(2*id,l,r);
	else if(l>mid)query(2*id+1,l,r);
	else
	{
		query(2*id,l,mid);
		query(2*id+1,mid+1,r);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int ll[maxn],rr[maxn];
		sum=0;
		memset(flag,0,sizeof(flag));
		int n;
		scanf("%d",&n);
		int k=1;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&ll[i],&rr[i]);
			point[k].num=ll[i];
			point[k].id=i;
			point[k++].is=true;
			point[k].num=rr[i];
			point[k].id=i;
			point[k++].is=false;
		}
		sort(point,point+k,cmp);
		int temp=-1,su=0;
		for(int i=0;i<k;i++)
		{
			if(point[i].num>temp+1)
			{
				su++;
			}
			if(point[i].num!=temp)
			{
				temp=point[i].num;su++;
			}
			if(point[i].is)
			{
				ll[point[i].id]=su;
			}
			else rr[point[i].id]=su;
		}
		//将节点读入
		buildtree(1,1,su);
		for(int i=0;i<n;i++)
		{
			update(1,ll[i],rr[i],i+1);
		}
		query(1,1,su);
		printf("%d\n",sum);
	}
	return 0;
}


代码风格更新后:

/*
离散一般来说有两种
一种就是用映射,那就没有必要新开数组
不用映射的话,就得开数组
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define maxn 20008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
bool vis[maxn];
int ll[maxn],rr[maxn];
struct ST
{
	int l,r,col;//col是染成的颜色
}st[12*maxn];
struct Point
{
	int x,id;
	bool is;//用来标记是左端点还是右端点
}point[2*maxn];
bool cmp(Point a,Point b)
{
	return a.x<b.x;
}
void PushDown(int id)
{
	if(st[id].col)
	{
		st[2*id].col=st[2*id+1].col=st[id].col;
		st[id].col=0;
	}
}
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].col=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].col=0;
}
void update(int id,int l,int r,int newcol)
{
	if(st[id].l==l && st[id].r==r)
	{
		st[id].col=newcol;
		return;
	}
	PushDown(id);
	if(st[2*id].r>=r)
	{
		update(2*id,l,r,newcol);
		return;
	}
	if(st[2*id+1].l<=l)
	{
		update(2*id+1,l,r,newcol);
		return;
	}
	update(2*id,l,st[2*id].r,newcol);
	update(2*id+1,st[2*id+1].l,r,newcol);
}
int query(int id)
{
	if(st[id].col&&!vis[st[id].col])
	{
		vis[st[id].col]=1;
		return 1;
	}
	if(st[id].l==st[id].r)return 0;
	PushDown(id);
	return query(2*id)+query(2*id+1);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(vis,0,sizeof(vis));
		int n;
		scanf("%d",&n);
		int k=1,u,v;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&u,&v);
			point[k].x=u;
			point[k].id=i;
			point[k++].is=1;
			point[k].x=v;
			point[k].id=i;
			point[k++].is=0;
		}
		//现在点已经存好了,接下来是关键的离散化
		//离散化得先将横坐标排序,也就是先将点按横坐标排序
		sort(point+1,point+k,cmp);
		int temp=-1,su=0;
		for(int i=1;i<k;i++)
		{
			if(point[i].x>temp+1)
			{
				su++;
			}
			if(point[i].x!=temp)
			{
				temp=point[i].x;
				su++;
			}
			if(point[i].is)
			{
				ll[point[i].id]=su;
			}
			else rr[point[i].id]=su;
		}
		buildtree(1,1,su);
		for(int i=1;i<=n;i++)
		{
			update(1,ll[i],rr[i],i);
		}
		printf("%d\n",query(1));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值