spl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Forestedf/spl

:heavy_check_mark: data_structure/union_of_rectangles.hpp

Depends on

Verified with

Code

#pragma once
#include "lazy_segment_tree.hpp"
#include <algorithm>
template <typename T>
struct CountMin {
    using Value = std::pair<int, T>;
    using Func = int;
    static Value id() {
        return Value(10010001001, 0);
    }
    static Value op(const Value &x, const Value &y) {
        int m = std::min(x.first, y.first);
        T c = 0;
        if (x.first == m) {
            c += x.second;
        }
        if (y.first == m) {
            c += y.second;
        }
        return Value(m, c);
    }
    static Func func_id() {
        return 0;
    }
    static Func composite(Func f, Func g) {
        return f + g;
    }
    static Value apply(Func f, const Value &x) {
        return Value(f + x.first, x.second);
    }
};
// (l, r, d, u) -> [l, r) * [d, u)
template <typename T>
T area_of_union_of_rectangles(const std::vector<std::tuple<T, T, T, T>> &rects) {
    if (rects.empty()) {
        return 0;
    }
    std::vector<T> xs, ys;
    xs.reserve(2 * rects.size());
    ys.reserve(2 * rects.size());
    for (const auto &[l, r, d, u] : rects) {
        xs.push_back(l);
        xs.push_back(r);
        ys.push_back(d);
        ys.push_back(u);
    }
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
    std::sort(ys.begin(), ys.end());
    ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
    LazySegmentTree<CountMin<T>> seg((int)xs.size() - 1);
    for (int i = 0; i < (int)xs.size() - 1; ++i) {
        seg.update(i, std::pair<int, T>(0, xs[i + 1] - xs[i]));
    }
    std::vector<std::vector<std::pair<int, int>>> add(ys.size()), sub(ys.size());
    for (const auto &[l, r, d, u] : rects) {
        int l_ = (int)(std::lower_bound(xs.begin(), xs.end(), l) - xs.begin());
        int r_ = (int)(std::lower_bound(xs.begin(), xs.end(), r) - xs.begin());
        int d_ = (int)(std::lower_bound(ys.begin(), ys.end(), d) - ys.begin());
        int u_ = (int)(std::lower_bound(ys.begin(), ys.end(), u) - ys.begin());
        add[d_].emplace_back(l_, r_);
        sub[u_].emplace_back(l_, r_);
    }
    T ans = 0;
    T xlen = xs.back() - xs.front();
    for (int i = 0; i < (int)ys.size() - 1; ++i) {
        for (auto [l, r] : add[i]) {
            seg.apply(l, r, 1);
        }
        for (auto [l, r] : sub[i]) {
            seg.apply(l, r, -1);
        }
        T dy = ys[i + 1] - ys[i];
        std::pair<int, T> p = seg.all_prod();
        if (p.first == 0) {
            ans += (xlen - p.second) * dy;
        } else {
            ans += xlen * dy;
        }
    }
    return ans;
}
#line 2 "data_structure/lazy_segment_tree.hpp"

#include <cassert>
#include <utility>
#include <vector>

template <typename MonoidFunc>
class LazySegmentTree {
public:
    using Value = typename MonoidFunc::Value;
    using Func = typename MonoidFunc::Func;

private:
    int old_length;
    int lg;
    int length;
    std::vector<Value> values;
    std::vector<Func> funcs;

    static int lg2(int n) {
        int x = 1;
        int l = 0;
        while (x < n) {
            x <<= 1;
            ++l;
        }
        return l;
    }

    void _apply(int idx, const Func &func) {
        values[idx] = MonoidFunc::apply(func, values[idx]);
        funcs[idx] = MonoidFunc::composite(func, funcs[idx]);
    }

    void push(int idx) {
        _apply(idx << 1, funcs[idx]);
        _apply(idx << 1 | 1, funcs[idx]);
        funcs[idx] = MonoidFunc::func_id();
    }

    void recalc_values(int idx) {
        values[idx] = MonoidFunc::op(values[idx << 1], values[idx << 1 | 1]);
    }

public:
    LazySegmentTree(int n)
        : old_length(n),
          lg(lg2(n)),
          length(1 << lg),
          values(length << 1, MonoidFunc::id()),
          funcs(length << 1, MonoidFunc::func_id()) {
        assert(n >= 0);
    }

    LazySegmentTree(const std::vector<Value> &v)
        : old_length((int)v.size()),
          lg(lg2(old_length)),
          length(1 << lg),
          values(length << 1, MonoidFunc::id()),
          funcs(length << 1, MonoidFunc::func_id()) {
        for (int i = 0; i < old_length; ++i) {
            values[i + length] = v[i];
        }
        for (int i = length - 1; i > 0; --i) {
            recalc_values(i);
        }
    }

