POJ -- 1151
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 16222 | Accepted: 6172 |
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
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 ;
}
POJ -- 1151的更多相关文章
- 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]
学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...
- POJ 1151 Atlantis(扫描线)
题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS Memory Limit: 10 ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- poj 1151(离散化+矩形面积并)
题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...
- 【POJ 1151】Atlantis
离散化后扫描线扫一遍. 夏令营时gty学长就讲过扫描线,可惜当时too naive,知道现在才写出模板题. 当时也不会线段树啊233 #include<cstdio> #include&l ...
- POJ 1151 Atlantis 线段树+离散化+扫描线
这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...
- [POJ 1151] Atlantis
一样的题:HDU 1542 Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18148 Accepted ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- IOS GCD 的理解
GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...
- 04_XML_02_XML语法
[XML组成] 一个XMl文件分为以下几部分组成 * 文档说明 * 元素 * 属性 * CDATA区.特殊字符 * 处理指令(processing Instruction) [1.文档说明] * 最简 ...
- (hdu)1022 Train Problem I 火车进站问题
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 Problem Description As the new term comes, ...
- MySQL数据库原理
我们知道,数据是信息的载体——一种我们约定了如何解释的符号.在计算机系统中,最常见的应该是文本数据.我们用它记录配置信息,写日志,等等.而在应用程序中,按一定的数据结构来组织数据的方式叫做数据库管理系 ...
- 欢迎使用skymvc框架,简单易用的php框架
skymvc是一款轻量.简单易用的php mvc框架,经过多个项目实践改良. 特点: 1.mvc架构 2.m.v.c之间可以互相调用 3.简单的路由控制 R("/index.php" ...
- xml技术DTD约束定义
XML约束 在XML技术中,可以编写一个文档来约束一个xml文档的书写规范,这称之为XML约束为什么需要XML约束? class.xml <stu><面积>?人怎么会有面积元素 ...
- ∑–△型模数转换器(ADC)简介
∑–△型模数转换器(ADC) 1.概述 近年来,随着超大规模集成电路制造水平的提高,Σ-Δ型模数转换器正以其分辨率高.线性度好.成本低等特点得到越来越广泛的应用.Σ-Δ型模数转换器方案早在20世纪60 ...
- JavaMail 发送邮件
JavaMail邮件发送 引用maven jar包 <dependency> <groupId>javax.mail</groupId> <artifactI ...
- iOS 改变UILabel部分颜色
//协议 UILabel *xieLabel = [[UILabel alloc] init]; xieLabel.textColor = TextGrayColor; xieLabel.font = ...
- Swift2.0新特性
随着刚刚结束的 WWDC 2015 苹果发布了一系列更新,这其中就包括了令人振奋的 Swift 2.0. 这是对之前语言特性的一次大幅的更新,加入了很多实用和方便的元素,下面我们就一起来看看这次更新都 ...