Description
- 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.
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
Output
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
题意:在1~10000000这个区间中读取n个海报的区间信息,后面的海报会覆盖前面的海报,问最后能看到几张海报.
分析:先离散化、再线段树。
离散化,将所有出现过的点存储在排序,然后去掉相同的点。如下面的例子(题目的样例),因为单位1是一个单位长度,将下面的
1 2 3 4 6 7 8 10
— — — — — — — —
1 2 3 4 5 6 7 8
离散化 X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[7] = 8; X[8] = 10
于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),最后统计不同颜色的段数。
但是只是这样简单的离散化是错误的,
如三张海报为:1~10 1~4 6~10
离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的1~4被染为1;
第二张海报时:墙的1~2被染为2,3~4仍为1;
第三张海报时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)
X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10
这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3
最终,1~2为2,3为1,4~5为3,于是输出正确结果3。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 10005
using namespace std;
struct node
{
int l,r,mid,data;
};
node tree[MAX<<4];
int li[MAX],ri[MAX],x[MAX<<3],tot,step_x[MAX<<3],ans;
bool book[MAX*3];
void CreatTree(int rt,int l,int r)
{
tree[rt].l=l,tree[rt].r=r;
tree[rt].mid=(l+r)>>1;
tree[rt].data=0;
if(l==r){
return;
}
CreatTree(rt<<1,l,tree[rt].mid);
CreatTree(rt<<1|1,tree[rt].mid+1,r);
}
void change(int rt,int l,int r,int v){
if(tree[rt].data==v)return;
if(tree[rt].l==l && tree[rt].r==r){
tree[rt].data=v;
return;
}
if(tree[rt].data!=0){
tree[rt<<1].data=tree[rt<<1|1].data=tree[rt].data;
tree[rt].data=0;
}
if(l>tree[rt].mid)change(rt<<1|1,l,r,v);
else if(r<=tree[rt].mid)change(rt<<1,l,r,v);
else{
change(rt<<1,l,tree[rt].mid,v);
change(rt<<1|1,tree[rt].mid+1,r,v);
}
}
void CreatX()
{
sort(x+1,x+tot+1); //出现过的点排序
int n,i;
for(i=1;i<=tot;i++)step_x[i]=x[i];
n=tot;tot=1;
for(i=2;i<=n;i++){
if(step_x[i]==step_x[i-1])continue; //去除相同的点
if(step_x[i]==step_x[i-1]+1)x[++tot]=step_x[i]; //在相差大于1的两点之间加上一个数.
else{
x[++tot]=step_x[i-1]+1;
x[++tot]=step_x[i];
}
}
}
int SearchX(int data){ //查询该点对应的离散化后的点.(二分)
int l=1,r=tot,mid;
while(l<=r){
mid=(l+r)>>1;
if(x[mid]==data)return mid;
if(x[mid]>data)r=mid-1;
else l=mid+1;
}
return l;
}
void Solve(int rt)
{
if(tree[rt].data!=0 ){
if(tree[rt].data && book[tree[rt].data]==false) ans++;
book[tree[rt].data]=true;
return;
}
if(tree[rt].l==tree[rt].r)return;
Solve(rt<<1);
Solve(rt<<1|1);
}
int main()
{
int t,i,n,left,right;
scanf("%d",&t);
while(t--){
scanf("%d",&n);tot=0;
for(i=1;i<=n;i++){
scanf("%d%d",&li[i],&ri[i]);
x[++tot]=li[i];
x[++tot]=ri[i]; //存储所有点.
}
CreatX(); //离散化.
CreatTree(1,1,tot); //线段树.
for(i=1;i<=n;i++){
left=SearchX(li[i]);
right=SearchX(ri[i]);
change(1,left,right,i);
}
ans=0;
memset(book,false,sizeof(book));
Solve(1);
printf("%d\n",ans);
}
return 0;
}