Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7580 Accepted Submission(s): 3828
Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.
Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据.
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input
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
Sample Output
7.63 0.00
#include
using namespace std;
#define db double
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
const int MAX = 100005;
struct node{
db x, y, h;
int flag;
node() {}
node(db x, db y, db h, int flag) : x(x), y(y), h(h), flag(flag) {}
bool operator < (const node &a) const{
return h < a.h;
}
} e[MAX];
db X[MAX];
db lz[MAX];
db len[MAX], Len[MAX];
void pushup(int rt, int l, int r){
if(lz[rt])
len[rt] = X[r + 1] - X[l];
else if(l == r) len[rt] = 0;
else len[rt] = len[rt << 1] + len[rt << 1 | 1];
if(lz[rt] > 1) Len[rt] = X[r + 1] - X[l];
else if(l == r) Len[rt] = 0;
else if(lz[rt] == 1)
Len[rt] = len[rt << 1] + len[rt << 1 | 1];
else Len[rt] = Len[rt << 1] + Len[rt << 1 | 1];
}
void update(int rt, int l, int r, int x, int y, int v){
if(x <= l && r <= y){
lz[rt] += v;
pushup(rt, l, r);
return;
}
int mid = (l + r) >> 1;
if(x <= mid) update(lson, x, y, v);
if(mid < y) update(rson, x, y, v);
pushup(rt, l, r);
}
int main(){
int N;
scanf("%d", &N);
while(N--){
int n;
memset(X, 0 ,sizeof X);
memset(lz, 0, sizeof lz);
memset(len, 0, sizeof len);
memset(Len, 0, sizeof Len);
int cnt = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++){
db a, b, c, d;
scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
e[cnt] = node(a, c, b, 1);
X[cnt++] = a;
e[cnt] = node(a, c, d, -1);
X[cnt++] = c;
}
sort(X, X + cnt), sort(e, e + cnt);
db ans = 0.0;
int num = unique(X, X + cnt) - X;
for(int i = 0; i < cnt - 1; i++){
int l = lower_bound(X, X + num, e[i].x) - X;
int r = lower_bound(X, X + num, e[i].y) - X - 1;
update(1, 0, num - 1, l, r, e[i].flag);
ans += Len[1] * (e[i + 1].h - e[i].h);
}
printf("%.2f\n", ans);
}
return 0;
}