HDU 1542 Atlantis(矩形面积并)
HDU 1542 Atlantis
题意:给定一些矩形,求面积并
思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了。假设数据大。就要用线段树
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int N = 205;
const int M = 100005;
const double eps = 1e-8; int n, vis[N], hn;
double hash[N]; struct Line {
double l, r, y;
int flag;
Line() {}
Line(double l, double r, double y, int flag) {
this->l = l;
this->r = r;
this->y = y;
this->flag = flag;
}
} line[N]; bool cmp(Line a, Line b) {
return a.y < b.y;
} int get(double x) {
return lower_bound(hash, hash + hn, x) - hash;
} int main() {
int cas = 0;
while (~scanf("%d", &n) && n) {
double x1, x2, y1, y2; hn = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[i * 2] = Line(x1, x2, y1, 1);
line[i * 2 + 1] = Line(x1, x2, y2, -1);
hash[hn++] = x1; hash[hn++] = x2;
}
n *= 2;
sort(line, line + n, cmp);
sort(hash, hash + hn);
hn = 1;
for (int i = 1; i < n; i++) {
if (fabs(hash[i] - hash[i - 1]) < eps) continue;
hash[hn++] = hash[i];
}
double ans = 0;
for (int i = 0; i < n; i++) {
int l = get(line[i].l), r = get(line[i].r);
double len = 0;
for (int j = 0; j < hn - 1; j++) if (vis[j] > 0) len += (hash[j + 1] - hash[j]);
if (i) ans += len * (line[i].y - line[i - 1].y);
for (int j = l; j < r; j++) vis[j] += line[i].flag;
}
printf("Test case #%d\n", ++cas);
printf("Total explored area: %.2lf\n\n", ans);
}
return 0;
}
HDU 1542 Atlantis(矩形面积并)的更多相关文章
- (HDU 1542) Atlantis 矩形面积并——扫描线
n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 1542 Atlantis(扫描线)题解
题意:给n个可能相交的矩形,问你不重复的总面积 思路:扫描线,一边扫一边加. 扫描线:图片来源:理解扫描线 假设我们要算以下四个矩形面积,显然中间深色的是重复的.我们按照x的大小,从左往右扫,然后用线 ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
随机推荐
- Run Nutch In Eclipse on Linux and Windows nutch version 0.9
Running Nutch in Eclipse Here are instructions for setting up a development environment for Nutch un ...
- jQuery中focusin()和focus()、find()和children()的差别
jQuery中focus()和focusin().focus()和children()的差别 focus()和focusin() focus()和focusin()的差别在于focusin()支持事件 ...
- Spring中@Transactional事务回滚(含实例具体解说,附源代码)
一.使用场景举例 在了解@Transactional怎么用之前我们必须要先知道@Transactional有什么用. 以下举个栗子:比方一个部门里面有非常多成员,这两者分别保存在部门表和成员表里面,在 ...
- Linux网络编程(附1)——封装read、write
原打算实践简单的模型的时候,主要专注于基本的模型,先用UNIX I/O糊弄下,可是未封装的read和write用起来实在心累,还是直接用前辈们已经实现好的高级版本号read.write. UNIX I ...
- kibana智能检索发送多次_msearch —— 配置index pattern,同时设置时间段,就知道到底是在哪些索引里去查找数据了
kibanasite/elasticsearch/log-*/_field_stats?level=indices 返回: {"_shards":{"total ...
- 机器翻译引擎的基本原理 ——LSTM
机器翻译引擎的基本原理 摘自:infoq 谷歌机器翻译 Zero-shot:零次 Training:训练 Google Neural Machine Translation:谷歌神经机器翻译 我们每 ...
- (五)api网关服务 zuul-路由
路由是微服务架构中必须的一部分,比如,“/” 可能映射到你的WEB程序上,”/api/users “可能映射到你的用户服务上,“/api/shop”可能映射到你的商品服务商.(注解:我理解这里的这几个 ...
- Kettle学习系列之数据仓库、数据整合、ETL、ELT和EII之间的区别?
不多说,直接上干货! 在数据仓库领域里,的一个重要概念就是数据整合(data intergration).数据整合它就是把不同数据库中的数据整合到一起,对外提供统一的数据视图. 数据整合最典型的案例就 ...
- 001.ActiveMQ概述
1. 概念 ActiveMQ是Apache推出的,一款开源的,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现的消息中间件(Message Oriented Middleware ...
- [转] CentOS 7 为firewalld添加开放端口及相关资料
转自http://www.cnblogs.com/hubing/p/6058932.html 1.运行.停止.禁用firewalld 启动:# systemctl start firewalld 查 ...