【题目链接】
线段的循环要从1到2n,与面积并不一样,面积并是1到2n-1。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 5005, maxm = maxn << 1; int n, m, tr[maxm << 2], sum[maxm << 2], segnum[maxm << 2], disc[maxm]; bool lseg[maxm << 2], rseg[maxm << 2]; struct _line { int x1, x2, y, c; bool operator < (const _line &l) const { return y != l.y ? y < l.y : c > l.c; } } line[maxm]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline int intabs(int x) { return x < 0 ? -x : x; } inline int find(int x) { int l = 1, r = m; while(l <= r) { int mid = l + r >> 1; if(disc[mid] < x) l = mid + 1; else if(disc[mid] == x) return mid; else r = mid - 1; } } inline void pushup(int p, int l, int r) { if(tr[p]) { sum[p] = disc[r + 1] - disc[l]; lseg[p] = rseg[p] = 1; segnum[p] = 2; } else if(l == r) sum[p] = lseg[p] = rseg[p] = segnum[p] = 0; else { sum[p] = sum[p << 1] + sum[p << 1 | 1]; lseg[p] = lseg[p << 1]; rseg[p] = rseg[p << 1 | 1]; segnum[p] = segnum[p << 1] + segnum[p << 1 | 1]; if(rseg[p << 1] && lseg[p << 1 | 1]) segnum[p] -= 2; } } inline void insert(int p, int l, int r, int x, int y, int c) { if(x == l && r == y) { tr[p] += c; pushup(p, l, r); return; } int mid = l + r >> 1; if(y <= mid) insert(p << 1, l, mid, x, y, c); else if(x > mid) insert(p << 1 | 1, mid + 1, r, x, y, c); else { insert(p << 1, l, mid, x, mid, c); insert(p << 1 | 1, mid + 1, r, mid + 1, y, c); } pushup(p, l, r); } int main() { while(scanf("%d", &n) != EOF) { m = 0; for(int i = 1; i <= n; i++) { int x1 = iread(), y1 = iread(), x2 = iread(), y2 = iread(); m++; line[m] = (_line){x1, x2, y1, 1}; disc[m] = x1; m++; line[m] = (_line){x1, x2, y2, -1}; disc[m] = x2; } sort(disc + 1, disc + 1 + m); sort(line + 1, line + 1 + m); int ans = 0, last = 0; for(int i = 1; i <= m; i++) { int l = find(line[i].x1), r = find(line[i].x2); insert(1, 1, m, l, r - 1, line[i].c); ans += intabs(sum[1] - last) + segnum[1] * (line[i + 1].y - line[i].y); last = sum[1]; } printf("%d\n", ans); } return 0; }