有点难,扫描线易懂,离散化然后线段树处理有点不太好理解。
因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了。
AC代码
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = + ; struct Line{
double x, y1, y2;
int flag;
Line(double a, double b, double c, int d) {
x = a; y1 = b; y2 = c; flag = d;
}
bool operator < (const Line& a) const {
return x < a.x;
}
}; vector<Line> line;
map<double, int> Hash;
vector<double> y; double p[maxn << ]; // 区间长度
int covers[maxn << ]; // 覆盖次数
double len[maxn << ]; // 覆盖长度 void buildTree(int o, int l, int r) {
len[o] = covers[o] = ;
p[o] = y[r] - y[l];
if(l + != r) {
int m = (l+r) / ;
buildTree(o*, l, m);
buildTree(o*+, m, r);
}
} int ul, ur;
void update(int o, int l, int r, int f) {
if(l+ == r) {
covers[o] += f;
if(covers[o] == ) len[o] = ;
else len[o] = p[o];
}
else {
int m = (l+r) / ;
if(ul < m) update(o*, l, m, f);
if(m < ur) update(o*+, m, r, f);
// pushUp
len[o] = len[o*] + len[o*+];
}
} int main() {
int n, kase = ;
while(scanf("%d", &n) == && n) {
Hash.clear(); line.clear(); y.clear();
double x1, y1, x2, y2;
for(int i = ; i < n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line.push_back(Line(x1, y1, y2, ));
line.push_back(Line(x2, y1, y2, -));
y.push_back(y1);
y.push_back(y2);
} sort(line.begin(), line.end());
sort(y.begin(), y.end());
y.erase(unique(y.begin(), y.end()), y.end());
for(int i = ; i < (int)y.size(); i++) {
Hash[y[i]] = i;
} buildTree(, , y.size()-);
double ans = ;
for(int i = ; i < line.size(); i++) {
if(i > ) ans += (line[i].x - line[i-].x) * len[];
ul = Hash[line[i].y1]; ur = Hash[line[i].y2];
update(, , y.size()-, line[i].flag);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", kase++, ans);
}
return ;
}
如有不当之处欢迎指出!