2 10 10 20 20 15 15 25 25.5 0
Test case #1 Total explored area: 180.00
矩形面积并模板题
#include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 400005; int n, m, tt = 0; double l, r, a[maxn], ans; struct seg { double h, l, r; int k; bool operator <(const seg&a) { return h < a.h; } }p[maxn]; struct ST { double dis[maxn]; int sum[maxn]; void build(int x, int l, int r) { sum[x] = dis[x] = 0; if (l == r) return; else { int mid = l + r >> 1; build(x + x, l, mid); build(x + x + 1, mid + 1, r); } } void insert(int x, int l, int r, int ll, int rr, int v) { if (ll <= l&&r <= rr) { if (v < 0 && sum[x] == 0) goto next; sum[x] += v; if (sum[x]) dis[x] = a[r + 1] - a[l]; else { if(l==r) dis[x]=0; else dis[x] = dis[x + x] + dis[x + x + 1]; } } else { next: int mid = l + r >> 1; if (sum[x]) { insert(x + x, l, mid, l, r, sum[x]); insert(x + x + 1, mid + 1, r, l, r, sum[x]); sum[x] = 0; } if (ll <= mid) insert(x + x, l, mid, ll, rr, v); if (rr > mid) insert(x + x + 1, mid + 1, r, ll, rr, v); dis[x] = dis[x + x] + dis[x + x + 1]; } } }st; int main() { while (scanf("%d", &n), n) { for (int i = 1; i <= n; i++) { scanf("%lf%lf%lf%lf", &l, &p[i + i - 1].h, &r, &p[i + i].h); p[i + i - 1].l = p[i + i].l = a[i + i - 1] = l; p[i + i - 1].r = p[i + i].r = a[i + i] = r; p[i + i - 1].k = 1; p[i + i].k = -1; } sort(a + 1, a + n + n + 1); m = unique(a + 1, a + n + n + 1) - a; sort(p + 1, p + n + n + 1); ans = 0; st.build(1, 1, m - 2); for (int i = 1; i <= n + n; i++) { int u = lower_bound(a + 1, a + m, p[i].l) - a; int v = lower_bound(a + 1, a + m, p[i].r) - a; st.insert(1, 1, m - 2, u, v - 1, p[i].k); ans += (p[i + 1].h - p[i].h)*st.dis[1]; } printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++tt, ans); } return 0; }