1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; const int INF = 1e9; #define int long long
struct Line { int x1, x2, y, op; };
struct SegTree{ int n; vector<int> a, v, w; SegTree(int size) : a(2 * size + 1), v(size * 8 + 1), w(size * 8 + 1) {}
void push_up(int L, int R, int u) { if(v[u]) w[u] = a[R] - a[L]; else if(L + 1 == R) w[u] = 0; else w[u] = w[2 * u] + w[2 * u | 1]; }
void add(int L, int R, int op, int u, int ul, int ur) { if(L <= ul && ur <= R) { v[u] += op; push_up(ul, ur, u); return; } int mid = (ul + ur) / 2; if(L < mid) add(L, R, op, 2 * u, ul, mid); if(R > mid) add(L, R, op, 2 * u | 1, mid, ur); push_up(ul, ur, u); } };
void solve() { int n; cin >> n; vector<Line> b(n * 2 + 1); SegTree t(n); for(int i = 1; i <= n; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; b[i] = {x1, x2, y1, 1}; b[i + n] = {x1, x2, y2, -1}; t.a[i] = x1; t.a[i + n] = x2; }
sort(t.a.begin() + 1, t.a.begin() + 2 * n + 1); t.a.erase(unique(t.a.begin() + 1, t.a.begin() + 2 * n + 1), t.a.end()); int sz = t.a.size() - 1;
auto f = [&](int x) { return lower_bound(t.a.begin() + 1, t.a.begin() + sz + 1, x) - t.a.begin(); };
sort(b.begin() + 1, b.begin() + 2 * n + 1, [&](auto _1, auto _2){ return _1.y < _2.y; });
int ans = 0; for(int i = 1; i <= 2 * n; i++) { int x1 = f(b[i].x1); int x2 = f(b[i].x2); ans += (b[i].y - b[i - 1].y) * t.w[1]; t.add(x1, x2, b[i].op, 1, 1, sz); } cout << ans << "\n"; }
signed main(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
int T = 1; while(T--) solve();
return 0; }
|