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

题目链接:http://poj.org/problem?id=2528

                                                                                             Mayor's posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 75249 Accepted: 21693

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

题意:给定操作数n,每个操作给定区间(li,ri),每次操作将该区间涂成一种颜色,且每次操作所涂颜色互不相同,问n个操作后最后能看到几种颜色

解法:从题意看要维护区间信息,可以使用lazy标记,但如果直接对给定的区间处理,肯定是不行的,因为数据范围是1<=li<=ri<=10000000,开这么大的数组肯定会MLE的,所以就要将数据离散化。所以这里先介绍一下离散化。

离散化:例如给定三个区间(2,199),(567,9999),(10000,10000000)。由于2到199之间的数我们并不关心,所以就可以用1表示左区间,2表示右区间,依此类推,所以上述三个区间可以离散化为(1,2),(3,4),(5,6),这样就可以节省大量空间.

离散化之后就是用线段树存储区间信息喽,用-1表示该区间没有涂色,0表示有多种颜色,大于0的数color表示该区间被涂成颜色color;

下面给出离散化的代码实现:

int a[maxn],b[maxn],n;
scanf("%d",&n);
for(int i=0;i<n;i++) {
    scanf("%d",&a[i]);
    b[i]=a[i];
}
sort(b,b+n);
int size=unique(b,b+n)-b;//unique表示去重,例如1,2,2,2,3,3去重后变为1,2,3,2,2,3
for(int i=0;i<size;i++) {
    a[i]=lower_bound(b,b+size,a[i])-b+1;
    cout << a[i] << ' ';
}


下面是AC的代码:

不过这种做法在有一种情况下会出错,例如区间对于数据(1,10),(1,4),(6,10),会输出2,而不是3,原因在于离散化后的区间变为(1,4),(1,2),(3,4)这样一来会抹去原区间(4,6)的颜色,解决方法是如果某区间的li,ri分别是另两个区间的lj,rk.则在rj和lk之间加一个点(rj>=lk就不用了),但是poj没有这样的测试数据,知道就好了。

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<map>
#include<cmath>
#include<string>
#include<cstring>
#include<iomanip>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<algorithm>

using namespace std;
#define eps 0x3f3f3f3f;
#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define in(a) scanf("%d",&a)
#define in1(a,b) scanf("%d%d",&a,&b)
#define out(a) printf("%d\n",a)
#define lson le,mid,2*k
#define rson mid+1,ri,2*k+1
#define getmid int mid=(le+ri) >> 1;
#define PI 3.1415926535897932384626433832795
#define maxn 100000+10
#define ll long long

int t,n,b[2*maxn];

bool ans[maxn];

struct node1{
	int l,r;
}a[maxn];

struct node{
	int l,r,color;
}tree[maxn << 2];

void build(int le,int ri,int k)
{
	tree[k].l=le;
	tree[k].r=ri;
	tree[k].color=-1;
	if(tree[k].l==tree[k].r) return;
	getmid;
	build(lson);
	build(rson);
}

void down(int k)
{
	tree[k << 1].color=tree[k].color;
	tree[k << 1 | 1].color=tree[k].color;
	tree[k].color=0;
}

void update(int k,int t,int a,int b)
{
	if(tree[k].l>=a&&tree[k].r<=b){
		tree[k].color=t;
		return;
	}
	if(tree[k].color>0) down(k);
	int mid=(tree[k].l+tree[k].r)>>1;
	if(a<=mid) update(k<<1,t,a,b);
	if(b>mid) update(k<<1|1,t,a,b);
	if(tree[k<<1].color==tree[k<<1|1].color) tree[k].color=tree[k<<1].color;
	else tree[k].color=0;
}

void query(int k)
{
	if(tree[k].color==-1) return;
	else if(tree[k].color>0){
		ans[tree[k].color]=true;
		return;
	}
	else{
		query(k<<1);
		query(k<<1|1);
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	in(t);
	while(t--){
		in(n);
		mem(ans,false);
		int l=0,cnt=0;
		per(i,1,n){
			in1(a[i].l,a[i].r);
			b[l++]=a[i].l;
			b[l++]=a[i].r;
		}
		sort(b,b+2*n);
		int size=unique(b,b+2*n)-b;
		per(i,1,n){
			a[i].l=lower_bound(b,b+size,a[i].l)-b+1;
			a[i].r=lower_bound(b,b+size,a[i].r)-b+1;
		}
		build(1,2*n,1);
		per(i,1,n)
		   update(1,i,a[i].l,a[i].r);
		query(1);
		per(i,1,n){
		  if(ans[i]) 
		     cnt++;
		}
		out(cnt);
	}
 } 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值