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 80 81 82 83 84 85 86 87
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 5e4 + 50; int n, dtot, xtot, ytot, dis[4 * maxn];
struct node1 { int x_, y1_, y2_, flag_; bool operator<(node1 &x) { if (x_ != x.x_) return x_ < x.x_; return flag_ > x.flag_; } } xl[2 * maxn], yl[2 * maxn];
struct node2 { ll status_, sum_; } seg[4 * 2 * maxn]; #define lson (rt << 1) #define rson (rt << 1 | 1) #define stat(x) seg[x].status_ #define sum(x) seg[x].sum_
void up(int rt, int l, int r) { sum(rt) = stat(rt) ? dis[r + 1] - dis[l] : (l == r ? 0 : sum(lson) + sum(rson)); }
void update(int rt, int l, int r, int L, int R, int v) { if (L <= l && r <= R) { stat(rt) += v, up(rt, l, r); return; } int mid = (l + r) / 2; if (L <= mid) update(lson, l, mid, L, R, v); if (R > mid) update(rson, mid + 1, r, L, R, v); up(rt, l, r); }
ll ywork() { int y1, y2; sort(xl + 1, xl + 1 + xtot); ll pre = 0, ans = 0; for (int i = 1; i <= xtot; ++i) { y1 = lower_bound(dis + 1, dis + 1 + dtot, xl[i].y1_) - dis; y2 = lower_bound(dis + 1, dis + 1 + dtot, xl[i].y2_) - dis; update(1, 1, dtot - 1, y1, y2 - 1, xl[i].flag_); if (sum(1) != pre) ans += abs(sum(1) - pre); pre = sum(1); } memset(seg, 0, sizeof(seg)); return ans; }
ll xwork() { int x1, x2; sort(yl + 1, yl + 1 + ytot); ll pre = 0, ans = 0; for (int i = 1; i <= ytot; ++i) { x1 = lower_bound(dis + 1, dis + 1 + dtot, yl[i].y1_) - dis; x2 = lower_bound(dis + 1, dis + 1 + dtot, yl[i].y2_) - dis; update(1, 1, dtot - 1, x1, x2 - 1, yl[i].flag_); if (sum(1) != pre) ans += abs(sum(1) - pre); pre = sum(1); } return ans; }
int main() { while (cin >> n) { int x1, y1, x2, y2; xtot = ytot = dtot = 0, memset(seg, 0, sizeof(seg)); for (int i = 1; i <= n; ++i) { cin >> x1 >> y1 >> x2 >> y2; xl[++xtot] = {x1, y1, y2, 1}, xl[++xtot] = {x2, y1, y2, -1}; yl[++ytot] = {y1, x1, x2, 1}, yl[++ytot] = {y2, x1, x2, -1}; dis[++dtot] = x1, dis[++dtot] = y1, dis[++dtot] = x2, dis[++dtot] = y2; } sort(dis + 1, dis + 1 + dtot); dtot = unique(dis + 1, dis + 1 + dtot) - 1 - dis; cout << ywork() + xwork() << endl; } return 0; }
|