暴力统计覆盖超过一次的区域。1y。
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <set> 7 8 using namespace std; 9 10 typedef pair<double, int> PDBI; 11 multiset<PDBI> pos; 12 #define MPR make_pair 13 #define FI first 14 #define SE second 15 const int N = 1111; 16 double rec[N][4]; 17 18 struct Mark { 19 double x1, x2, y; 20 bool end; 21 Mark() {} 22 Mark(double x1, double x2, double y, bool end) : 23 x1(x1), x2(x2), y(y), end(end) {} 24 bool operator < (Mark x) const { return y < x.y;} 25 } mk[N << 1]; 26 27 int main() { 28 int n, T; 29 cin >> T; 30 while (T-- && cin >> n) { 31 for (int i = 0; i < n; i++) { 32 for (int j = 0; j < 4; j++) { 33 cin >> rec[i][j]; 34 } 35 if (rec[i][0] > rec[i][2]) swap(rec[i][0], rec[i][2]); 36 if (rec[i][1] > rec[i][3]) swap(rec[i][1], rec[i][3]); 37 mk[i << 1] = Mark(rec[i][0], rec[i][2], rec[i][1], false); 38 mk[i << 1 | 1] = Mark(rec[i][0], rec[i][2], rec[i][3], true); 39 } 40 sort(mk, mk + (n << 1)); 41 pos.clear(); 42 if (mk[0].end) { 43 puts("WTF?!!"); 44 while (1) {} 45 } 46 pos.insert(MPR(mk[0].x1, 1)); 47 pos.insert(MPR(mk[0].x2, -1)); 48 multiset<PDBI>::iterator msi; 49 double area = 0.0; 50 for (int i = 1, end = n << 1; i < end; i++) { 51 msi = pos.begin(); 52 int cnt = 0; 53 double last, sum = 0.0; 54 while (msi != pos.end()) { 55 double fi = (*msi).FI; 56 int se = (*msi).SE; 57 if (se > 0) { 58 cnt++; 59 if (cnt == 2) last = fi; 60 } else { 61 cnt--; 62 if (cnt == 1) sum += fi - last; 63 } 64 msi++; 65 } 66 area += sum * (mk[i].y - mk[i - 1].y); 67 if (mk[i].end) { 68 pos.erase(pos.find(MPR(mk[i].x1, 1))); 69 pos.erase(pos.find(MPR(mk[i].x2, -1))); 70 } else { 71 pos.insert(MPR(mk[i].x1, 1)); 72 pos.insert(MPR(mk[i].x2, -1)); 73 } 74 } 75 if (pos.size()) { 76 puts("shit!"); 77 while (1) {} 78 } 79 printf("%.2f ", area); 80 } 81 return 0; 82 }
——written by Lyon