您的位置:首页 > 其它

poj 2528 Mayor's posters

2016-05-31 01:07 302 查看
题目链接:poj2528

Mayor's posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 56457 Accepted: 16343
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


题意:有许多海报高度相同,但长度和位置不同,现给出这些海报的两边的位置和新旧次序,问最后有多少海报是露在外面的。
题目分析:处理这道题可以用线段树的成段更新,大体思想是按次序更新线段树,这样如果有重叠部分也会被覆盖。之后再对整个线段树遍历一遍,找出有多少不同的海报露在外面。

这里处理数据需要离散化,只储存边界的位置后进行排序,再根据排序的位置映射到从0开始的连续数组中。这题比较坑的一点是它的1234指的是一个个的线段而不是端点。这时使用普通的离散化会出现问题,比如这组数据:

1

3

5 6

4 5

6 8

按端点做答案是3,实际上答案为2。

还有

2

3

1 6

1 3

4 6

3

1 6

1 3

5 6

答案应为

2

3

为了避免冲突,需要在得到离散化数组后遍历一遍,遇到非连号的数在中间插入一个数,以此来区分大小线段的长度。

题目中的数据范围有问题,不过开到10w后就没有问题了。

//
// main.cpp
// POJ2528
//
// Created by teddywang on 16/5/30.
// Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 100010
int c,n;
int lazy[N<<2],num[N<<2],has[N<<2];
int li
,ri
;
int ans;

void pushdown(int rt)
{
if(lazy[rt]!=-1)
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}

void query(int l,int r,int rt)
{
if(lazy[rt]!=-1)
{
if(has[ lazy[rt] ]==0) {
// cout<<lazy[rt]<<endl;
ans++;
has[ lazy[rt] ]=1;
}
return ;
}
if(l==r) return ;
int m=(l+r)>>1;
query(lson);
query(rson);
}

void update(int p,int l1,int r1,int l,int r,int rt)
{
if(l1<=l&&r<=r1)
{
lazy[rt]=p;
//pushdown(rt);
return ;
}
pushdown(rt);
int m=(l+r)>>1;
if(m>=l1) update(p,l1,r1,lson);
if(m<r1) update(p,l1,r1,rson);
}

int searchs(int p,int l,int r,int *x)
{

while(l<=r)
{
int m=(l+r)>>1;
if(x[m]==p) return m;
else if(x[m]>p)
r=m-1;
else l=m+1;
}
return -1;
}

int main()
{
cin>>c;
while(c--)
{
cin>>n;
int len=0,m=1;
for(int i=0;i<n;i++)
{
scanf("%d%d",&li[i],&ri[i]);
num[len++]=li[i];
num[len++]=ri[i];
}
sort(num,num+len);
for(int i=1;i<len;i++)
{
if(num[i]!=num[i-1]) num[m++]=num[i];
}
for(int i=m-1;i>0;i--)
{
if(num[i]-num[i-1]>1) num[m++]=num[i-1]+1;
}
sort(num,num+m);
memset(lazy,-1,sizeof(lazy));
for(int i=0;i<n;i++)
{
int l=searchs(li[i],0,m-1,num);
int r=searchs(ri[i],0,m-1,num);
update(i,l,r,0,m-1,1);
}
ans=0;
memset(has,0,sizeof(has));
query(0,m-1,1);
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: