**[离散化+线段树] POJ - 2528 Mayor's posters**
-题目大意:
- 给你一个长度为10000000的墙,在上面贴n张海报,海报的高度相同,宽度在1-10000000之间
,海报之间可能会覆盖,问最后不被完全覆盖的海报有多少张。
-分析:
首先,先贴的海报,只可能被后贴的海报覆盖,因此我们可以“从后向前贴”,每贴一次就看下是否被完全覆盖,也可以说,每贴一张海报,就看下要贴的区间有没有瓷砖,还是全都是海报。
还有一点就是我们不能直接根据单个瓷砖建树,数据太大。可以对海报端点进行离散化处理。
对海报端点进行离散化处理:
for(int i=0;i<n;i++)
{
scanf("%d %d",&posts[i].l,&posts[i].r);
x[num_cnt++]=posts[i].l;//X数组存海报的端点值
x[num_cnt++]=posts[i].r;
}
sort(x,x+num_cnt);
int ans=unique(x,x+num_cnt)-x;
int value=0;
for(int i=0;i<ans;i++)
{
Hash[x[i]]=value;
if(i<ans-1)
{
if(x[i+1]-x[i]==1)
value++;
else
value+=2;
}
}
完整代码如下:
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct post
{
int l,r;
};
struct node
{
int l,r;
bool is;
};
post posts[10005];
node Tree[400000];
int Hash[10000005];
int x[20005];
int mid(int root)
{
return (Tree[root].l+Tree[root].r)/2;
}
void BuildTree(int root,int l,int r)
{
Tree[root].l=l;
Tree[root].r=r;
Tree[root].is=false;
if(l!=r)
{
BuildTree(2*root+1,l,mid(root));
BuildTree(2*root+2,mid(root)+1,r);
}
}
bool check(int root,int l,int r)
{
if(Tree[root].is)return false;
if(Tree[root].l==l&&Tree[root].r==r)
{
Tree[root].is=true;
return true;
}
bool re=false;
if(r<=mid(root))
{
re=check(2*root+1,l,r);
}
else if(l>mid(root))
{
re=check(2*root+2,l,r);
}
else
{
bool re1=check(2*root+1,l,mid(root));
bool re2=check(2*root+2,mid(root)+1,r);
re=re1||re2;
}
if(Tree[2*root+1].is&&Tree[2*root+2].is)
Tree[root].is=true;
return re;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int num_cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d %d",&posts[i].l,&posts[i].r);
x[num_cnt++]=posts[i].l;
x[num_cnt++]=posts[i].r;
}
sort(x,x+num_cnt);
int ans=unique(x,x+num_cnt)-x;
int value=0;
for(int i=0;i<ans;i++)
{
Hash[x[i]]=value;
if(i<ans-1)
{
if(x[i+1]-x[i]==1)
value++;
else
value+=2;
}
}
BuildTree(0,0,value);
int re=0;
for(int i=n-1;i>=0;i--)
{
if(check(0,Hash[posts[i].l],Hash[posts[i].r]))
re++;
}
printf("%d\n",re);
}
return 0;
}