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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| const int maxn = 1e4 + 10; int n, m[2];
class A { public: int x, y1, y2, c; A() = default; A(int x, int y1, int y2, int c) : x(x), y1(y1), y2(y2), c(c) {}
bool operator< (const A &rhs) const { return x < rhs.x || (x == rhs.x && c < rhs.c); } } a[2][maxn];
vector<int> B[2]; map<int, int> ny[2];
void prework() { _for(i, 0, 2) { sort(a[i]+1, a[i]+1+m[i]); sort(B[i].begin(), B[i].end()); B[i].erase(unique(B[i].begin(), B[i].end()), B[i].end());
for (auto x : B[i]) ny[i][x] = lower_bound(B[i].begin(), B[i].end(), x) - B[i].begin(); } }
class segTree { public: int l, r; int cnt; bool fl, fr; int cover;
void clear() { l = r = 0; cnt = 0; fl = fr = false; cover = 0; } } tr[maxn * 4];
void build(int p, int l, int r) { tr[p].clear(); tr[p].l = l; tr[p].r = r;
if (l == r) return;
int mid = (l + r) >> 1; build(p<<1, l, mid); build(p<<1 | 1, mid + 1, r); }
void pushup(int p, int sgn) { if (tr[p].cover) { tr[p].cnt = 1; tr[p].fl = tr[p].fr = 1; } else { if (tr[p].l == tr[p].r) { tr[p].fl = tr[p].fr = 0; tr[p].cnt = 0; } else { tr[p].fl = tr[p<<1].fl; tr[p].fr = tr[p<<1 | 1].fr;
tr[p].cnt = tr[p<<1].cnt + tr[p<<1 | 1].cnt; if (tr[p<<1].fr && tr[p<<1 | 1].fl) tr[p].cnt--; } } }
void change(int p, int l, int r, int v, int sgn) { if (l <= tr[p].l && tr[p].r <= r) { tr[p].cover += v; pushup(p, sgn); return; }
int mid = (tr[p].l + tr[p].r) >> 1; if (l <= mid) change(p<<1, l, r, v, sgn); if (r > mid) change(p<<1 | 1, l, r, v, sgn); pushup(p, sgn); }
int solve(int sgn) { build(1, 0, B[sgn].size() - 2); int ans = 0;
_rep(i, 1, m[sgn]) { change(1, ny[sgn][a[sgn][i].y1], ny[sgn][a[sgn][i].y2]-1, a[sgn][i].c, sgn); //debug(ny[a[i].y1]); ans += 2 * tr[1].cnt * (a[sgn][i+1].x - a[sgn][i].x); } return ans; }
int ans = 0; void init() { memset(m, 0, sizeof(m)); _for(i, 0, 2) { B[i].clear(); ny[i].clear(); } ans = 0; }
int main() { freopen("input.txt", "r", stdin); while (~scanf("%d", &n)) { init();
_rep(i, 1, n) { int x1, y1, x2, y2; scanf("%d%d%d%d", &y1, &x1, &y2, &x2); a[0][++m[0]] = A(x1, y1, y2, 1); a[0][++m[0]] = A(x2, y1, y2, -1); B[0].push_back(y1); B[0].push_back(y2);
a[1][++m[1]] = A(y1, x1, x2, 1); a[1][++m[1]] = A(y2, x1, x2, -1); B[1].push_back(x1); B[1].push_back(x2); } assert(m[0] == n*2 && m[1] == n*2);
// prework prework(); _for(i, 0, 2) ans += solve(i); printf("%d\n", ans); } }
|