Hdu 1542

题目链接

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6645    Accepted Submission(s): 2923


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
2 10 10 20 20 15 15 25 25.5 0

 

Sample Output
Test case #1 Total explored area: 180.00
Accepted Code:
  1 /*************************************************************************
  2     > File Name: G.cpp
  3     > Author: Stomach_ache
  4     > Mail: sudaweitong@gmail.com
  5     > Created Time: 2014年07月26日 星期六 19时05分05秒
  6     > Propose: hdu 1542
  7  ************************************************************************/
  8 #include <cmath>
  9 #include <string>
 10 #include <cstdio>
 11 #include <fstream>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 
 17 const int maxn = 110;
 18 // 保存每条线的信息
 19 struct Line {
 20       // 线段的横坐标和上下的纵坐标
 21       double x, y1, y2;
 22     // 线段被覆盖的次数
 23     int cover;
 24     friend bool operator < (Line a, Line b) {
 25           return a.x < b.x;
 26     }
 27 }line[maxn*2 + 5];
 28 
 29 //线段树的结点信息, 按y轴建树
 30 struct node {
 31       double x, y1, y2;
 32     // flag表示该线段是否为元线段,元线段即离散化之后相邻的两个y轴之间的线段
 33     // cover = 1表示线段是矩形的左边, cover = -1表示矩形的右边
 34     int cover, flag;
 35 }Tree[maxn*1000];
 36 int n;
 37 //保存y轴坐标
 38 double y[maxn*2];
 39 
 40 void build(int root, int L, int R) {
 41       Tree[root].x = -1;
 42       Tree[root].cover = 0;
 43     Tree[root].y1 = y[L];
 44     Tree[root].y2 = y[R];
 45     Tree[root].flag = 0;
 46       if (R - L == 1) {
 47           Tree[root].flag = 1;
 48         return ;
 49     }
 50     int M = (L + R) / 2;
 51     build(root * 2, L, M);
 52     build(root * 2 + 1, M, R);
 53 }
 54 
 55 double insert(int root, Line &ll) {
 56       if (Tree[root].y1 >= ll.y2 || Tree[root].y2 <= ll.y1) {
 57           return 0.0;
 58     }
 59     //走到叶子结点
 60     if (Tree[root].flag) {
 61           //叶子结点已经被覆盖
 62           if (Tree[root].cover > 0) {
 63               // 返回面积
 64             double ans = (ll.x - Tree[root].x) * (Tree[root].y2 - Tree[root].y1);
 65             Tree[root].x = ll.x;
 66               Tree[root].cover += ll.cover;
 67             return ans;
 68         } else {
 69               // 更新叶子结点信息
 70               Tree[rot].cover += ll.cover;
 71             Tree[root].x = ll.x;
 72             return 0.0;
 73         }
 74     }
 75     double ans1 = insert(root * 2, ll);
 76     double ans2 = insert(root * 2 + 1, ll);
 77     return ans1 + ans2;
 78 }
 79 
 80 int main(void) {
 81 #ifndef ONLINE_JUDGE
 82       freopen("in.txt", "r", stdin);
 83 #endif
 84       int cas = 1;
 85       while (~scanf("%d", &n) && n) {
 86           int cnt = 0;
 87           for (int i = 0; i < n; i++) {
 88              double xx, yy, xxx, yyy;
 89             scanf("%lf %lf %lf %lf", &xx, &yy, &xxx, &yyy);
 90             y[++cnt] = yy;
 91             line[cnt].x = xx; line[cnt].y1 = yy; line[cnt].y2 = yyy;
 92             line[cnt].cover = 1;
 93             y[++cnt] = yyy;
 94             line[cnt].x = xxx; line[cnt].y1 = yy; line[cnt].y2 = yyy;
 95             line[cnt].cover = -1;
 96         }
 97         sort(y + 1, y + cnt + 1);
 98         sort(line + 1, line + cnt + 1);
 99         build(1, 1, cnt);
100         double ans = 0.0;
101         for (int i = 1; i <= cnt; i++) {
102               ans += insert(1, line[i]);
103         }
104         printf("Test case #%d\nTotal explored area: %.2f\n\n", cas++, ans);
105     }
106 
107     return 0;
108 }

 

posted on 2014-07-27 18:24  Stomach_ache  阅读(161)  评论(0编辑  收藏  举报

导航