1. 程式人生 > >矩形面積並、矩形面積交、矩形周長並(線段樹、掃描線總結)(轉載)

矩形面積並、矩形面積交、矩形周長並(線段樹、掃描線總結)(轉載)

{} 沒有 bound 但是 log class ani 操作 iomanip

HDU 1542 [POJ 1151] Atlantis (矩形面積並)

  • 題意:

    N<=100

  • 分析:

    • 離散化: 這些技巧都是老生常談的了, 不然浮點數怎麽建樹, 離散化x坐標就可以了
    • 掃描線: 首先把矩形按y軸分成兩條邊, 上邊和下邊, 對x軸建樹, 掃描線可以看成一根平行於x軸的直線.
      y=0開始往上掃, 下邊表示要計算面積+1, 上邊表示已經掃過了?1, 直到掃到最後一條平行於x軸的邊
      但是真正在做的時候, 不需要完全模擬這個過程, 一條一條邊地插入線段樹就好了
    • 線段樹: 用於動態維護掃描線在往上走時, x軸哪些區域是有合法面積的
    • ps:這種線段樹是不用lazy
      的, 因為不用push_down, 為啥不用push_down, 因為沒有查詢操作
  • 掃描線掃描的過程(建議配合代碼模擬)

    ps:,,
    以下圖轉載自@kk303的博客

技術分享圖片

初始狀態

技術分享圖片

掃到最下邊的線, 點13更新為1

技術分享圖片

掃到第二根線, 此時S=lcnt!=0?h, 得到綠色的面積, 加到答案中去, 隨後更新計數

技術分享圖片

同上, 將黃色的面積加到答案中去

技術分享圖片

同上, 將灰色的面積加到答案中去

技術分享圖片

同上, 將紫色的面積加到答案中去

技術分享圖片

同上, 將藍色的面積加到答案中去

  • 代碼
//
//  Created by TaoSama on 2015-07-14
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 205, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n;
struct Seg {
    double l, r, h; int d;
    Seg() {}
    Seg(double l, double r, double h, int d): l(l), r(r), h(h), d(d) {}
    bool operator< (const Seg& rhs) const {return h < rhs.h;}
} a[N];

int cnt[N << 2]; //根節點維護的是[l, r+1]的區間
double sum[N << 2], all[N];

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

void push_up(int l, int r, int rt) {
    if(cnt[rt]) sum[rt] = all[r + 1] - all[l];
    else if(l == r) sum[rt] = 0; //leaves have no sons
    else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void update(int L, int R, int v, int l, int r, int rt) {
    if(L <= l && r <= R) {
        cnt[rt] += v;
        push_up(l, r, rt);
        return;
    }
    int m = l + r >> 1;
    if(L <= m) update(L, R, v, lson);
    if(R > m) update(L, R, v, rson);
    push_up(l, r, rt);
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int kase = 0;
    while(scanf("%d", &n) == 1 && n) {
        for(int i = 1; i <= n; ++i) {
            double x1, y1, x2, y2;
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            a[i] = Seg(x1, x2, y1, 1);
            a[i + n] = Seg(x1, x2, y2, -1);
            all[i] = x1; all[i + n] = x2;
        }
        n <<= 1;
        sort(a + 1, a + 1 + n);
        sort(all + 1, all + 1 + n);
        int m = unique(all + 1, all + 1 + n) - all - 1;

        memset(cnt, 0, sizeof cnt);
        memset(sum, 0, sizeof sum);

        double ans = 0;
        for(int i = 1; i < n; ++i) {
            int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all;
            int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all;
            if(l < r) update(l, r - 1, a[i].d, 1, m, 1);
            ans += sum[1] * (a[i + 1].h - a[i].h);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n", ++kase, ans);
    }
    return 0;
}

矩形面積並、矩形面積交、矩形周長並(線段樹、掃描線總結)(轉載)