POJ 1151Atlantis 扫描线+线段树求矩形面积并

时间:2023-03-08 21:46:14

题目链接

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
struct segment
{
double l, r, h;
int flag;
segment(){}
segment(double l, double r, double h, int flag):l(l), r(r), h(h), flag(flag){}
bool operator < (segment a) const
{
return h<a.h;
}
}line[maxn];
double a[maxn], sum[maxn<<];
int cover[maxn<<];
void pushUp(int rt, int l, int r) {
if(cover[rt]) {
sum[rt] = a[r+]-a[l];
} else if(l == r) {
sum[rt] = ;
} else {
sum[rt] = sum[rt<<]+sum[rt<<|];
}
}
void update(int L, int R, int l, int r, int rt, int flag) {
if(L<=l&&R>=r) {
cover[rt] += flag;
pushUp(rt, l-, r-);
return ;
}
int m = l+r>>;
if(L<=m)
update(L, R, lson, flag);
if(R>m)
update(L, R, rson, flag);
pushUp(rt, l-, r-);
}
int main()
{
int n, cnt, k = ;
double x1, y1, x2, y2;
while(scanf("%d", &n)&&n) {
cnt = ;
mem(cover);
for(int i = ; i<n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[cnt] = segment(x1, x2, y1, );
a[cnt++] = x1;
line[cnt] = segment(x1, x2, y2, -);
a[cnt++] = x2;
}
sort(a, a+cnt);
sort(line, line+cnt);
double ans = ;
int num = unique(a, a+cnt)-a;
for(int i = ; i<cnt; i++) {
int L = lower_bound(a, a+num, line[i].l)-a+;
int R = lower_bound(a, a+num, line[i].r)-a;
update(L, R, , num, , line[i].flag);
ans += sum[]*(line[i+].h-line[i].h);
}
printf("Test case #%d\n", k++);
printf("Total explored area: %.2f\n\n", ans);
}
return ;
}