spl

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

View the Project on GitHub Forestedf/spl

:heavy_check_mark: data_structure/test/area_of_union_of_rectangles.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/area_of_union_of_rectangles"
#define FAST_IO

#include "../../data_structure/union_of_rectangles.hpp"
#include "../../template/template.hpp"

int main() {
    i32 n;
    cin >> n;
    V<tuple<i64, i64, i64, i64>> rects(n);
    for (auto &[l, r, d, u] : rects) {
        cin >> l >> d >> r >> u;
    }
    cout << area_of_union_of_rectangles(rects) << '\n';
}
#line 1 "data_structure/test/area_of_union_of_rectangles.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/area_of_union_of_rectangles"
#define FAST_IO

#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;
}
#line 2 "template/template.hpp"
#include <bits/stdc++.h>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, m, n) for (i32 i = (i32)(n)-1; i >= (i32)(m); --i)
#define PER(...) OVERRIDE(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define ALL(x) begin(x), end(x)
#define LEN(x) (i32)(x.size())
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using i32 = signed int;
using i64 = signed long long;
using f64 = double;
using f80 = long double;
using pi = pair<i32, i32>;
using pl = pair<i64, i64>;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<V<V<T>>>;
template <typename T>
using VVVV = V<V<V<V<T>>>>;
template <typename T>
using PQR = priority_queue<T, V<T>, greater<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
i32 lob(const V<T> &arr, const T &v) {
    return (i32)(lower_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
i32 upb(const V<T> &arr, const T &v) {
    return (i32)(upper_bound(ALL(arr), v) - arr.begin());
}
template <typename T>
V<i32> argsort(const V<T> &arr) {
    V<i32> ret(arr.size());
    iota(ALL(ret), 0);
    sort(ALL(ret), [&](i32 i, i32 j) -> bool {
        if (arr[i] == arr[j]) {
            return i < j;
        } else {
            return arr[i] < arr[j];
        }
    });
    return ret;
}
#ifdef INT128
using u128 = __uint128_t;
using i128 = __int128_t;
#endif
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
void scan(char &x) { cin >> x; }
void scan(u32 &x) { cin >> x; }
void scan(u64 &x) { cin >> x; }
void scan(i32 &x) { cin >> x; }
void scan(i64 &x) { cin >> x; }
void scan(f64 &x) { cin >> x; }
void scan(string &x) { cin >> x; }
template <typename T>
void scan(V<T> &x) {
    for (T &ele : x) {
        scan(ele);
    }
}
void read() {}
template <typename Head, typename... Tail>
void read(Head &head, Tail &...tail) {
    scan(head);
    read(tail...);
}
#define CHAR(...)     \
    char __VA_ARGS__; \
    read(__VA_ARGS__);
#define U32(...)     \
    u32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define U64(...)     \
    u64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I32(...)     \
    i32 __VA_ARGS__; \
    read(__VA_ARGS__);
#define I64(...)     \
    i64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define F64(...)     \
    f64 __VA_ARGS__; \
    read(__VA_ARGS__);
#define STR(...)        \
    string __VA_ARGS__; \
    read(__VA_ARGS__);
#define VEC(type, name, size) \
    V<type> name(size);       \
    read(name);
#define VVEC(type, name, size1, size2)    \
    VV<type> name(size1, V<type>(size2)); \
    read(name);
#line 6 "data_structure/test/area_of_union_of_rectangles.test.cpp"

int main() {
    i32 n;
    cin >> n;
    V<tuple<i64, i64, i64, i64>> rects(n);
    for (auto &[l, r, d, u] : rects) {
        cin >> l >> d >> r >> u;
    }
    cout << area_of_union_of_rectangles(rects) << '\n';
}
Back to top page