Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8806 Accepted Submission(s): 3788
Problem 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 file 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
Mid-Central European Regional Contest 2000
题意:给你n个矩形,求面积的并。
http://www.cnblogs.com/scau20110726/archive/2013/03/21/2972808.html
这里有句话对扫描线的理解很有帮助:就好比一个畸形的容器,往里面倒水,从最下面往上面涨,被水淹过的部分其实就是我们要求的面积。
其实就是线段树不断更新X轴被覆盖的情况,然后倒水,水深增加为a[i+1].y-a[i].y,这样面积就是所有x轴覆盖的长度*a[i+1].y-a[i].y。
#include<cstring> #include<cstdio> #include<algorithm> #include<iostream> #define lc idx<<1 #define rc idx<<1|1 #define lson l,mid,lc #define rson mid,r,rc #define N 222 using namespace std; int n,m; double X[N]; struct Jidong { double lx,rx,y; int flag; } a[N]; struct _Jidong { double len; int cnt; } tree[N<<2]; bool cmp(Jidong a,Jidong b) { if(a.y==b.y)return a.flag>b.flag; return a.y<b.y; } void push_up(int l,int r,int idx) { if(tree[idx].cnt) tree[idx].len=X[r]-X[l]; else if(l+1==r)tree[idx].len=0; else tree[idx].len=tree[lc].len+tree[rc].len; } void build(int l,int r,int idx) { tree[idx].len=tree[idx].cnt=0; if(l+1==r)return; int mid=(l+r)>>1; build(l,mid,lc); build(mid,r,rc);///这里必须注意 } void update(int l,int r,int idx,int x,int y,int flag) { if(x<=l&&r<=y) { tree[idx].cnt+=flag; push_up(l,r,idx); return; } int mid=(r+l)>>1; if(x<mid)update(lson,x,y,flag); if(y>mid)update(rson,x,y,flag); push_up(l,r,idx); } int main() { //freopen("test.in","r",stdin); int ca=1; while(cin>>n) { if(!n)break; double x,y,_x,_y; m=1; for(int i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&x,&y,&_x,&_y); X[m]=x; a[m].lx=x,a[m].rx=_x; a[m].y=y,a[m++].flag=1; X[m]=_x; a[m].lx=x,a[m].rx=_x; a[m].y=_y,a[m++].flag=-1; } sort(X+1,X+m);//离散x,排序 sort(a+1,a+m,cmp);///y从小到大排序 int mm=1; X[mm++]=X[1]; for(int i=2; i<m; i++) {///去重 if(X[i]!=X[i-1])X[mm++]=X[i]; } build(1,mm-1,1); double ans=0; for(int i=1; i<m-1; i++) { int l=lower_bound(X+1,X+mm,a[i].lx)-X;///找对应下标 int r=lower_bound(X+1,X+mm,a[i].rx)-X; update(1,mm-1,1,l,r,a[i].flag); ans+=tree[1].len*(a[i+1].y-a[i].y);///ans+=X的覆盖长度*y增加高度 } printf("Test case #%d\n",ca++); printf("Total explored area: %.2f\n\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。