这道题的题意就是给出一些矩形,问这些矩形覆盖的面积,也就是矩形可能是相交的
这道题由于数据量很小,所以可以按照将输入的坐标排序的方法进行分割矩形
如下面的代码
/* ID: sdj22251 PROG: subset LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <map> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 15500 #define INF 1000000000 using namespace std; double a1[122], b1[122], a2[122], b2[122]; double t1[222], t2[222]; int main() { int n, cnt, cas = 0; while(scanf("%d", &n) != EOF && n) { cnt = 0; for(int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &a1[i], &b1[i], &a2[i], &b2[i]); t1[cnt] = a1[i]; t2[cnt] = b1[i]; cnt++; t1[cnt] = a2[i]; t2[cnt] = b2[i]; cnt++; } sort(t1, t1 + cnt); sort(t2, t2 + cnt); double ans = 0; for(int i = 0; i < cnt - 1; i++) { for(int j = 0; j < cnt - 1; j++) { for(int k = 0; k < n; k++) { if(t1[i] >= a1[k] && t2[j] >= b1[k] && t1[i + 1] <= a2[k] && t2[j + 1] <= b2[k]) { ans += (t1[i + 1] - t1[i]) * (t2[j + 1] - t2[j]); break; } } } } printf("Test case #%d\n", ++cas); printf("Total explored area: %.2f\n\n", ans); } return 0; }
矩形切割 根据线段切割的思想,我们稍做推广,便能得出矩形切割的方法。
类似地,若矩形集合中已有矩形(x1,y1,x2,y2),现加入矩形(x3,y3,x4,y4)。它们的位置关系可以有很多种(有17种之多),这里就不一一列举了。但无论它们的位置关系如何复杂,运用线段切割的思想来进行矩形切割,就会变得十分明了。
我们将矩形的切割正交分解,先进行x方向上的切割,再进行y方向的切割。
以下图为例:
插入矩形(x3,y3,x4,y4)后,对矩形(x1,y1,x2,y2)进行切割。
Step 1:首先从x方向上切。把线段(x1,x2)切成(x1,x3),(x4,x2)两条线段。于是相应地,我们就把两个矩形切了出来——(x1,y1,x3,y2),(x4,y1,x2,y2)。把它们加到矩形集合中。去掉了这两个矩形后,我们要切的矩形就变为(x3,y1,x4,y2)。
Step 2:接着我们再进行y方向上的切割。把线段(y1,y2)切成(y1,y3)。相应地又得到一个矩形(x3,y1,x4,y2)。把它放入矩形集合。
Step 3:剩下的矩形为(x3,y3,x4,y2),这个矩形已经被矩形(x3,y3,x4,y4)覆盖了,因此直接把它删掉。
我们可以归纳出矩形切割的思想:
1、先对被切割矩形进行x方向上的切割。取(x1,x2),(x3,x4)的交集(k1,k2)
① 若x1<k1,则加入矩形(x1,y1,k1,y2)
② 若k2<x2,则加入矩形(k2,y1,x2,y2)
2、再对切剩的矩形(k1,y1,k2,y2) 进行y方向上的切割。取(y1,y2),(y3,y4)的交集(k3,k4)
① 若y1<k3,则加入矩形(k1,y1,k2,k3)
② 若k4<y2,则加入矩形(k1,k4,k2,y2)
3、把矩形(x1,y1,x2,y2)从矩形集合中删除。
然后实现代码如下
/* ID: sdj22251 PROG: rect1 LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 2555 #define MAXM 104444 #define INF 100000000 #define eps 1e-7 #define L(X) X<<1 #define R(X) X<<1|1 using namespace std; int n; double xa[1010], xb[1010], ya[1010], yb[1010], area; void cover(double lx, double rx, double ly, double ry, int t) { while((t <= n) && ((lx >= xb[t]) || (rx <= xa[t]) || (ly >= yb[t]) || (ry <= ya[t]))) t++; if(t > n) { area += (rx - lx ) * (ry - ly); return; } if(lx < xa[t]) { cover(lx, xa[t], ly, ry, t + 1 ); lx = xa[t]; } if(rx > xb[t]) { cover(xb[t], rx , ly, ry, t + 1 ); rx = xb[t]; } if(ly < ya[t]) { cover(lx, rx, ly, ya[t], t + 1 ); ly = ya[t]; } if(ry > yb[t]) { cover(lx, rx, yb[t], ry, t + 1 ); ry = yb[t]; } } int main() { int cas = 0; while(scanf("%d", &n) != EOF && n) { area = 0; for(int i = 1; i <= n; i++) scanf("%lf%lf%lf%lf", &xa[i], &ya[i], &xb[i], &yb[i]); for(int i = n; i >= 1; i--) cover(xa[i], xb[i], ya[i], yb[i], i + 1); printf("Test case #%d\n", ++cas); printf("Total explored area: %.2f\n\n", area); } return 0; }
然后放出线段树版本的
/* ID: sdj22251 PROG: subset LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 2222 #define MAXM 164444 #define INF 100000000 #define eps 1e-7 #define L(X) X<<1 #define R(X) X<<1|1 using namespace std; struct node { int left, right, mid; int cnt; double sum; }tree[4 * MAXN]; struct Seg { double h, l, r; int s; Seg(){} Seg(double a, double b, double c, int d) {l = a; r = b; h = c; s = d;} bool operator <(const Seg &cmp)const{ return h < cmp.h; } }seg[MAXN]; double x[MAXN]; void up(int C) { if(tree[C].cnt) tree[C].sum = x[tree[C].right + 1] - x[tree[C].left]; else if(tree[C].left == tree[C].right) tree[C].sum = 0; else tree[C].sum = tree[L(C)].sum + tree[R(C)].sum; } void make_tree(int s, int e, int C) { tree[C].left = s; tree[C].right = e; tree[C].mid = (s + e) >> 1; tree[C].cnt = 0; tree[C].sum = 0; if(s == e) return; make_tree(s, tree[C].mid, L(C)); make_tree(tree[C].mid + 1, e, R(C)); } void update(int s, int e, int v, int C) { if(tree[C].left >= s && tree[C].right <= e) { tree[C].cnt += v; up(C); return; } if(tree[C].mid >= s) update(s, e, v, L(C)); if(tree[C].mid < e) update(s, e, v, R(C)); up(C); } int bin(double v, int bound) { int low = 0, high = bound - 1; while(low <= high) { int mid = (low + high) >> 1; if(x[mid] == v) return mid; if(x[mid] < v) low = mid + 1; else high = mid - 1; } return -1; } int main() { int n, cas = 0; while(scanf("%d", &n) != EOF && n) { int m = 0; double a, b, c, d; while(n--) { scanf("%lf%lf%lf%lf", &a, &b, &c, &d); x[m] = a; seg[m++] = Seg(a, c, b, 1); x[m] = c; seg[m++] = Seg(a, c, d, -1); } sort(x, x + m); sort(seg, seg + m); int k = unique(x, x + m) - x; make_tree(0, k - 1, 1); double ans = 0; for(int i = 0; i < m - 1; i ++) { int l = bin(seg[i].l, k); int r = bin(seg[i].r, k) - 1; //这里-1的原因是建树时是建到叶子结点的,而我们更新的是线段 update(l, r, seg[i].s, 1); ans += tree[1].sum * (seg[i + 1].h - seg[i].h); } printf("Test case #%d\nTotal explored area: %.2f\n\n", ++cas, ans); } return 0; }