hdu 1542 Atlantis 扫描线 矩形面积并

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20693    Accepted Submission(s): 8258


 

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

题目大意:求矩形的面积并

扫描线,线段树每个节点维护该区间内有多长被大于等于1个边覆盖了,这个题目对于lazy标记不需要pushdown,因为pushdown之后很难维护(当add为-1的时候必须向下继续递归,复杂度会升高),pushdown原本是用于修改操作的顺序会影响结果,如果和顺序无关的话是不需要pushdown的,每个节点的值为执行以当前节点为根的子树内部的所有lazy标记之后的值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e3 + 10;
#define lson o * 2, l, m
#define rson o * 2 + 1, m + 1, r
#define ls o * 2
#define rs o * 2 + 1

double sum[N * 4];
int add[N * 4];
int n;
vector <double> pos; 
struct line {
    double a, b, c;
    int op;
    bool operator<(const line& o) const{
        return c < o.c;
    }
};

vector <line> L; 

void build(int o, int l, int r) {
    sum[o] = add[o] = 0;
    if (l == r) return;
    int m = (l + r) / 2;
    build(lson);
    build(rson);
}

void pushup(int o, int l, int r) {
    if (add[o]) sum[o] = pos[r + 1] - pos[l];
    else if (l == r) sum[o] = 0;
    else sum[o] = sum[ls] + sum[rs];
}

void update(int o, int l, int r, int L, int R, int op) {
    if (L <= l && r <= R) {
        add[o] += op;
        pushup(o, l, r);
        return;
    }
    int m = (l + r) / 2;
    if (L <= m) update(lson, L, R, op);
    if (R > m) update(rson, L, R, op);
    pushup(o, l, r);
}

int main() {
    int cas = 0;
    while (scanf("%d", &n) != EOF) {
        if (n == 0) break;
        cas++;
        pos.clear();
        L.clear();
        double a, b, c, d;
        for (int i = 1; i <= n; i++) {
            scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
            L.push_back({a, c, b, 1});
            L.push_back({a, c, d, -1});
            pos.push_back(a);
            pos.push_back(c);
        }
        sort(L.begin(), L.end());
        sort(pos.begin(), pos.end());
        pos.erase(unique(pos.begin(), pos.end()), pos.end());
        int mx = pos.size() + 5;
        build(1, 0, mx);
        double ans = 0;
        double last = 0;
        for (int i = 0; i < L.size(); i++) {
            ans += sum[1] * (L[i].c - last);
            int x = lower_bound(pos.begin(), pos.end(), L[i].a) - pos.begin();
            int y = lower_bound(pos.begin(), pos.end(), L[i].b) - pos.begin() - 1;
            update(1, 0, mx, x, y, L[i].op);
            last = L[i].c;
        }
        printf("Test case #%d\n", cas);
        printf("Total explored area: %.2lf\n\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值