    template <typename F>
    LazySegmentTree(int n, const F &f)
        : old_length(n),
          lg(lg2(n)),
          length(1 << lg),
          values(length << 1, MonoidFunc::id()),
          funcs(length << 1, MonoidFunc::func_id()) {
        for (int i = 0; i < old_length; ++i) {
            values[i + length] = f(i);
        }
        for (int i = length - 1; i > 0; --i) {
            recalc_values(i);
        }
    }

    void update(int idx, Value val) {
        assert(idx >= 0 && idx < old_length);
        idx += length;
        for (int i = lg; i > 0; --i) {
            push(idx >> i);
        }
        values[idx] = std::move(val);
        while (idx >>= 1) {
            recalc_values(idx);
        }
    }

    void apply(int l, int r, const Func &func) {
        assert(l >= 0 && l <= r && r <= old_length);
        if (l == r) {
            return;
        }
        l += length;
        r += length;
        int _l = l;
        int _r = r;
        for (int i = lg; i > 0; --i) {
            push(_l >> i);
            push((_r - 1) >> i);
        }
        while (l < r) {
            if (l & 1) {
                _apply(l++, func);
            }
            if (r & 1) {
                _apply(--r, func);
            }
            l >>= 1;
            r >>= 1;
        }
        for (int i = 1; i <= lg; ++i) {
            if ((_l >> i << i) != _l) {
                recalc_values(_l >> i);
            }
            if ((_r >> i << i) != _r) {
                recalc_values((_r - 1) >> i);
            }
        }
    }

    Value prod(int l, int r) {
        assert(l >= 0 && l <= r && r <= old_length);
        if (l == r) {
            return MonoidFunc::id();
        }
        l += length;
        r += length;
        for (int i = lg; i > 0; --i) {
            push(l >> i);
            push((r - 1) >> i);
        }
        Value lp = MonoidFunc::id();
        Value rp = MonoidFunc::id();
        while (l < r) {
            if (l & 1) {
                lp = MonoidFunc::op(lp, values[l++]);
            }
            if (r & 1) {
                rp = MonoidFunc::op(values[--r], rp);
            }
            l >>= 1;
            r >>= 1;
        }
        return MonoidFunc::op(lp, rp);
    }

    Value all_prod() const { return values[1]; }
};
#line 3 "data_structure/union_of_rectangles.hpp"
#include <algorithm>
template <typename T>
struct CountMin {
    using Value = std::pair<int, T>;
    using Func = int;
    static Value id() {
        return Value(10010001001, 0);
    }
    static Value op(const Value &x, const Value &y) {
        int m = std::min(x.first, y.first);
        T c = 0;
        if (x.first == m) {
            c += x.second;
        }
        if (y.first == m) {
            c += y.second;
        }
        return Value(m, c);
    }
    static Func func_id() {
        return 0;
    }
    static Func composite(Func f, Func g) {
        return f + g;
    }
    static Value apply(Func f, const Value &x) {
        return Value(f + x.first, x.second);
    }
};
// (l, r, d, u) -> [l, r) * [d, u)
template <typename T>
T area_of_union_of_rectangles(const std::vector<std::tuple<T, T, T, T>> &rects) {
    if (rects.empty()) {
        return 0;
    }
    std::vector<T> xs, ys;
    xs.reserve(2 * rects.size());
    ys.reserve(2 * rects.size());
    for (const auto &[l, r, d, u] : rects) {
        xs.push_back(l);
        xs.push_back(r);
        ys.push_back(d);
        ys.push_back(u);
    }
    std::sort(xs.begin(), xs.end());
    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
    std::sort(ys.begin(), ys.end());
    ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
    LazySegmentTree<CountMin<T>> seg((int)xs.size() - 1);
    for (int i = 0; i < (int)xs.size() - 1; ++i) {
        seg.update(i, std::pair<int, T>(0, xs[i + 1] - xs[i]));
    }
    std::vector<std::vector<std::pair<int, int>>> add(ys.size()), sub(ys.size());
    for (const auto &[l, r, d, u] : rects) {
        int l_ = (int)(std::lower_bound(xs.begin(), xs.end(), l) - xs.begin());
        int r_ = (int)(std::lower_bound(xs.begin(), xs.end(), r) - xs.begin());
        int d_ = (int)(std::lower_bound(ys.begin(), ys.end(), d) - ys.begin());
        int u_ = (int)(std::lower_bound(ys.begin(), ys.end(), u) - ys.begin());
        add[d_].emplace_back(l_, r_);
        sub[u_].emplace_back(l_, r_);
    }
    T ans = 0;
    T xlen = xs.back() - xs.front();
    for (int i = 0; i < (int)ys.size() - 1; ++i) {
        for (auto [l, r] : add[i]) {
            seg.apply(l, r, 1);
        }
        for (auto [l, r] : sub[i]) {
            seg.apply(l, r, -1);
        }
        T dy = ys[i + 1] - ys[i];
        std::pair<int, T> p = seg.all_prod();
        if (p.first == 0) {
            ans += (xlen - p.second) * dy;
        } else {
            ans += xlen * dy;
        }
    }
    return ans;
}
Back to top page