您的位置:首页 > 其它

HDU - 1542 Atlantis(线段树扫描线求矩形面积并)

2017-09-25 11:16 549 查看


Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14316    Accepted Submission(s): 5908


Problem Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the
total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

 

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000),
not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

 

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area
(i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

 

Sample Input

2
10 10 20 20
15 15 25 25.5
0

 

Sample Output

Test case #1
Total explored area: 180.00

#include <bits/stdc++.h>
using namespace std;

const int N = 2e4 + 10;
int n, p = 0;
double X[N<<1];
double T[N<<2];
int add[N<<2];
struct xx{
double l, r, y;
int v;
} a[N<<1];

bool cmp(xx a, xx b){
return a.y < b.y;
}

void Pushup(int l, int r, int k){
if(add[k]) T[k] = X[r+1]-X[l];
else T[k] = T[k<<1]+T[k<<1|1];
}

void Update(int l, int r, int L, int R, int v, int k){
if(l <= L && r >= R){
add[k] += v;
Pushup(L, R, k);
return;
}
int mid = (L+R)>>1;
if(l <= mid) Update(l, r, L, mid, v, k<<1);
if(r > mid) Update(l, r,
a754
mid+1, R, v, k<<1|1);
Pushup(L, R, k);
}

int main(){
while(scanf("%d", &n) == 1, n){
memset(T, 0, sizeof(T));
memset(add, 0, sizeof(add));
int tot = 0;
for(int i = 0; i < n; i++){
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
X[tot] = x1;
a[tot].l = x1, a[tot].r = x2, a[tot].y = y1, a[tot++].v = 1;
X[tot] = x2;
a[tot].l = x1, a[tot].r = x2, a[tot].y = y2, a[tot++].v = -1;
}
sort(X, X+tot);
sort(a, a+tot, cmp);
int k = 1;
for(int i = 1; i < tot; i++){
if(X[i] != X[i-1]) X[k++] = X[i];
}
double ans = 0;
for(int i = 0; i < tot-1; i++){
int l = lower_bound(X, X+k, a[i].l)-X;
int r = lower_bound(X, X+k, a[i].r)-X-1;
Update(l, r, 0, k, a[i].v, 1);
ans += T[1]*(a[i+1].y-a[i].y);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n", ++p, ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: