POJ 1389 (扫描线)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 2222
#define pl c<<1
#define pr (c<<1)|1
#define lson tree[c].l,tree[c].mid,c<<1
#define rson tree[c].mid+1,tree[c].r,(c<<1)|1

int n;
double pos[maxn]; //竖线
struct node {
    int l, r, mid;
    int cover;
    double len;
}tree[maxn<<4];

struct segment {
    double l, r, h;
    int flag;
    bool operator < (segment a) const {
        return h < a.h;
    }
}p[maxn];
double a, b, c, d;
int cnt, m;

void build_tree (int l, int r, int c) {
    tree[c].l = l, tree[c].r = r, tree[c].mid = (l+r)>>1;
    tree[c].cover = 0, tree[c].len = 0;
    if (l == r)
        return ;
    build_tree (lson);
    build_tree (rson);
    return ;
}

void push_up (int c) {
    if (tree[c].cover > 0) {
        tree[c].len = pos[tree[c].r+1] - pos[tree[c].l];
    }
    else if (tree[c].l == tree[c].r)
        tree[c].len = 0;
    else
        tree[c].len = tree[pl].len + tree[pr].len;
}

void update (int l, int r, int c, int x, int y, int v) {
    if (l == x && r == y) {
        tree[c].cover += v;
        push_up (c);
        return ;
    }
    if (tree[c].mid >= y) {
        update (lson, x, y, v);
    }
    else if (tree[c].mid < x) {
        update (rson, x, y, v);
    }
    else {
        update (lson, x, tree[c].mid, v);
        update (rson, tree[c].mid+1, y, v);
    }
    push_up (c);
    return ;
}

void debug (int c) {
    cout << "l:" << tree[c].l << " r:" << tree[c].r << " len:" <<tree[c].len << " cover:" <<tree[c].cover <<endl;
    if (tree[c].l == tree[c].r)
        return ;
    debug (pl);
    debug (pr);
}

int main () {
    //freopen ("in", "r", stdin);
    int kase = 0;
    while (scanf ("%d", &n) == 1 && n) {
        cnt = 0;
        for (int i = 1; i <= n; i++) {
            scanf ("%lf%lf%lf%lf", &a, &b, &c, &d);
            p[cnt].l = a, p[cnt].r = c, p[cnt].h = b, p[cnt].flag = 1, pos[cnt] = a, cnt++;
            p[cnt].l = a, p[cnt].r = c, p[cnt].h = d, p[cnt].flag = -1, pos[cnt] = c, cnt++;
        }
        sort (pos, pos+cnt);
        sort (p, p+cnt);
        m = 0;
        pos[m++] = pos[0];
        for (int i = 1; i < cnt; i++) {
            if (pos[i] != pos[i-1]) {
                pos[m++] = pos[i];
            }
        } 
        build_tree (0, m-1, 1); 
        double ans = 0.0;
        for (int i = 0; i < cnt-1; i++) {
            int l = lower_bound (pos, pos+m, p[i].l) - pos;
            int r = lower_bound (pos, pos+m, p[i].r) - pos - 1;
            update (0, m-1, 1, l, r, p[i].flag);
            ans += tree[1].len * (p[i+1].h - p[i].h); 
        }
        printf ("Test case #%d\n", ++kase);
        printf ("Total explored area: %.2f\n\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值