The first scanned line.
# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <climits> # include <string> # include <iostream> # include <map> # include <cstdlib> # include <list> # include <set> # include <queue> # include <stack> using namespace STD; const int maxn = 4004; double hash [maxn <2]; int color [maxn <2]; double sum [maxn <2]; # define lson L, mid, RT <1 # define rson Mid + 1, R, RT <1 | 1 struct node {int add; d Ouble L; Double R; double H;} node [maxn <2]; int CMP (const node & A, const node & B) {return. h <B. h;} void up (int l, int R, int RT) {If (color [RT]> 0) sum [RT] = hash [R + 1]-Hash [l]; // The length from the L-th line segment to the R-th line segment = the distance between the R + 1 vertex and the L-th point // else if (L = r) sum [RT] = 0; else sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Update (INT l, int R, int add, int L, int R, int RT) {If (L <= L & R <= r) {color [RT] + = add; up (L, R, RT); return;} int mid = (L + r)> 1; if (L <= mid) Update (L, R, add, lson ); if (r> mid) Update (L, R, add, rson); up (L, R, RT);} int Bin (double key, double A [], int Len) // discrete {int L = 0; int r = len-1; while (L <= r) {int mid = (L + r)> 1; if (A [Mid] = Key) return mid; if (a [Mid] <key) L = Mid + 1; else r = mid-1 ;} return-1 ;}int main () {int N; Double A, B, C, D; int Gg = 0; while (CIN> N, N) {int m = 0; For (INT I = 0; I <n; I ++) {CIN> A> B> C> D; hash [m] = A; node [M]. L = A; node [M]. R = C; node [M]. H = B; node [M ++]. add = 1; hash [m] = C; node [M]. L = A; node [M]. R = C; node [M]. H = D; node [M ++]. add =-1;} int K = 0; sort (hash, hash + M); sort (node, node + M, CMP); k ++; for (INT I = 1; I <m; I ++) if (hash [I]! = Hash [I-1]) hash [k ++] = hash [I]; double ret = 0; For (INT I = 0; I <m; I ++) {// After scanning the last line, sum and color are cleared. If you want to manually initialize it, change I <m to I <M-1 int L = Bin (node [I]. l, hash, k); int r = Bin (node [I]. r, hash, k)-1; // point-varying line segments such as 1 (1) 2 (2) 3 (3) 4 Update (L, R, node [I]. add, 0, k-1, 1); RET + = sum [1] * (node [I + 1]. h-node [I]. h);} printf ("Test Case # % d \ n", ++ GG); printf ("total occupied ed area: %. 2f \ n ", RET); cout <Endl;} return 0 ;}
Poj1151atlanta scanning line