HDU1542Atlantis(扫描线)

HDU1542Atlantis(扫描线)

题目链接

题目大意:给你n个覆盖矩形,问最后覆盖的面积。

解题思路:将每个矩形拆成两条线段,一条是+1的,另一条是减1的,然后扫描先从上往下扫描,碰到加1的那条线段,那么这条线段范围内的节点的覆盖信息就+1,直到碰到减1这个线段范围内的节点的覆盖信息都需要减1。这样说可能理解不了,就可以画画矩形然后画下扫描线在理解理解。然后就是需要离散化的建树,因为这里是都double型的。所谓离散化建树的意思就是现在每个叶子节点代表的是一段区间,而不是单独的一点。

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = 205;

#define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1)

struct Node {

    int l, r, add;
    double s;
    void set (int l, int r, double s, int add) {

        this->l = l;
        this->r = r;
        this->s = s;
        this->add = add;
    }
}node[4 * maxn];

struct Line {

    double x, y1, y2;
    int flag;
    Line (double x, double y1, double y2, int flag) {

        this->x = x;
        this->y1 = y1;
        this->y2 = y2;
        this->flag = flag;
    }

    bool operator < (const Line &l) const {

        return x < l.x;   
    }
};

int n;
vector<Line> L;
vector<double> pos;

void pushup(int u) {

    if (node[u].add) 
        node[u].s = pos[node[u].r + 1] - pos[node[u].l];
    else if (node[u].l == node[u].r)
        node[u].s = 0;
    else
        node[u].s = node[lson(u)].s + node[rson(u)].s;
}

void build (int u, int l, int r) {

    node[u].set (l, r, 0, 0);
    if (l == r)
        return;
    int m = (l + r)>>1;
    build(lson(u), l, m);
    build(rson(u), m + 1, r);
}

void update (int u, int l, int r, int v) {

    if (node[u].l >= l && node[u].r <= r) {
        node[u].add += v;
        pushup(u);
        return;
    }

    int m = (node[u].l + node[u].r)>>1;
    if (l <= m)
        update (lson(u), l, r, v);
    if (r > m)
        update (rson(u), l, r, v);
    pushup(u);
}

void init () {

    pos.clear();
    L.clear();
    double x1, y1, x2, y2;

    for (int i = 0; i < n; i++) {
        scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
        pos.push_back(y1);
        pos.push_back(y2);
        L.push_back(Line(x1, y1, y2, 1));
        L.push_back(Line(x2, y1, y2, -1));
    }

    sort (pos.begin(), pos.end());
    sort (L.begin(), L.end());
    pos.erase(unique(pos.begin(), pos.end()), pos.end());

    build(1, 0, (int)pos.size() - 1);
}

double solve() {

    init();
    double ans = 0;
    for (int i = 0; i < (int)L.size() - 1; i++) {

        int l = lower_bound(pos.begin(), pos.end(), L[i].y1) - pos.begin();
        int r = lower_bound(pos.begin(), pos.end(), L[i].y2) - pos.begin();
        update (1, l, r - 1, L[i].flag);
//        printf ("%.2lf\n", node[1].s);
        ans += node[1].s * (L[i + 1].x - L[i].x);    
    }
    return ans;
}

int main () {

    int T = 0;
    double x1, y1, x2, y2;
    while (scanf ("%d", &n) && n) {

        printf ("Test case #%d\n", ++T);
        printf ("Total explored area: %.2lf\n\n", solve());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值