ACM_Notebook_new

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

View the Project on GitHub ngthanhtrung23/ACM_Notebook_new

:heavy_check_mark: DataStructure/test/aizu_dsl_4_a_range_set.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_4_A"

#include "../../template.h"
#include "../RangeSet.h"

void solve() {
    int n; std::cin >> n;
    std::set<int> xs;
    std::vector<std::tuple<int,int,int,int>> rects(n);
    for (auto& [x1, y1, x2, y2] : rects) {
        std::cin >> x1 >> y1 >> x2 >> y2;
        xs.insert(x1);
        xs.insert(x2);
    }

    long long res = 0;
    for (auto it = std::next(xs.begin()); it != xs.end(); ++it) {
        int left = *std::prev(it);
        int right = *it;

        RangeSet<int> rs;
        for (auto [x1, y1, x2, y2] : rects) {
            if (x1 <= left && x2 >= right) {
                rs.insert(y1, y2 - 1);
            }
        }
        res += (right - left) * (long long) rs.n_elements();
    }
    std::cout << res << endl;
}
#line 1 "DataStructure/test/aizu_dsl_4_a_range_set.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_4_A"

#line 1 "template.h"
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }

#define sqr(x) ((x) * (x))

// For printing pair, container, etc.
// Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html
template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) {
    return out << '(' << p.first << ", " << p.second << ')';
}

template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator << (ostream& out, const Con& con) {
    out << '{';
    for (auto beg = con.begin(), it = beg; it != con.end(); it++) {
        out << (it == beg ? "" : ", ") << *it;
    }
    return out << '}';
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
    if constexpr(i == tuple_size<T>::value) return out << ")"; 
    else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); 
}
template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) {
    return print_tuple_utils<0, tuple<U...>>(out, t);
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long get_rand(long long r) {
    return uniform_int_distribution<long long> (0, r-1)(rng);
}

template<typename T>
vector<T> read_vector(int n) {
    vector<T> res(n);
    for (int& x : res) cin >> x;
    return res;
}

void solve();

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}
#line 1 "DataStructure/RangeSet.h"
// RangeSet - for maintaining set of segments {{{
//
// merge_adjacent_segment = true -> merge 2 consecutive segments,
// e.g. [1, 10] and [11, 20] --> [1, 20]
//
// Based on https://suisen-cp.github.io/cp-library-cpp/library/datastructure/util/range_set.hpp
template<typename T, bool merge_adjacent_segment = true>
struct RangeSet {
    T n_elements() const { return sz; }
    T n_ranges() const { return ranges.size(); }

    bool contains(T x) const {
        auto it = ranges.upper_bound(x);
        return it != ranges.begin() && x <= std::prev(it)->second;
    }

    // Find range containing x, i.e. l <= x <= r
    auto find_range(T x) const {
        auto it = ranges.upper_bound(x);
        return it != ranges.begin() && x <= prev(it)->second ? prev(it) : ranges.end();
    }

    // Insert [l, r]
    // Returns number of new integers added.
    // AMORTIZED O(logN)
    T insert(T l, T r) {
        assert(l <= r);
        auto it = ranges.upper_bound(l);
        if (it != ranges.begin() && is_mergeable(std::prev(it)->second, l)) {
            it = std::prev(it);
            l = std::min(l, it->first);
        }
        T inserted = 0;
        for (; it != ranges.end() && is_mergeable(r, it->first); it = ranges.erase(it)) {
            auto [cl, cr] = *it;
            r = std::max(r, cr);
            inserted -= cr - cl + 1;
        }

        inserted += r - l + 1;
        ranges[l] = r;
        sz += inserted;
        return inserted;
    }

    // Erase [l, r]
    // Returns number of integers removed
    // AMORTIZED O(logN)
    T erase(T l, T r) {
        assert(l <= r);
        T tl = l, tr = r;
        auto it = ranges.upper_bound(l);
        if (it != ranges.begin() && l <= std::prev(it)->second) {
            it = std::prev(it);
            tl = it->first;
        }

        T erased = 0;
        for (; it != ranges.end() && it->first <= r; it = ranges.erase(it)) {
            auto [cl, cr] = *it;
            tr = cr;
            erased += cr - cl + 1;
        }
        if (tl < l) {
            ranges[tl] = l-1;
            erased -= l - tl;
        }
        if (r < tr) {
            ranges[r + 1] = tr;
            erased -= tr - r;
        }
        sz -= erased;
        return erased;
    }

    // Find min x: x >= lower && x NOT in this set
    T minimum_excluded(T lower) const {
        static_assert(merge_adjacent_segment);
        auto it = find_range(lower);
        return it == ranges.end() ? lower : it->second + 1;
    }

    // Find max x: x <= upper && x NOT in this set
    T maximum_excluded(T upper) const {
        static_assert(merge_adjacent_segment);
        auto it = find_range(upper);
        return it == ranges.end() ? upper : it->first - 1;
    }

    T sz = 0;
    std::map<T, T> ranges;

    bool is_mergeable(T cur_r, T next_l) {
        return next_l <= cur_r + merge_adjacent_segment;
    }
};
// }}}
#line 5 "DataStructure/test/aizu_dsl_4_a_range_set.test.cpp"

void solve() {
    int n; std::cin >> n;
    std::set<int> xs;
    std::vector<std::tuple<int,int,int,int>> rects(n);
    for (auto& [x1, y1, x2, y2] : rects) {
        std::cin >> x1 >> y1 >> x2 >> y2;
        xs.insert(x1);
        xs.insert(x2);
    }

    long long res = 0;
    for (auto it = std::next(xs.begin()); it != xs.end(); ++it) {
        int left = *std::prev(it);
        int right = *it;

        RangeSet<int> rs;
        for (auto [x1, y1, x2, y2] : rects) {
            if (x1 <= left && x2 >= right) {
                rs.insert(y1, y2 - 1);
            }
        }
        res += (right - left) * (long long) rs.n_elements();
    }
    std::cout << res << endl;
}
Back to top page