链接:戳这里
Atlantis
Time Limit: 1000MS
Memory Limit: 10000K
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
题意:
n个矩形铺在二维坐标系下,输出这些矩形面积的并
思路:
说清楚一点吧!
先说下扫描线部分,也就是矩形的上下两条边,对应的数据值为:x1,x2,y,f 。
(x1,y)为线段的左端点,(x2,y)为线段的右端点,f=1表示为矩形的下边,f=-1为矩形的上边
线段树:用来维护矩形的上下边,下边为入边,上边为出边。
线段树上的区间和sum表示当前yi高度下,有多少边在这一高度,那么与下面的yi-1则肯定有面积存在=(yi-y[i-1])*sum
一直统计下去就是答案了。
离散化是因为线段树不能存浮点数且数据过大超数组范围,我们只需要知道当前的x在原坐标的第几个位置就可以了
类似于上图,依次求的面积是s1、s2、s3、s4。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
struct line{
double x1,x2,y;
int f;
line(double x1=0,double x2=0,double y=0,int f=0):x1(x1),x2(x2),y(y),f(f){}
bool operator < (const line &a)const{
if(y==a.y) return f>a.f;
return y<a.y;
}
}L[1010];
double X[1010];
int tr[4010];
double len[4010];
void build(int root,int l,int r){
if(l==r) {
tr[root]=0;
len[root]=0;
return ;
}
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
len[root]=len[root*2]+len[root*2+1];
}
void pushup(int root,int l,int r){
if(tr[root]) len[root]=X[r+1]-X[l];
else if(l==r) len[root]=0;
else len[root]=len[root*2]+len[root*2+1];
}
void update(int root,int l,int r,int x,int y,int v){
if(x<=l && y>=r){
tr[root]+=v;
pushup(root,l,r);
return ;
}
int mid=(l+r)/2;
if(y<=mid) update(root*2,l,mid,x,y,v);
else if(x>mid) update(root*2+1,mid+1,r,x,y,v);
else {
update(root*2,l,mid,x,mid,v);
update(root*2+1,mid+1,r,mid+1,y,v);
}
pushup(root,l,r);
}
int main(){
int cas=0;
double x1,y1,x2,y2;
while(scanf("%d",&n)!=EOF){
mst(len,0);
mst(tr,0);
mst(L,0);
mst(X,0);
if(n==0) break;
int m=0,cnt=0;
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
L[++m]=line(x1,x2,y1,1);
X[m]=x1;
L[++m]=line(x1,x2,y2,-1);
X[m]=x2;
}
sort(X+1,X+m+1);
sort(L+1,L+m+1);
cnt=unique(X+1,X+m+1)-(X+1);
build(1,1,cnt-1);
double ans=0;
for(int i=1;i<m;i++){
int l=lower_bound(X+1,X+cnt+1,L[i].x1)-X;
int r=lower_bound(X+1,X+cnt+1,L[i].x2)-X-1;
if(l<=r) update(1,1,cnt-1,l,r,L[i].f);
ans+=len[1]*(L[i+1].y-L[i].y);
}
printf("Test case #%d\n",++cas);
printf("Total explored area: %.2f\n\n",ans);
}
return 0;
}
/*
2
10 10 20 20
15 15 25 25.5
*/

本文介绍了一种计算多个矩形在二维平面上所覆盖总面积的算法。通过使用扫描线和线段树的方法,有效地解决了计算矩形面积并集的问题。

被折叠的 条评论
为什么被折叠?



