Language:
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:
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 Source |
题意:给出n个海报所覆盖的范围,求最后贴完海报后所能看到海报的种类。
思路:线段树的区间更新,但是查种类的时候要逐个的往下搜索查询。这个数据量有点大,所以要离散化。而离散化的话要注意如果两个数相差大于1,那么中间还要插入一个值不然离散化后更新查询的结果是错误的。注意里面的col数组要开到16*maxn。因为x数组因为l和r以及中间插入的数要至少开4*maxn,所以col要是x数组长度的4倍。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10005;
int l[maxn],r[maxn],x[4*maxn];
int col[16*maxn];
bool hasht[4*maxn];
int ans=0;
void pushdown(int i)
{
if(col[i]!=-1)
{
col[i<<1]=col[i<<1|1]=col[i];
col[i]=-1;
}
}
void update(int i,int l,int r,int x,int y,int c)
{
if(x<=l&&r<=y)
{
col[i]=c;
return;
}
pushdown(i);
int mid=(l+r)/2;
if(x<=mid) update(2*i,l,mid,x,y,c);
if(y>mid) update(2*i+1,mid+1,r,x,y,c);
}
void query(int i,int l,int r)
{
if(col[i]!=-1)
{
if(hasht[col[i]]==false) ans++;
hasht[col[i]]=true;
return;
}
if(l==r)
return;
int mid=(l+r)/2;
query(2*i,l,mid);
query(2*i+1,mid+1,r);
}
int main()
{
int c,n;
scanf("%d",&c);
while(c--)
{
memset(col,-1,sizeof(col));
memset(hasht,false,sizeof(hasht));
scanf("%d",&n);
int pos=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&l[i],&r[i]);
x[pos++]=l[i];
x[pos++]=r[i];
}
sort(x,x+pos);
int frnt=1;
for(int i=1;i<pos;i++)
if(x[i]!=x[i-1]) x[frnt++]=x[i];//去除重点
for(int i=frnt-1;i>0;i--)
if(x[i]!=x[i-1]+1) x[frnt++]=x[i-1]+1;//在相差大于1的数之间插入一个数
sort(x,x+frnt);
ans=0;
for(int i=0;i<n;i++)
{
int li=lower_bound(x,x+frnt,l[i])-x;
int ri=lower_bound(x,x+frnt,r[i])-x;
update(1,0,frnt,li,ri,i);
}
query(1,0,frnt);
printf("%d\n",ans);
}
return 0;
}