Time limit:1000ms
Memory limit:65536 kB
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
c
c giving the number of cases that follow. The first line of data for a single case contains number
1
≤
n
≤
10000
1 \leq n \leq 10000
1≤n≤10000. The subsequent
n
n
n lines describe the posters in the order in which they were placed. The
i
i
i-th line among the n lines contains two integer numbers l
l
l
li and
r
r
ri which are the number of the wall segment occupied by the left end and the right end of the
i
i
i-th poster, respectively. We know that for each
1
≤
i
≤
n
1 \leq i \leq n
1≤i≤n,
1
≤
l
1 \leq l
1≤li
≤
r
\leq r
≤ri
≤
10000000
\leq 10000000
≤10000000. After the
i
i
i-th poster is placed, it entirely covers all wall segments numbered
l
l
li,
l
l
li+1 ,… ,
r
r
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.
[外链图片转存失败(img-hYspM7Xq-1563675709140)(https://vj.2vjd.cn/85d8df2191db8df82ba259ebe2bfe93e?v=1551668288)]
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
题意
这题大意是按次序往墙上贴
n
n
n张海报,第
i
i
i张海报占据的区间是
[
l
[l
[li,
r
r
ri
]
]
],求最后有多少张海报可以被看到(不被其它海报完全覆盖就算作可以被看到)。
思路
首先这题主要操作是区间修改(即海报的覆盖),输出结果时需要一次查询(即判断每个位置上有无海报),故很容易想到用线段树去写。但是1e7的数据范围直接用线段树必然会MLE,这个时候就可以采用离散化的方式来降低空间复杂度。
具体来说,这种区间覆盖问题,我们只关心每个区间是否被完全覆盖而不关心这个区间的具体长度,故只需记录每个区间的左右端点,并且以这些端点来建立线段树即可。
举个例子
给定4个区间[2,4] [3,6] [8,10] [6,9]
将端点分别取出,去重排序后得2,3,4,6,8,9,10(共7个数)
则新建立的线段树区间为
[
1
,
7
]
[1,7]
[1,7]
[
2
,
4
]
[2,4]
[2,4]变为
[
1
,
3
]
[1,3]
[1,3]
[
3
,
6
]
[3,6]
[3,6]变为
[
2
,
4
]
[2,4]
[2,4]
[
8
,
10
]
[8,10]
[8,10]变为
[
5
,
7
]
[5,7]
[5,7]
[
6
,
9
]
[6,9]
[6,9]变为
[
4
,
7
]
[4,7]
[4,7]
可验证覆盖关系不变
但是注意简单的离散化可能会出现问题,给出下面的例子:
例一:
[
1
,
10
]
[1,10]
[1,10]
[
1
,
4
]
[1,4]
[1,4]
[
5
,
10
]
[5,10]
[5,10]
例二:
[
1
,
10
]
[1,10]
[1,10]
[
1
,
4
]
[1,4]
[1,4]
[
6
,
10
]
[6,10]
[6,10]
普通离散化后都变成了
[
1
,
4
]
[1,4]
[1,4]
[
1
,
2
]
[1,2]
[1,2]
[
3
,
4
]
[3,4]
[3,4]
线段2覆盖了
[
1
,
2
]
[1,2]
[1,2],线段3覆盖了
[
3
,
4
]
[3,4]
[3,4],那么线段1是否被完全覆盖掉了呢?
例一是完全被覆盖掉了,而例二没有被完全覆盖
解决的办法是对于距离大于1的两相邻点,中间再插入一个点。
下面附上代码
(说明一下,首先众所周知递归型的线段树需要4N的空间,这里N
≤
10000
\leq10000
≤10000,而每个区间有左右端点所以就是最多20000个点,再考虑最差情况下每两个相邻点都需要插入一个点,则最多可能有40000个点,所以最后线段树的数组空间开了160000,同理,用于离散化的数组开了40000)
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAx 10000
struct node{
int l,r,cover;
};
struct node dat[MAx*16+5];
int n,m,t;
int nu[MAx*4+5],qu[MAx*4+5];
int flag[MAx+5];
void init()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&nu[i*2-1],&nu[i*2]);
qu[i*2-1]=nu[i*2-1];qu[i*2]=nu[i*2];
}
return;
}
void hash()
{
sort(nu+1,nu+n*2+1);
m=1;
for(int i=2;i<=n*2;i++)
if(nu[i]!=nu[i-1]) nu[++m]=nu[i];
int tem=m;
for(int i=2;i<=tem;i++)
if((nu[i]-nu[i-1]-1)) nu[++m]=nu[i-1]+1;
sort(nu+1,nu+m+1);
return;
}
void mt(int now,int l,int r)
{
int mid=(l+r)>>1;
dat[now].l=l;dat[now].r=r;
dat[now].cover=0;
if(l==r)
{
return;
}else
{
mt(now*2,l,mid);
mt(now*2+1,mid+1,r);
return;
}
}
void ed(int now,int l,int r,int s)
{
if(l==dat[now].l&&r==dat[now].r)
{
dat[now].cover=s;
return;
}
int mid=(dat[now].l+dat[now].r)>>1;
if(dat[now].cover)
{
dat[now*2].cover=dat[now].cover;
dat[now*2+1].cover=dat[now].cover;
dat[now].cover=0;
}
if(l>mid)
{
ed(now*2+1,l,r,s);
return;
}
if(r<=mid)
{
ed(now*2,l,r,s);
return;
}
ed(now*2,l,mid,s);
ed(now*2+1,mid+1,r,s);
return;
}
void doit()
{
for(int i=1;i<=n;i++)
{
int l=lower_bound(nu+1,nu+m+1,qu[i*2-1])-nu;
int r=lower_bound(nu+1,nu+m+1,qu[i*2])-nu;
ed(1,l,r,i);
}
return;
}
int ans(int now)
{
if(dat[now].cover)
{
if(!flag[dat[now].cover])
{
flag[dat[now].cover]=1;
return 1;
}
return 0;
}
if(dat[now].l!=dat[now].r) return ans(now*2)+ans(now*2+1);
return 0;
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
hash();
mt(1,1,m);
doit();
printf("%d\n",ans(1));
memset(flag,0,sizeof(flag));
}
return 0;
}