Description
Input
Output
Sample Input
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
写代码是为了省事,没怎么考虑就写了update中的递归,找了一下午的错,才知道那个位置写错了。。。
扫描线第二弹
题意:给出了n个矩形的左下和右上的坐标,问所有的矩形面积的并是多少
1.在建立线段树是左子树为(l,mid)右子树为(mid,r),不能是mid+1,那样会使mid+1到mid一段不被统计到
2.按x由左向右扫描,因为给出的数是实数,所以除了要离散化x点外,对于线段树中的每一段的左右区间应该更新成实数y1,y2.,通过对区间的大小比对,判断直接得到值,还是继续深入,在向下一层深入的时候,要改变要判断的区间,使得区间在节点的控制内。
3.lazy数组标记了这条线段出现的次数,如果为0时,那么该节点等于左右字数节点的和,否则就是该节点的最大值。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 300
struct node1{
double l , r ;
double sum ;
}cl[maxn<<3];
int lazy[maxn<<3] ;
struct node2{
double x , y1 , y2 ;
int flag ;
}p[maxn<<3];
double s[maxn<<3] ;
bool cmp(node2 a,node2 b)
{
return a.x < b.x ;
}
void push_up(int rt)
{
if( lazy[rt] > 0 )
cl[rt].sum = cl[rt].r - cl[rt].l ;
else
cl[rt].sum = cl[rt*2].sum + cl[rt*2+1].sum ;
}
void creat(int rt,int l,int r)
{
if( r - l > 1 )
{
cl[rt].l = s[l] ;
cl[rt].r = s[r] ;
creat(rt*2,l,(l+r)/2);
creat(rt*2+1,(l+r)/2,r);
push_up(rt);
}
else
{
cl[rt].l = s[l] ;
cl[rt].r = s[r] ;
cl[rt].sum = 0 ;
}
return ;
}
void update(int rt,double y1,double y2,int flag)
{
if( cl[rt].l == y1 && cl[rt].r == y2 )
{
lazy[rt] += flag ;
push_up(rt);
return ;
}
else
{
if( cl[rt*2].r > y1 )
update(rt*2,y1,min(cl[rt*2].r,y2),flag);
if( cl[rt*2+1].l < y2 )
update(rt*2+1,max(cl[rt*2+1].l,y1),y2,flag);
push_up(rt);
}
}
int main()
{
int temp = 1 , n , i , j ;
double x1 , y1 , x2 , y2 , ans ;
while(scanf("%d", &n) && n)
{
ans = 0 ;
for(i = 0 ; i < n ; i++)
{
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
p[i].x = x1 ;
p[i].y1 = y1 ;
p[i].y2 = y2 ;
p[i].flag = 1 ;
p[i+n].x = x2 ;
p[i+n].y1 = y1 ;
p[i+n].y2 = y2 ;
p[i+n].flag = -1 ;
s[i+1] = y1 ;
s[i+n+1] = y2 ;
}
sort(s+1,s+(2*n+1));
sort(p,p+2*n,cmp);
creat(1,1,2*n);
memset(lazy,0,sizeof(lazy));
update(1,p[0].y1,p[0].y2,p[0].flag);
for(i = 1 ; i < 2*n ; i++)
{
ans += ( p[i].x-p[i-1].x )*cl[1].sum ;
update(1,p[i].y1,p[i].y2,p[i].flag);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", temp++, ans);
}
return 0;
}
poj1151-- Atlantis(线段树+离散化+扫描线),布布扣,bubuko.com
poj1151-- Atlantis(线段树+离散化+扫描线)
原文:http://blog.csdn.net/winddreams/article/details/38495093