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 ...
随机推荐
- 数据结构实现(四)二叉查找树java实现
转载 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树 ...
- 学一下HDFS,很不错(大数据技术原理及应用)
http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.
- 用MyEclipse 打包JAR文件
用MyEclipse 将自己定义标签打成JAR包 1.新建一个javaproject 2.将标签有关的java代码拷贝到新建javaproject的一个包中,这时会报错 ...
- POJ 1281 MANAGER
MANAGER Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Description O ...
- Python 获取Google+特定用户最新动态
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...
- 深入分析Java中的I/O类的特征及适用场合
Java中有40多个与输入输出有关的类.假设不理清它们之间的关系.就不能灵活地运用它们. 假设从流的流向来分,可分为输入流和输出流,而输入流和输出流又都可分为字节流和字符流.因而可将Java中的I/O ...
- Objective-c 中如何重写父类的初始化方法
在我们的日常开发中我们经常会定义一些自己的子类继承一些UIKit 库中的类,那我们应该如何重写的这些初化方法呢?那我们先看看这些类有哪些初初化方法吧.(这里就用UIView为例) - (id)init ...
- (转载)详解7.0带来的新工具类:DiffUtil
[Android]详解7.0带来的新工具类:DiffUtil 标签: diffutil 2017-04-17 18:21 226人阅读 评论(0) 收藏 举报 分类: Android学习笔记(94) ...
- 关于js里的document.compatmode
document.compatmode为获取页面的渲染模式. 其中有两个渲染模式 1.CSS1Compat(标准模式).浏览器宽度:document.documentElement.clientHei ...
- 网页结构的简介和Xpath语法的入门教程
相信很多小伙伴已经听说过Xpath,之前小编也写过一篇关于Xpath的文章,感兴趣的小伙伴可以戳这篇文章如何利用Xpath抓取京东网商品信息以及Python网络爬虫四大选择器(正则表达式.BS4.Xp ...