题意:
给你 n
(n<=100) 个矩阵,问你矩阵并后的面积。
解析:
http://www.cnblogs.com/kane0526/archive/2013/02/26/2934214.html
参考了这篇题解报告,终于学会了基本的线段树扫描线。
my code
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ls o<<1
#define rs o<<1|1
#define lson ls, L, M
#define rson rs, M+1, R
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 205;
double X[maxn];
int n, m, tot;
struct Line {
double l, r, h;
int d;
Line() {}
Line(double x1, double x2, double h, int d) : l(x1), r(x2), h(h), d(d) {}
bool operator < (const Line& rhs) const {
return h < rhs.h;
}
} line[maxn];
double sumv[maxn<<2];
int cover[maxn<<2];
inline void pushUp(int o, int L, int R) {
if(cover[o]) sumv[o] = X[R+1] - X[L];
else if(L == R) sumv[o] = 0;
else sumv[o] = sumv[ls] + sumv[rs];
}
void build(int o, int L, int R) {
sumv[o] = cover[o] = 0;
if(L == R) return ;
int M = (L + R)/2;
build(lson);
build(rson);
}
void modify(int o, int L, int R, int ql, int qr, int d) {
if(ql <= L && R <= qr) {
cover[o] += d;
pushUp(o, L, R);
return ;
}
int M = (L + R)/2;
if(ql <= M) modify(lson, ql, qr, d);
if(qr > M) modify(rson, ql, qr, d);
pushUp(o, L, R);
}
int main() {
double x1, y1, x2, y2;
int cas = 1;
while(~scanf("%d", &n) && n) {
m = tot = 0;
for(int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[m] = Line(x1, x2, y1, 1);
X[m++] = x1;
line[m] = Line(x1, x2, y2, -1);
X[m++] = x2;
}
//discrete
sort(X, X+m);
tot = unique(X, X+m) - X;
sort(line, line+m);
build(1, 0, tot-1);
int ql, qr;
double ans = 0;
for(int i = 0; i < m; i++) {
ql = lower_bound(X, X+tot, line[i].l) - X;
qr = lower_bound(X, X+tot, line[i].r) - X - 1;
modify(1, 0, tot-1, ql, qr, line[i].d);
ans += sumv[1] * (line[i+1].h - line[i].h);
}
printf("Test case #%d\n", cas++);
printf("Total explored area: %.2lf\n\n", ans);
}
return 0;
}