扫描线专题 hdu1255

时间:2023-03-09 19:05:46
扫描线专题 hdu1255

hdu1255

求覆盖至少两次的面积,和直接求覆盖面积比,就是保证cover>1就可以了。

没有进行lazy操作,因为每一次更新伴随着询问,感觉没有必要。982MS水过。

#include <bits/stdc++.h>
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int N = 20005; struct ScanLine {
double x;
double upY, downY;
int flag;
bool operator<(const ScanLine a) const {
return x < a.x;
}
ScanLine() {}
ScanLine(double x, double y1, double y2, int f) : x(x), upY(y1), downY(y2), flag(f) {}
} line[N]; double tr[N];
int cover[N];
double yy[N]; #define lson (o<<1)
#define rson (o<<1|1)
#define mid ((l+r)>>1)
int yl, yr, v; double update(int o, int l, int r)
{
if (yl > r || yr < l) return 0; if (l == r) {
cover[o] += v;
if (cover[o] > 1) return tr[o] = yy[r + 1] - yy[l];
return tr[o] = 0;
} if (yl <= mid) update(lson, l, mid);
if (yr > mid) update(rson, mid + 1, r); return tr[o] = tr[lson] + tr[rson];
} int main()
{
int n;
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int cnt = 0;
double x1, y1, x2, y2;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[++cnt] = ScanLine(x1, y2, y1, 1);
yy[cnt] = y1;
line[++cnt] = ScanLine(x2, y2, y1, -1);
yy[cnt] = y2;
}
sort(yy + 1, yy + cnt + 1);
sort(line + 1, line + cnt + 1);
int len = unique(yy + 1, yy + cnt + 1) - yy - 1;
clr(cover, 0);
clr(tr, 0);
double ans = 0;
for (int i = 1; i < cnt; ++i) {
yl = lower_bound(yy+1, yy+len+1, line[i].downY) - yy;
yr = lower_bound(yy+1, yy+len+1, line[i].upY) - yy - 1;
v = line[i].flag;
ans += update(1, 1, len) * (line[i+1].x - line[i].x);
}
printf("%.2f\n", ans);
}
return 0;
}