HDU 1542 Atlantis(线段树扫描线,面积并)

题意:

给你 n (n<=100)个矩阵,问你矩阵并后的面积。

解析:

http://www.cnblogs.com/kane0526/archive/2013/02/26/2934214.html
参考了这篇题解报告,终于学会了基本的线段树扫描线。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ls o<<1
#define rs o<<1|1
#define lson ls, L, M
#define rson rs, M+1, R
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 205;
double X[maxn];
int n, m, tot;

struct Line {
    double l, r, h;
    int d;
    Line() {}
    Line(double x1, double x2, double h, int d) : l(x1), r(x2), h(h), d(d) {}
    bool operator < (const Line& rhs) const {
        return h < rhs.h;
    }
} line[maxn];

double sumv[maxn<<2];
int cover[maxn<<2];

inline void pushUp(int o, int L, int R) {
    if(cover[o]) sumv[o] = X[R+1] - X[L];
    else if(L == R) sumv[o] = 0;
    else sumv[o] = sumv[ls] + sumv[rs];
}

void build(int o, int L, int R) {
    sumv[o] = cover[o] = 0;
    if(L == R) return ;
    int M = (L + R)/2;
    build(lson);
    build(rson);
}

void modify(int o, int L, int R, int ql, int qr, int d) {
    if(ql <= L && R <= qr) {
        cover[o] += d;
        pushUp(o, L, R);
        return ;
    }
    int M = (L + R)/2;
    if(ql <= M) modify(lson, ql, qr, d);
    if(qr > M) modify(rson, ql, qr, d);
    pushUp(o, L, R);
}

int main() {
    double x1, y1, x2, y2;
    int cas = 1;
    while(~scanf("%d", &n) && n) {
        m = tot = 0;
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            line[m] = Line(x1, x2, y1, 1);
            X[m++] = x1;
            line[m] = Line(x1, x2, y2, -1);
            X[m++] = x2;
        }

        //discrete
        sort(X, X+m);
        tot = unique(X, X+m) - X;

        sort(line, line+m);
        build(1, 0, tot-1);

        int ql, qr;
        double ans = 0;

        for(int i = 0; i < m; i++) {
            ql = lower_bound(X, X+tot, line[i].l) - X;
            qr = lower_bound(X, X+tot, line[i].r) - X - 1;
            modify(1, 0, tot-1, ql, qr, line[i].d);
            ans += sumv[1] * (line[i+1].h - line[i].h);
        }
        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、付费专栏及课程。

余额充值