给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
注意:本题的输入数据较多,推荐使用scanf读入数据.
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
7.630.00
求N<=1000个矩形覆盖至少两次区域的面积,也就是矩形面积交
- 前面的与矩形面积并类似, 不同的是
push_up
的时候要考虑至少覆盖一次
one
和至少覆盖两次
two
的更新
尤其是当前被覆盖了一次的时候, 由于没有 push_down 操作, 父亲节点的信息是没有同步到儿子节点的, 这样的话 push_up 就要考虑了. - 父亲被记录覆盖了一次, 但是如果儿子被覆盖过, 这些操作都是在这个父亲这个大区间上的, 就相当于父亲区间被覆盖了至少两次, 所以 two 和 one 都要更新
//
// Created by TaoSama on 2015-10-04
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 2e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n;
struct Seg {
double l, r, h; int d;
Seg() {}
Seg(double l, double r, double h, double d): l(l), r(r), h(h), d(d) {}
bool operator< (const Seg& rhs) const {
return h < rhs.h;
}
} a[N];
int cnt[N << 2];
double one[N << 2], two[N << 2], all[N];
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
void push_up(int l, int r, int rt) {
if(cnt[rt] >= 2) two[rt] = one[rt] = all[r + 1] - all[l];
else if(cnt[rt] == 1) {
one[rt] = all[r + 1] - all[l];
if(l == r) two[rt] = 0;
else two[rt] = one[rt << 1] + one[rt << 1 | 1];
} else {
if(l == r) one[rt] = two[rt] = 0;
else {
one[rt] = one[rt << 1] + one[rt << 1 | 1];
two[rt] = two[rt << 1] + two[rt << 1 | 1];
}
}
}
void update(int L, int R, int v, int l, int r, int rt) {
if(L <= l && r <= R) {
cnt[rt] += v;
push_up(l, r, rt);
return;
}
int m = l + r >> 1;
if(L <= m) update(L, R, v, lson);
if(R > m) update(L, R, v, rson);
push_up(l, r, rt);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
a[i] = Seg(x1, x2, y1, 1);
a[i + n] = Seg(x1, x2, y2, -1);
all[i] = x1; all[i + n] = x2;
}
n <<= 1;
sort(a + 1, a + 1 + n);
sort(all + 1, all + 1 + n);
int m = unique(all + 1, all + 1 + n) - all - 1;
memset(cnt, 0, sizeof cnt);
memset(one, 0, sizeof one);
memset(two, 0, sizeof two);
double ans = 0;
for(int i = 1; i < n; ++i) {
int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all;
int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all;
if(l < r) update(l, r - 1, a[i].d, 1, m, 1);
ans += two[1] * (a[i + 1].h - a[i].h);
}
printf("%.2f\n", ans);
}
return 0;
}