poj 1151 Atlantis (線段樹+掃描線+離散化),pojatlantis
Atlantis
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 18061 |
|
Accepted: 6873 |
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
210 10 20 2015 15 25 25.50
Sample Output
Test case #1Total explored area: 180.00
求矩形覆蓋下的面積。可以把矩形平行y軸的線段都刪去,這樣就剩下很多平行x軸的線段。把線段y值按從小到大排序,每次插入一條線段得到當前覆蓋在x軸的長度,乘以該條線段和下一條線段的z縱座標之差,就是面積。求和即可。見
#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<vector>#include<queue>#include<algorithm>using namespace std;#define N 410#define ll __int64double x[N];struct line //記錄和x軸平行的線段資訊{ int f; //f為0、1代表為底邊或頂邊 double x1,x2,y; // 線段從左至右的端點,在y軸上的位置}a[N];struct node //線段樹結構體{ int f; //記錄該區間是否重疊 double l,r,len; //記錄區間的左右端點,和實際覆蓋長度}f[N*3];bool cmp(line a,line b){ return a.y<b.y;}void creat(int t,int l,int r){ f[t].l=x[l]; f[t].r=x[r]; f[t].len=f[t].f=0; if(l==r-1) return ; int tmp=t<<1,mid=(l+r)>>1; creat(tmp,l,mid); creat(tmp|1,mid,r); //線段是連續的,長度為f[t].r-f[t].l}void pushup(int t){ if(f[t].f) f[t].len=f[t].r-f[t].l; else f[t].len=f[t<<1].len+f[t<<1|1].len;}void update(int t,line a){ if(f[t].l==a.x1&&f[t].r==a.x2) { f[t].f+=a.f; pushup(t); return ; } int tmp=t<<1; if(a.x2<=f[tmp].r) //線段落在左邊子區間 update(tmp,a); else if(a.x1>=f[tmp|1].l) ////線段落在右邊子區間 update(tmp|1,a); else // 線段落在左右子區間 { line b=a; //拆分線段為兩段分屬兩個區間 b.x2=f[tmp].r; update(tmp,b); b=a; b.x1=f[tmp].r; update(tmp|1,b); } pushup(t); //向父節點求和}int main(){ int i,n,cnt=1,m; double x1,x2,y1,y2; while(scanf("%d",&n),n) { for(i=m=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); a[m].x1=x1; a[m].x2=x2; a[m].y=y1; a[m].f=1; x[m++]=x1; a[m].x1=x1; a[m].x2=x2; a[m].y=y2; a[m].f=-1; x[m++]=x2; } sort(x+1,x+m); //排序 sort(a+1,a+m,cmp); creat(1,1,m-1); update(1,a[1]); double ans=0; for(i=2;i<m;i++) { ans+=f[1].len*(a[i].y-a[i-1].y); update(1,a[i]); } printf("Test case #%d\n",cnt++); printf("Total explored area: %.2f\n\n",ans); } return 0;}