From 9896659358bb19549f143e791eadc7a879bfa0bf Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 14 Sep 2022 20:02:52 +0800 Subject: [PATCH] =?UTF-8?q?P3875=20[TJOI2010]=E8=A2=AB=E6=B1=A1=E6=9F=93?= =?UTF-8?q?=E7=9A=84=E6=B2=B3=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/86620351 --- Luogu/P3875/P3875.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 Luogu/P3875/P3875.cpp diff --git a/Luogu/P3875/P3875.cpp b/Luogu/P3875/P3875.cpp new file mode 100644 index 00000000..f8077111 --- /dev/null +++ b/Luogu/P3875/P3875.cpp @@ -0,0 +1,121 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, k; +long long ans; +std::vector ys; + +struct Segment { + int x, y1, y2, k; + + Segment() + : x(0), y1(0), y2(0), k(0) {} + + Segment(int _x, int _y1, int _y2, int _k) + : x(_x), y1(_y1), y2(_y2), k(_k) {} + + bool operator<(const Segment& b) const { + return x < b.x; + } +}; + +struct node { + int l, r; + long long cnt, len; + + node(const int& _l = 0, const int& _r = 0) + : l(_l), r(_r), cnt(0), len(0) {} +} tr[N << 2]; + +inline int find(int y) { + return std::lower_bound(ys.begin(), ys.end(), y) - ys.begin(); +} + +inline void pushup(int u) { + if (tr[u].cnt) { + tr[u].len = ys[tr[u].r + 1] - ys[tr[u].l]; + } else if (tr[u].l != tr[u].r) { + tr[u].len = tr[u << 1].len + tr[u << 1 | 1].len; + } else { + tr[u].len = 0; + } +} + +void build(int u, int l, int r) { + tr[u] = node(l, r); + + if (l == r) return; + + int mid = l + r >> 1; + + build(u << 1, l, mid); + build(u << 1 | 1, mid + 1, r); +} + +void modify(int u, int l, int r, int x) { + if (l <= tr[u].l && tr[u].r <= r) { + tr[u].cnt += x; + } else { + int mid = tr[u].l + tr[u].r >> 1; + if (l <= mid) modify(u << 1, l, r, x); + if (r > mid) modify(u << 1 | 1, l, r, x); + } + + pushup(u); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + std::vector seg; + + for (int i = 0; i < n; i++) { + int x1, y1, x2, y2; + + cin >> x1 >> y1 >> x2 >> y2; + + if (x1 == x2) { + if (y1 > y2) std::swap(y1, y2); + + x1--, x2++; + } else if (y1 == y2) { + if (x1 > x2) std::swap(x1, x2); + + y1--, y2++; + } + + seg.emplace_back(x1, y1, y2, 1); + seg.emplace_back(x2, y1, y2, -1); + + ys.push_back(y1); + ys.push_back(y2); + } + + std::sort(ys.begin(), ys.end()); + ys.erase(std::unique(ys.begin(), ys.end()), ys.end()); + + build(1, 0, ys.size() - 2); + std::sort(seg.begin(), seg.end()); + + for (int i = 0; i < seg.size(); i++) { + if (i > 0) { + ans += tr[1].len * (seg[i].x - seg[i - 1].x); + } + + modify(1, find(seg[i].y1), find(seg[i].y2) - 1, seg[i].k); + } + + cout << ans << endl; + + return 0; +}