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
| #include <cstdio> #include <algorithm> using namespace std; const int N = 1e5 + 10; typedef long long LL; int n, len, d[N << 1]; int get(int x) { return lower_bound(d + 1, d + 1 + len, x) - d; } struct Line { int x, y1, y2, op; bool operator<(const Line &_) const { return x < _.x; } } e[N << 1]; struct Node { int l, r, cnt, len; } tr[N << 3]; void build(int x, int l, int r) { tr[x].l = l, tr[x].r = r; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); } void modify(int x, int l, int r, int k) { if (tr[x].l >= l && tr[x].r <= r) { tr[x].cnt += k; if (tr[x].cnt > 0) tr[x].len = d[tr[x].r + 1] - d[tr[x].l]; else tr[x].len = (tr[x].l == tr[x].r ? 0 : tr[x << 1].len + tr[x << 1 | 1].len); return; } int mid = (tr[x].l + tr[x].r) >> 1; if (l <= mid) modify(x << 1, l, r, k); if (r > mid) modify(x << 1 | 1, l, r, k); tr[x].len = (tr[x].cnt > 0 ? d[tr[x].r + 1] - d[tr[x].l] : tr[x << 1].len + tr[x << 1 | 1].len); } int main() { scanf("%d", &n); len = 0; for (int i = 1; i <= n; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); d[++len] = y1, d[++len] = y2; e[i] = (Line){x1, y1, y2, 1}; e[i + n] = (Line){x2, y1, y2, -1}; } sort(d + 1, d + len + 1); len = unique(d + 1, d + len + 1) - (d + 1); sort(e + 1, e + 2 * n + 1); build(1, 1, len); LL ans = 0; for (int i = 1; i <= 2 * n; i++) { modify(1, get(e[i].y1), get(e[i].y2) - 1, e[i].op); if (i < 2 * n && e[i].x != e[i + 1].x) ans += (LL)(e[i + 1].x - e[i].x) * tr[1].len; } printf("%lld", ans); return 0; }
|