【题目链接】
再记录一个rep数组,表示当前区间至少被覆盖2次的长度。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef double DB; const int maxn = 1005; int n, m, tot, tr[maxn << 4]; DB sum[maxn << 4], rep[maxn << 4], disc[maxn << 1]; struct _line { DB x1, x2, y; int c; bool operator < (const _line &l) const { return y < l.y; } } line[maxn << 1]; inline int find(DB x) { int l = 1, r = tot; while(l <= r) { int mid = l + r >> 1; if(disc[mid] < x) l = mid + 1; else if(disc[mid] == x) return mid; else r = mid - 1; } } inline void pushup(int p, int l, int r) { if(tr[p]) sum[p] = disc[r + 1] - disc[l]; else if(l == r) sum[p] = 0; else sum[p] = sum[p << 1] + sum[p << 1 | 1]; if(tr[p] >= 2) rep[p] = disc[r + 1] - disc[l]; else if(l == r) rep[p] = 0; else if(tr[p] == 1) rep[p] = sum[p << 1] + sum[p << 1 | 1]; else rep[p] = rep[p << 1] + rep[p << 1 | 1]; } inline void insert(int p, int l, int r, int x, int y, int c) { if(x == l && r == y) { tr[p] += c; pushup(p, l, r); return; } int mid = l + r >> 1; if(y <= mid) insert(p << 1, l, mid, x, y, c); else if(x > mid) insert(p << 1 | 1, mid + 1, r, x, y, c); else { insert(p << 1, l, mid, x, mid, c); insert(p << 1 | 1, mid + 1, r, mid + 1, y, c); } pushup(p, l, r); } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); m = n << 1; tot = 0; for(int i = 1; i <= n; i++) { DB x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); tot++; line[tot] = (_line){x1, x2, y1, 1}; disc[tot] = x1; tot++; line[tot] = (_line){x1, x2, y2, -1}; disc[tot] = x2; } sort(disc + 1, disc + 1 + tot); memset(tr, 0, sizeof(tr)); memset(sum, 0, sizeof(sum)); memset(rep, 0, sizeof(rep)); sort(line + 1, line + 1 + m); DB ans = 0; for(int i = 1; i < m; i++) { int l = find(line[i].x1), r = find(line[i].x2); insert(1, 1, tot, l, r - 1, line[i].c); ans += rep[1] * (line[i + 1].y - line[i].y); } printf("%.2f\n", ans); } return 0; }