AtlantisTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9299 Accepted Submission(s): 3960
Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.
Output a blank line after each test case.
Sample Input
Sample Output
|
题意:求n个矩阵的面积并。
hdoj 1542 && poj 1151 ——AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define MAXN 200 using namespace std; struct Tree { int l, r; double sum;//区间被覆盖长度 int cover;//是否被覆盖 为1被覆盖 为0没有被覆盖 }; Tree tree[MAXN<<2]; struct Node { double x1, x2, y; int cover; }; Node node[MAXN]; bool cmp(Node a, Node b) { return a.y < b.y; } void build(int o, int l, int r) { tree[o].l = l, tree[o].r = r; tree[o].sum = tree[o].cover = 0; if(l == r) return ; int mid = (l + r) >> 1; build(lson); build(rson); } double rec[MAXN]; void PushUp(int o) { if(tree[o].cover)//被覆盖 tree[o].sum = rec[tree[o].r+1] - rec[tree[o].l]; else//没有被覆盖 { if(tree[o].l == tree[o].r)//节点 没有长度 tree[o].sum = 0; else tree[o].sum = tree[ll].sum + tree[rr].sum; } } void update(int L, int R, int o, int v) { if(L <= tree[o].l && R >= tree[o].r) { tree[o].cover += v; PushUp(o); return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(R <= mid) update(L, R, ll, v); else if(L > mid) update(L, R, rr, v); else { update(L, mid, ll, v); update(mid+1, R, rr, v); } PushUp(o); } int Find(int l, int r, double val) { while(r >= l) { int mid = (l + r) >> 1; if(rec[mid] == val) return mid; else if(rec[mid] > val) r = mid - 1; else l = mid + 1; } return -1; } int main() { int p = 1; int n; while(scanf("%d", &n), n) { int k = 1; double x1, y1, x2, y2; for(int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); node[k].x1 = x1; node[k].x2 = x2; node[k].y = y1; node[k].cover = 1; rec[k++] = x1; node[k].x1 = x1; node[k].x2 = x2; node[k].y = y2; node[k].cover = -1; rec[k++] = x2; } sort(node+1, node+k, cmp); //离散化 去重 sort(rec+1, rec+k); int R = 2; for(int i = 2; i < k; i++) if(rec[i] != rec[i-1]) rec[R++] = rec[i]; sort(rec+1, rec+R); build(1, 1, R-1);//建树 double ans = 0; for(int i = 1; i < k-1; i++) { int x = Find(1, R-1, node[i].x1); int y = Find(1, R-1, node[i].x2); if(x <= y-1) update(x, y-1, 1, node[i].cover); ans += tree[1].sum * (node[i+1].y - node[i].y); } printf("Test case #%d\n", p++); printf("Total explored area: %.2lf\n\n", ans); } return 0; }
poj 1389
poj 1389 —— AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define MAXN 2000+10 using namespace std; struct Tree { int l, r; int sum;//区间被覆盖长度 int cover;//是否被覆盖 为1被覆盖 为0没有被覆盖 }; Tree tree[MAXN<<2]; struct Node { int x1, x2, y; int cover; }; Node node[MAXN]; bool cmp(Node a, Node b) { return a.y < b.y; } void build(int o, int l, int r) { tree[o].l = l, tree[o].r = r; tree[o].sum = tree[o].cover = 0; if(l == r) return ; int mid = (l + r) >> 1; build(lson); build(rson); } int rec[MAXN]; void PushUp(int o) { if(tree[o].cover)//被覆盖 tree[o].sum = rec[tree[o].r+1] - rec[tree[o].l]; else//没有被覆盖 { if(tree[o].l == tree[o].r)//节点 没有长度 tree[o].sum = 0; else tree[o].sum = tree[ll].sum + tree[rr].sum; } } void update(int L, int R, int o, int v) { if(L <= tree[o].l && R >= tree[o].r) { tree[o].cover += v; PushUp(o); return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(R <= mid) update(L, R, ll, v); else if(L > mid) update(L, R, rr, v); else { update(L, mid, ll, v); update(mid+1, R, rr, v); } PushUp(o); } int Find(int l, int r, int val) { while(r >= l) { int mid = (l + r) >> 1; if(rec[mid] == val) return mid; else if(rec[mid] > val) r = mid - 1; else l = mid + 1; } return -1; } int main() { int x1, y1, x2, y2; while(scanf("%d%d%d%d", &x1, &y1, &x2, &y2) != EOF) { if(x1 == -1 || y1 == -1 || x2 == -1 || y2 == -1) return 0; int k = 1; node[k].x1 = x1; node[k].x2 = x2; node[k].y = y1; node[k].cover = 1; rec[k++] = x1; node[k].x1 = x1; node[k].x2 = x2; node[k].y = y2; node[k].cover = -1; rec[k++] = x2; while(scanf("%d%d%d%d", &x1, &y1, &x2, &y2), x1 != -1 || y1 != -1 || x2 != -1 || y2 != -1) { node[k].x1 = x1; node[k].x2 = x2; node[k].y = y1; node[k].cover = 1; rec[k++] = x1; node[k].x1 = x1; node[k].x2 = x2; node[k].y = y2; node[k].cover = -1; rec[k++] = x2; } sort(node+1, node+k, cmp); //离散化 去重 sort(rec+1, rec+k); int R = 2; for(int i = 2; i < k; i++) if(rec[i] != rec[i-1]) rec[R++] = rec[i]; sort(rec+1, rec+R); build(1, 1, R-1);//建树 int ans = 0; for(int i = 1; i < k-1; i++) { int x = Find(1, R-1, node[i].x1); int y = Find(1, R-1, node[i].x2); if(x <= y-1) update(x, y-1, 1, node[i].cover); ans += tree[1].sum * (node[i+1].y - node[i].y); } printf("%d\n", ans); } return 0; }