POJ -- 1151
2024-10-01 07:50:43
Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16222 | Accepted: 6172 |
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 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.
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.
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 思路:首先将横坐标离散化,再对纵坐标排序,然后根据y轴从下往上扫描,每次的高度就是seg[i].y-seg[i-1].y,这就相当于分矩形的宽,然后要做的事就是查询x轴(矩形长)的有效长度,这就要交给线段树了。 AC代码:
/*************************************************************************
> File Name: area.cpp
> Author: wangzhili
> Mail: wangstdio.h@gmail.com
> Created Time: 2014/3/1 星期六 16:06:57
************************************************************************/ #include<iostream>
#include<algorithm>
#include<cstdio>
#define MAX 1000
using namespace std;
class TreeNode
{
public:
int left;
int right;
int mid;
int cover;
int flag;
double length;
};
typedef struct
{
double xl, xr, y;
int flag;
}Line;
TreeNode node[*MAX];
Line seg[MAX];
double x[MAX];
double length;
bool cmp(Line a, Line b)
{
return a.y < b.y;
} void BuildTree(int k, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].mid = (l + r) >> ;
node[k].cover = ;
node[k].flag = ;
if(l + == r)
{
node[k].flag = ;
return ;
}
int mid = (l + r) >> ;
BuildTree(k << , l, mid);
BuildTree(k << |, mid, r);
} void UpdateTree(int k, int l, int r, int flag)
{
if(node[k].left == l && node[k].right == r)
{
node[k].cover += flag;
node[k].length = x[r-] - x[l-];
return ;
}
if(node[k].flag)
return ;
if(node[k].mid <= l)
UpdateTree(k << |, l, r, flag);
else if(node[k].mid >= r)
UpdateTree(k << , l, r, flag);
else
{
UpdateTree(k << , l, node[k].mid, flag);
UpdateTree(k << |, node[k].mid, r, flag);
}
} void GetLength(int k)
{
if(node[k].cover > )
{
length += node[k].length;
return ;
}
if(node[k].flag)
return;
GetLength(k << );
GetLength(k << |);
} int GetIndex(double num, int length)
{
int l, r, mid;
l = , r = length-;
while(l <= r)
{
mid = (l + r) >> ;
if(x[mid] == num)
return mid;
else if(x[mid] > num)
r = mid - ;
else
l = mid + ;
}
} int main(int argc, char const *argv[])
{
int n, i, j, k, cnt;
int xl, xr;
double ans;
double x1, y1, x2, y2;
cnt = ;
BuildTree(, , );
// freopen("in.c", "r", stdin);
while(~scanf("%d", &n) && n)
{
j = ;
ans = .;
for(i = ; i < ; i ++)
{
node[i].cover = ;
}
for(i = ; i < n; i ++)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
seg[j].xl = x1;
seg[j].xr = x2;
seg[j].y = y1;
x[j] = x1;
seg[j ++].flag = ;
seg[j].xl = x1;
seg[j].xr = x2;
seg[j].y = y2;
x[j] = x2;
seg[j ++].flag = -;
}
sort(x, x+j);
sort(seg, seg+j, cmp);
k = ;
for(i = ; i < j; i ++)
{
if(x[i] != x[i-])
x[k ++] = x[i];
}
xl = GetIndex(seg[].xl, k) + ;
xr = GetIndex(seg[].xr, k) + ;
UpdateTree(, xl, xr, seg[].flag);
length = ;
GetLength();
for(i = ; i < j; i ++)
{
ans += (seg[i].y-seg[i-].y)*length;
xl = GetIndex(seg[i].xl, k)+;
xr = GetIndex(seg[i].xr, k)+;
UpdateTree(, xl, xr, seg[i].flag);
length = .;
GetLength();
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++cnt, ans);
}
return ;
}
最新文章
- html中span不显示背景
- nginx 免安装包
- JS_ECMA基本语法中的几种封装的小函数
- ASP.NET上传大文件的问题
- Android 带checkbox的listView 实现多选,全选,反选,删除
- Java发送邮件,所遇到的常见需求
- android 常用selector 颜色搭配(按钮、显示圆角等)
- 关于请求添加HttpRequestHeader
- OFBIZ bug_ControlServlet.java:239:ERROR
- 史上最全maven pom.xml详解
- HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏
- Snort
- 冒泡排序与简单选择排序——Java实现
- 关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类
- 第六章:3D向量类
- App开发 对生命周期的处理
- boto3库限速
- inux中ifreq 结构体分析和使用(转)
- 通过eclipse打开jdk native源码
- Status: Checked in and viewable by authorized users 出现在sharepoint 2013 home 页面