- 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.
The picture below illustrates the case of the sample input.
1 5 1 4 2 6 8 10 3 4 7 10
4
题目大意: 就是让你在墙上贴海报,然后很多海报,一层又一层,问你最后可以看到多少张海报。
题目分析: 数据范围很大,普通的线段树肯定超时+超内存,所以要用到离散化,离散化有基础的和稍微复杂一点的,然后这题要用到稍微复杂一点的,
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
然后,就是需要对pushdown的那段代码的理解,因为这个有一个叫做lazy思想的东西,就是用到的时候才更新节点下面的子节点,如果用不到就不更新了,这样可以大量的节省时间和空间。不过需要对于节点做好标记,然后每次pushdown之后还要恢复这个标记,刚开始就是这个点我一直理解不了。后来才理解了,这个pushdown我也感觉是线段树里面一个非常精彩的部分。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 20000 + 100;
int lisan[3*maxn];
bool visit[3*maxn];
int li[maxn],ri[maxn],tree[maxn<<4];
void pushdown(int p){
tree[p*2] = tree[p*2+1] =tree[p];
tree[p]=-1;
}
void update(int p,int l,int r,int x,int y,int v){
if(x <= l &&y >= r){
tree[p] = v;
return ;
}
if(tree[p] != -1)
pushdown(p);
int mid =(l+r)/2;
if(y<=mid) update(p<<1,l,mid,x,y,v);
else if(x>mid) update((p<<1)|1,mid+1,r,x,y,v);
else update(p<<1,l,mid,x,mid,v),update((p<<1)|1,mid+1,r,mid+1,y,v);
}
int ans;
void query(int q,int l,int r){
if(tree[q] != -1){
if(visit[tree[q]] != true){
ans++;
visit[tree[q]] =true;
}
return ;
}
if(l == r) return ;
int mid =(l+r)/2;
query(q*2,l,mid);
query(q*2+1,mid+1,r);
}
int main(){
int T;
cin>>T;
while(T--){
int n;
cin>>n;
memset(tree,-1,sizeof(tree));
memset(visit,false,sizeof(visit));
int tot =0 ;
for(int i=0;i<n;i++)
{
scanf("%d%d",&li[i],&ri[i]);
lisan[tot++]=li[i];
lisan[tot++]=ri[i];
}
sort(lisan,lisan+tot);//tot是数组长度
int m=unique(lisan,lisan+tot)-lisan;
int t=m;
for(int i=1;i<t;i++){
if(lisan[i]-lisan[i-1] > 1){
lisan[m++] = lisan[i-1]+1;
}
}
sort(lisan,lisan+m);
for(int i=0;i<n;i++)
{
int x=lower_bound(lisan,lisan+m,li[i])-lisan;
int y=lower_bound(lisan,lisan+m,ri[i])-lisan;
update(1,0,m-1,x,y,i);
}
ans=0;
query(1,0,m-1);
printf("%d\n",ans);
}
return 0;
}