HDU - 1542 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. 

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 

题意:就是扫描线求面积。

扫描线是从下到上的。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
struct node{
    double y,x1,x2;
    int flag;
    bool operator < (const node &now) const {
        return y < now.y;
    }
}g[1000];
struct segtree{
    double ll,rr,w;
    int l,r,a,b,c,cover;
    int cal_mid(){
        return (a + b) / 2;
    }
}f[1000];
int t,n,zz;
double x[1000],xx,yy;
void build(int p, int a, int b){
    f[p].a = a; f[p].b = b;
    f[p].ll = x[a]; f[p].rr = x[b];
    f[p].w = f[p].cover = f[p].c =0;
    if (a + 1 == b) return;
    int m = f[p].cal_mid();
    t++; f[p].l = t; build(t,a,m);
    t++; f[p].r = t; build(t,m,b);
    return;
}
void pushup(int p){
    if (f[p].cover) f[p].w = f[p].rr - f[p].ll; else
        if (f[p].a + 1 == f[p].b) f[p].w = 0; else
            f[p].w = f[f[p].l].w + f[f[p].r].w;
        return;
}
void Insert(int p){
    if ((xx <= f[p].ll && yy >= f[p].rr ) || f[p].a + 1 == f[p].b ){
        f[p].cover = f[p].cover + zz;
        pushup(p);
        return;
    }
    if (xx < f[f[p].l].rr) Insert(f[p].l);
    if (yy > f[f[p].r].ll) Insert(f[p].r); //    这里就要根据实际情况来判断了。
    pushup(p);
    return;
}
int main() {
    int num = 0;
    while(scanf("%d",&n) == 1 && n){
        double x1,x2,y1,y2;
        int tt = 1; 
        for (int i = 1; i <= n; i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            g[tt].x1 = x1; g[tt].x2 = x2; g[tt].flag = 1; x[tt] = x1; g[tt++].y = y1;
            g[tt].x1 = x1; g[tt].x2 = x2; g[tt].flag = -1; x[tt] = x2; g[tt++].y = y2;
        }
        sort(g + 1, g + tt); //离散化
        sort(x + 1, x + tt);
        tt--;
        t = 1;
        build(1,1,tt); //建树, 1 到 n 这种求区间长度的线段树,暂且理解为全部都是闭区间吧。
        xx = g[1].x1; yy = g[1].x2; zz = 1;
        Insert(1);
        double ans = 0;
        for (int i = 2; i <= tt; i++){
            xx = g[i].x1; yy = g[i].x2; zz = g[i].flag;
            ans += f[1].w * (g[i].y - g[i - 1].y);
            Insert(1);
        }
        printf("Test case #%d\n",++num);
        printf("Total explored area: %.2lf\n",ans);
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/81160508
今日推荐