Atlantis
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.
InputThe 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
The input file is terminated by a line containing a single 0、Don’t process it.OutputFor 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轴建立线段树 线段树上的点为需要用到的点(类似离散化)线段树的叶子结点为一段线段
t表示当前结点是否为叶子结点 f表示当前结点的区间是否被全部覆盖
#include #include using namespace std;typedef long long ll;const int MAXN = 300;double num[MAXN];struct in{double y1,y2,h,v;bool operator < (const in &a) const{ return h == a.h ? v > a.v : h < a.h;}}start[MAXN];struct Segment_Tree{struct info{double l,r,sum;int t,f;}node[MAXN << 2];void Build(int n,int l,int r){node[n].l = num[l],node[n].r = num[r];if(r - l == 1){node[n].t = 1;return;}int mid = (l+r) >> 1,lt = n << 1,rt = n << 1|1;Build(lt,l,mid);Build(rt,mid,r);node[n].t = 0;return;}void pushup(int n){if(node[n].f > 0){node[n].sum = node[n].r - node[n].l;}else if(node[n].t == 1){node[n].sum = 0;}else{node[n].sum = node[n << 1].sum + node[n << 1|1].sum;}}void modify(int n,double l,double r,int k){if(node[n].l >= l && node[n].r <= r){node[n].f += k;pushup(n);return;}int lt = n << 1,rt = n << 1|1;if(node[lt].r > l) modify(lt,l,r,k);if(node[rt].l < r) modify(rt,l,r,k);pushup(n);return;}}ST;int main(){int n,T = 0;while(scanf("%d",&n) && n){T++;double x1,y1,x2,y2;for(int i = 1;i <= n; i++){scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);int l = (i << 1) - 1,r = i << 1;num[l] = y1;num[r] = y2;start[l].y1 = y1,start[l].y2 = y2,start[l].h = x1,start[l].v = 1;start[r].y1 = y1,start[r].y2 = y2,start[r].h = x2,start[r].v = -1;}n <<= 1;sort(start+1,start+n+1);sort(num+1,num+n+1);int cnt = unique(num+1,num+n+1) - num - 1;ST.Build(1,1,cnt);double ans = 0;for(int i = 1;i <= n; i++){ans += (start[i].h-start[i-1].h)*ST.node[1].sum;ST.modify(1,start[i].y1,start[i].y2,start[i].v);}printf("Test case #%dnTotal explored area: %.2lfnn",T,ans);}return 0;}