**[離散化+線段樹] 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;
}