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;
}


你可能感兴趣的:(POJ 1389 (扫描线))