POJ1151 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 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 

细节有空再补QAQ
参考博客https://blog.csdn.net/narcissus2_/article/details/88418870?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define SIZE 405
using namespace std;
int tot = 0, n;
double y[805];//扫描线总数 离散化y数组 
struct ScanLine
{
public:
    double x;
    double y1;
    double y2;
    int k;//k为1表示入边,为2表示出边
    ScanLine() {}
    ScanLine(double _x, double _y1, double _y2, int _k)
    {
        x = _x;
        y1 = _y1;
        y2 = _y2;
        k = _k;
    }
} line[SIZE * 2];
struct SegmentTree
{
    int l;
    int r;
    double left;
    double right;
    double len;
    int cnt; //当前区间被完全覆盖的次数 
} t[SIZE * 4];
bool  cmp(ScanLine a, ScanLine b)
{
    return a.x < b.x;
}
void build(int p, int l, int r)
{
    t[p].l = l;
    t[p].r = r;
    t[p].left = y[l];
    t[p].right = y[r];
    if(l + 1 == r) return;//注意叶子节点的含义 
    t[p].len = 0.0;
    t[p].cnt = 0;
    
    int mid = (l + r) >> 1;
    build(2 * p, l, mid);
    build(2 * p + 1, mid, r);//注意这里
}
void calc(int p)
{
    if(t[p].cnt > 0) t[p].len = t[p].right - t[p].left;
    else if(t[p].r - t[p].l == 1) t[p].len = 0;
    else t[p].len = t[2 * p].len + t[2 * p + 1].len;
}
void change(int p, ScanLine b)
{
    if(t[p].left == b.y1 && t[p].right == b.y2)
    {
        t[p].cnt += b.k;
    }
    else if(b.y2 <= t[2 * p].right)
    {
        change(2 * p, b);
    }
    else if(b.y1 >= t[2 * p + 1].left)
    {
        change(2 * p + 1, b);
    }
    else
    {
        ScanLine temp;
        temp = b;
        temp.y2 = t[2 * p].right;
        change(2 * p, temp);
        temp = b;
        temp.y1 = t[2 * p + 1].left;
        change(2 * p + 1, temp);
    }
    calc(p);
}
//没必要写ask函数                                                                                                             
int main()
{
    int tcase = 0;
    while(scanf("%d", &n) && n)
    {
        tcase ++;
        int i;
        tot = 0;
        memset(y, 0, sizeof(y));
        for(i = 1; i <= n; i++)
        {
            double x1, y1, x2, y2;
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            ++tot;
            line[tot] = ScanLine(x1, y1, y2, 1);
            y[tot] = y1;
            ++tot;
            line[tot] = ScanLine(x2, y1, y2, -1);
            y[tot] = y2;
        }
        sort(y + 1, y + tot + 1);//对y坐标排序离散化
        sort(line + 1, line + tot + 1, cmp);//对扫描线数组按x坐标从小到大排序
        double ans = 0;
        build(1, 1, tot); 
        change(1, line[1]);
        for(i = 2; i <= tot; i++)
        {
            ans += (line[i].x - line[i-1].x) * t[1].len; 
            change(1, line[i]);
        }
        cout << "Test case #" << tcase <<endl;
        printf("Total explored area: %.2lf

", ans);//坑逼格式 
        
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lipoicyclic/p/13277090.html