Area of Simple Polygons POJ - 1389 (扫描线求矩形面积并)

题目链接:https://cn.vjudge.net/problem/POJ-1389

思路跟Atlantis POJ 1151 一样,只是换了一种输入格式

如果扫面线不懂的  可以点击此处看一看详细过程Atlantis

 

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long

using namespace std;

const int maxn = 2000 + 10;
struct node {
    int x, y, h;
    int flag;
} pos[maxn];

int cnt[maxn << 3];
int sum[maxn << 3], X[maxn];

bool cmp(node a, node b) {
    return a.h < b.h;
}
void pushup(int cur, int l, int r) {
    if(cnt[cur])
        sum[cur] = X[r + 1] - X[l];
    else
        sum[cur] = sum[cur<<1] + sum[cur<< 1 | 1];
}

void update(int l, int r, int val, int cur, int cl, int cr) {
    if(l <= cl && cr <= r) {
        cnt[cur] += val;
        pushup(cur, cl, cr);
        return ;
    }

    int mid = cl + cr >> 1;
    if(l <= mid)
        update(l, r, val, cur << 1, cl, mid);
    if(r > mid)
        update(l, r, val, cur << 1 | 1, mid + 1, cr);
    pushup(cur, cl, cr);
}

int main() {
    int n;
    while(1) {

        int xx = 0, hh = 0;
        int x1, y1, x2, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        if(x1 == -1 && x2 == -1 && y1 == -1 && y2 == -1) break;
        memset(cnt, 0, sizeof(cnt));
        memset(sum, 0, sizeof(sum));
        X[++xx] = x1;
        X[++xx] = x2;
        pos[++hh] = node{x1, x2, y1, 1};
        pos[++hh] = node{x1, x2, y2,-1};
        while(scanf("%d %d %d %d", &x1, &y1, &x2, &y2)) {
            if(x1 == -1 && x2 == -1 && y1 == -1 && y2 == -1) break;
            X[++xx] = x1;
            X[++xx] = x2;
            pos[++hh] = node{x1, x2, y1, 1};
            pos[++hh] = node{x1, x2, y2,-1};
        }
        sort(X + 1, X + 1 + xx);
        sort(pos + 1, pos + 1 + hh, cmp);
        xx=unique(X + 1, X + xx + 1) - X - 1;
        int ans = 0.0;
        for(int i = 1; i < hh; i++) {

            int l=lower_bound(X + 1, X + xx + 1, pos[i].x) - X;
            int r=lower_bound(X + 1, X + xx + 1, pos[i].y) - X;
            r--;
            update(l, r, pos[i].flag, 1, 1, xx - 1);
            ans += sum[1] * (pos[i + 1].h - pos[i].h);
        }
        printf("%d\n", ans);
    }
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值