见注释
//参考 https://blog.csdn.net/qq_41280600/article/details/104089018
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 205;
struct Line
{
int st;
double s, e, x;
bool operator < (const Line& w) const
{
return x < w.x;
}
}line[N];
struct node
{
int cnt;
double len;
}tr[N<<2];
int n, cnt;
double fy[N], xx1, xx2, yy1, yy2;
void build(int k, int l, int r)
{
tr[k].cnt = tr[k].len = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
}
void pushup(int k, int l, int r)
{
if (tr[k].cnt)
{
tr[k].len = fy[r + 1] - fy[l];
}
else if (l == r)
{
tr[k].len = 0;
}
else if (l != r)
{
tr[k].len = tr[k << 1].len + tr[k << 1 | 1].len;
}
}
void update(int k, int l, int r, int x, int y, int d)
{
if (x <= l && r <= y)//当前子树区间被实际区间包含 全部选择
{
tr[k].cnt += d;
pushup(k, l, r);
return;
}
int mid = (l + r) >> 1;
if (x <= mid) update(k << 1, l, mid, x, y, d);
if (y > mid) update(k << 1 | 1, mid + 1, r, x, y, d);
pushup(k, l, r);
}
int find(double y)
{
return lower_bound(fy + 1, fy + 1 + cnt, y) - fy;
}
int main()
{
int kase = 0;
while (scanf("%d", &n) != EOF)
{
if (n == 0) break;
for (int i = 1, j = 1; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &xx1, &yy1, &xx2, &yy2);
line[j].s = yy1, line[j].e = yy2, line[j].x = xx1, line[j].st = 1, fy[j++] = yy1;
line[j].s = yy1, line[j].e = yy2, line[j].x = xx2, line[j].st = -1, fy[j++] = yy2;
//以x的值为扫描线的水平位置 以y的值为区间范围[yy1,yy2]
}
sort(line + 1, line + 1 + 2 * n);//将所有扫描线按x值从小到大排序 即从小到大排序
sort(fy + 1, fy + 1 + 2 * n);//将y坐标从小到大排序 然后去重 方便离散化后的查找
cnt = unique(fy + 1, fy + 1 + 2 * n) - fy - 1;
build(1, 1, cnt - 1);//结点数量是cnt-1个 因为叶子结点实际上管理的闭区间大小是2 所以需要减1才能达到l==r的状态
double ans = 0;
for (int i = 1; i <= 2 * n; i++)
{
ans += tr[1].len * (line[i].x - line[i - 1].x);
//利用线段树计算宽度 然后乘以扫描线水平位置差值 即管辖范围 得到答案
update(1, 1, cnt - 1, find(line[i].s), find(line[i].e) - 1, line[i].st);
//每次利用扫描线中存储的st 不断更新 使得tr[1].len 改变
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++kase, ans);
}
}