POJ 1389 (扫描线)

第一题扫描线,连离散化都不用0.0

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 51111
#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

struct node {
    int l, r, mid;
    int len;
    int cover;
}tree[maxn<<4];

struct segment {
    int l, r, h, flag;
    bool operator < (segment a) const {
        return h < a.h;
    }
}p[2222];

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

void push_up (int c) {
    if (tree[c].cover) {
        tree[c].len = (tree[c].r - tree[c].l+1);
    }
    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 (x == l && y == r) {
        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);
}

int a, b, c, d;
int cnt;

void add () {
    c--;
    p[cnt].l = a, p[cnt].r = c, p[cnt].h = b, p[cnt].flag = 1;
    cnt++;
    p[cnt].l = a, p[cnt].r = c, p[cnt].h = d, p[cnt].flag = -1;
    cnt++;
}

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d%d%d%d", &a, &b, &c, &d) == 4 && a+b+c+d != -4) {
        cnt = 0;
        add ();
        while (scanf ("%d%d%d%d", &a, &b, &c, &d) == 4 && a+b+c+d != -4) {
            add ();
        }
        sort (p, p+cnt);
        build_tree (0, 50000, 1);
        long long ans = 0;
        for (int i = 0; i < cnt-1; i++) {
            update (0, 50000, 1, p[i].l, p[i].r, p[i].flag);
            ans += (p[i+1].h - p[i].h) * (long long)tree[1].len;
        }
        printf ("%lld\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值