Atlantis 纠结了,啥也不说了,上代码: 枚举: #include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <string> #include <cstdio> #include <climits> #include <queue> #include <map> #include <list> #include <set> #include <vector> using namespace std; double a1[202], a2[202], b1[202], b2[202], x[404], y[404]; int main() { int n, ti = 0; while (scanf("%d", &n), n) { printf("Test case #%d/n", ++ti); printf("Total explored area: "); int index = 0; for (int i=0; i < n; i++) { scanf("%lf%lf%lf%lf", a1+i, b1+i, a2+i, b2+i); x[index] = a1[i]; y[index] = b1[i]; index++; x[index] = a2[i]; y[index] = b2[i]; index++; } sort(x, x+index); sort(y, y+index); double result = 0; for (int i=0; i < index-1; i++) for (int j=0; j < index-1; j++) for (int k=0; k < n; k++) { if ((x[i] >= a1[k]) && (y[j] >= b1[k]) && (x[i+1] <= a2[k]) && (y[j+1] <= b2[k])) { result += (x[i+1]-x[i]) * (y[j+1]-y[j]); break; } } printf("%.2lf/n/n", result); } return 0; } 线段树: #include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <string> #include <cstdio> #include <climits> #include <queue> #include <map> #include <list> #include <set> #include <vector> using namespace std; double a1[202], a2[202], b1[202], b2[202], x[404], y[404]; int tree[1001]; double result, nx, ny; void ins(int l, int r, double a, double b, int pos) { tree[pos]++; if (l+1 == r) { if (tree[pos] == 1) { ny += b - a; } return; } int mid = (l+r)/2; if (b <= y[mid]) ins(l, mid, a, b, pos*2); else if (a >= y[mid]) ins(mid, r, a, b, pos*2+1); else { ins(l, mid, a, y[mid], pos*2); ins(mid, r, y[mid], b, pos*2+1); } } void del(int l, int r, double a, double b, int pos) { tree[pos]--; if (l+1 == r) { if (tree[pos] == 0) { ny -= b - a; } return; } int mid = (l+r)/2; if (b <= y[mid]) del(l, mid, a, b, pos*2); else if (a >= y[mid]) del(mid, r, a, b, pos*2+1); else { del(l, mid, a, y[mid], pos*2); del(mid, r, y[mid], b, pos*2+1); } } int main() { int n, ti = 0; while (scanf("%d", &n) == 1 && n) { printf("Test case #%d/n", ++ti); printf("Total explored area: "); int index = 0; for (int i=0; i < n; i++) { scanf("%lf%lf%lf%lf", a1+i, b1+i, a2+i, b2+i); x[index] = a1[i]; y[index] = b1[i]; index++; x[index] = a2[i]; y[index] = b2[i]; index++; } sort(x, x+index); sort(y, y+index); int ix = 1; for (int i=1; i < index; i++) { if (x[i] != x[i-1]) x[ix++] = x[i]; } int iy = 1; for (int i=1; i < index; i++) { if (y[i] != y[i-1]) y[iy++] = y[i]; } result = 0, nx = 0, ny = 0; memset(tree, 0, sizeof(tree)); //int j1 = 0, j2 = 0; double l = x[0], r; for (int i=1; i < ix; i++) { r = x[i]; for (int j1=0; j1 < n; j1++) { if (a1[j1] == l) ins(0, iy-1, b1[j1], b2[j1], 1); } for (int j2=0; j2< n; j2++) { if (a2[j2] == l) del(0, iy-1, b1[j2], b2[j2], 1); } result += (r-l)*ny; l = r; } printf("%.2lf/n/n", result); } return 0; }