知识点:扫描线,离散化
扫描线的第一道题,还没有上线段树,先理解算法为重,花了一个小时从看书到自己第一道扫描线的例题通过,李煜东讲的还是清晰易懂的
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
struct node {
double x, y1, y2;
int k;
node() {}
node(double a, double b, double c, int d): x(a), y1(b), y2(c), k(d) {}
};
int d[N];
double b[N], c[N];
node a[N];
bool cmp(node a, node b) {
return a.x < b.x;
}
int main() {
int n;
int tt = 1;
while (cin >> n && n) {
memset(d, 0, sizeof(d));
for (int i = 1; i <= n; i++) {
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
a[i] = node(x1, y1, y2, 1);
a[i + n] = node(x2, y1, y2, -1);
b[i] = y1; b[i + n] = y2;
}
sort(b + 1, b + n * 2 + 1);
int cnt = 0;
for (int i = 1; i <= n * 2; i++) {
if (!cnt || (cnt && b[i] > b[i - 1])) c[++cnt] = b[i];
}
sort(a + 1, a + n * 2 + 1, cmp);
double ans = 0;
for (int i = 1; i < n * 2; i++) {
int t1 = lower_bound(c + 1, c + cnt + 1, a[i].y1) - c;
int t2 = lower_bound(c + 1, c + cnt + 1, a[i].y2) - c;
for (int j = 1; j < cnt; j++) {
if (j >= t1 && j < t2) d[j] += a[i].k;
if (d[j] > 0) ans += (a[i + 1].x - a[i].x) * (c[j + 1] - c[j]);
}
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", tt++, ans);
}
return 0;
}