Language:
Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don't process it. Output
For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case. Sample Input 2 10 10 20 20 15 15 25 25.5 0 Sample Output Test case #1 Total explored area: 180.00 Source |
题意:求矩形并的面积和。
思路:首先用line存平行于y轴的线段,按x的升序排列。接着将y轴坐标离散化,建立线段树,沿着扫描线(即沿着x的正方向),不
断维护y轴线段的覆盖情况,不断更新面积。详见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
const int MAXN=200+100;
int n,edge_cnt;
double y[MAXN];
struct node
{
int l,r,lazy; // lazy是记录覆盖情况,lazy<0说明有矩形的平行于y轴的左线段覆盖;lazy=0,说明该段未完全覆盖(但存在部分覆盖的情况);
double len,lf,rf;
}segtree[MAXN<<2];
struct Line
{
int flag; // flag=-1,左,flag=1,右
double x,y1,y2; // y1<y2
}line[MAXN];
int cmp(Line a,Line b)
{
return a.x<b.x;
}
void build(int rt,int l,int r) //线段树是对平行于x轴的线段端点进行离散化
{
segtree[rt].l=l; segtree[rt].r=r;
segtree[rt].lazy=segtree[rt].len=0;
segtree[rt].lf=y[l]; segtree[rt].rf=y[r];
if(l+1 == r) return ;
int mid=(l+r)>>1;
build(L(rt),l,mid); build(R(rt),mid,r);
}
void cal(int rt)
{
if(segtree[rt].lazy<0){
segtree[rt].len=segtree[rt].rf-segtree[rt].lf;
return ;
}
if(segtree[rt].l+1==segtree[rt].r)
segtree[rt].len=0;
else
segtree[rt].len=segtree[L(rt)].len+segtree[R(rt)].len;
}
void update(int rt,Line e)
{
if(segtree[rt].lf == e.y1 && segtree[rt].rf == e.y2){
segtree[rt].lazy+=e.flag; cal(rt);
return ;
}
if(e.y2<=segtree[L(rt)].rf) update(L(rt),e);
else if(e.y1>=segtree[R(rt)].lf) update(R(rt),e);
else {
Line temp=e; temp.y2=segtree[L(rt)].rf;
update(L(rt),temp);
temp=e; temp.y1=segtree[R(rt)].lf;
update(R(rt),temp);
}
cal(rt);
}
int main()
{
//freopen("text.txt","r",stdin);
int kase=0;
double x1,y1,x2,y2;
while(~scanf("%d",&n) && n){
kase++;edge_cnt=0;
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[++edge_cnt].x=x1; line[edge_cnt].flag=-1;
line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y1;
line[++edge_cnt].x=x2; line[edge_cnt].flag=1;
line[edge_cnt].y1=y1; line[edge_cnt].y2=y2; y[edge_cnt]=y2;
}
sort(line+1,line+edge_cnt+1,cmp);
sort(y+1,y+edge_cnt+1); //对y坐标离散化
build(1,1,edge_cnt);
update(1,line[1]);
double ans=0;
for(int i=2;i<=edge_cnt;i++){
ans+=segtree[1].len*(line[i].x-line[i-1].x);
update(1,line[i]);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",kase,ans);
}
return 0;
}