概述
如题:http://poj.org/problem?id=2528
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46280 | Accepted: 13423 |
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.
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
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
Source
题目大意: 贴几张海报,给出海报左右端点坐标,问最后有几张海报可见。(注意:海报左右端点是闭的)
题目思路:非常经典的一道线段树题目。首先离散化,将坐标压缩,比如测试用例1,4,2,6,8,10,3,4,7,10 。先用l,r数组分别保存,将左右坐标全部存到一个数组X,然后对X从小到大排序,然后去掉相邻的重复,设置数组map,从1开始一个个往里面装,就变成了1,2,3,4,6,7,8,10是排序后的X数组,压缩后的map数组对应的下标1,2,3,4,5,6,7.8
于是原区间变成(map[1],map[4]),(map[2],map[5]),(map[7],map[8])...于是再建立线段树的范围大大缩小,撑死1-20001.因为n只到10000.取出的时候二分搜索原坐标就找到了离散后的区间。
下面要考虑模拟海报贴的时候区间的更新。设海报的编号1-n。
当i海报完全满足某一个区间,更新tree[p].cover=i。如果在它搜索这个区间前某区间被别的颜色覆盖,将这个大的区间的颜色转移到下面2个子区间,父区间的cover=0。这是因为在这个区间上继续贴海报原来海报颜色被覆盖,换成新的颜色,但是原来的海报还是要更新下去。
最后search,找到所有区间颜色>0的,用辅助数组记录这个海报是否已经可以看见,如果可以,置为1,下次搜索避免重复搜索到它的另一个子区间。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100010
int l[MAXN];
int r[MAXN];
int map[MAXN*2];
int flg[MAXN];
int n;
int res;
struct node
{
int l,r;
int cover;
};
node tree[4*MAXN];
void build(int pos,int l,int r)
{
if(l==r)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].cover=0;
return;
}
tree[pos].l=l;
tree[pos].r=r;
tree[pos].cover=0;
int mid=(l+r)/2;
build(pos*2,l,mid) ;
build(pos*2+1,mid+1,r);
}
void insert(int pos,int l,int r,int c)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
tree[pos].cover=c;
return;
}
if(tree[pos].cover>0&&tree[pos].cover!=c)
{
tree[pos*2].cover=tree[pos].cover;
tree[pos*2+1].cover=tree[pos].cover;
tree[pos].cover=0;
}
int mid=(tree[pos].l+tree[pos].r)/2;
if(l>mid)
insert(pos*2+1,l,r,c);
else if(r<=mid)
insert(pos*2,l,r,c);
else
{
insert(pos*2,l,mid,c);
insert(pos*2+1,mid+1,r,c);
}
}
void search(int pos)
{
if(tree[pos].cover>0)
{
if(flg[tree[pos].cover]==0)
{
res++;
flg[tree[pos].cover]=1;
}
return;
}
if(tree[pos].l==tree[pos].r)
return;
search(pos*2);
search(pos*2+1);
}
int main()
{
// freopen("C:\1.txt","r",stdin);
int C;
cin>>C;
while(C--)
{
res=0;
memset(tree,0,sizeof(tree));
memset(map,0,sizeof(map));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(flg,0,sizeof(flg));
cin>>n;
int cnt=0;
int i;
for(i=0;i<n;i++)
{
scanf("%d%d",&l[i],&r[i]);
map[++cnt]=l[i];
map[++cnt]=r[i];
}
sort(map+1,map+cnt+1);
int m=1;
for(i=2;i<=cnt;i++)
{
if(map[i]!=map[i-1])
map[++m]=map[i];
}
build(1,1,m);
for(i=0;i<n;i++)
{
int n_l=lower_bound(map+1,map+m+1,l[i])-map;
int n_r=lower_bound(map+1,map+m+1,r[i])-map;
insert(1,n_l,n_r,i+1);
}
search(1);
cout<<res<<endl;
}
}
在disscuss上看到一个30行的程序,用map,set.真是惊呆了。回头真得好好看STL了。
最后
以上就是现代玉米为你收集整理的poj2528 离散化+线段树的全部内容,希望文章能够帮你解决poj2528 离散化+线段树所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复