HDU 1542 Atlantis

题目链接

题意:给定一些矩形,求面积并

思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了。假设数据大。就要用线段树

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N = 205;
  8. const int M = 100005;
  9. const double eps = 1e-8;
  10.  
  11. int n, vis[N], hn;
  12. double hash[N];
  13.  
  14. struct Line {
  15. double l, r, y;
  16. int flag;
  17. Line() {}
  18. Line(double l, double r, double y, int flag) {
  19. this->l = l;
  20. this->r = r;
  21. this->y = y;
  22. this->flag = flag;
  23. }
  24. } line[N];
  25.  
  26. bool cmp(Line a, Line b) {
  27. return a.y < b.y;
  28. }
  29.  
  30. int get(double x) {
  31. return lower_bound(hash, hash + hn, x) - hash;
  32. }
  33.  
  34. int main() {
  35. int cas = 0;
  36. while (~scanf("%d", &n) && n) {
  37. double x1, x2, y1, y2; hn = 0;
  38. memset(vis, 0, sizeof(vis));
  39. for (int i = 0; i < n; i++) {
  40. scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
  41. line[i * 2] = Line(x1, x2, y1, 1);
  42. line[i * 2 + 1] = Line(x1, x2, y2, -1);
  43. hash[hn++] = x1; hash[hn++] = x2;
  44. }
  45. n *= 2;
  46. sort(line, line + n, cmp);
  47. sort(hash, hash + hn);
  48. hn = 1;
  49. for (int i = 1; i < n; i++) {
  50. if (fabs(hash[i] - hash[i - 1]) < eps) continue;
  51. hash[hn++] = hash[i];
  52. }
  53. double ans = 0;
  54. for (int i = 0; i < n; i++) {
  55. int l = get(line[i].l), r = get(line[i].r);
  56. double len = 0;
  57. for (int j = 0; j < hn - 1; j++) if (vis[j] > 0) len += (hash[j + 1] - hash[j]);
  58. if (i) ans += len * (line[i].y - line[i - 1].y);
  59. for (int j = l; j < r; j++) vis[j] += line[i].flag;
  60. }
  61. printf("Test case #%d\n", ++cas);
  62. printf("Total explored area: %.2lf\n\n", ans);
  63. }
  64. return 0;
  65. }

HDU 1542 Atlantis(矩形面积并)的更多相关文章

  1. (HDU 1542) Atlantis 矩形面积并——扫描线

    n个矩形,可以重叠,求面积并. n<=100: 暴力模拟扫描线.模拟赛大水题.(n^2) 甚至网上一种“分块”:分成n^2块,每一块看是否属于一个矩形. 甚至这个题就可以这么做. n<=1 ...

  2. 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))

    扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...

  3. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  4. POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并

    题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...

  5. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  6. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. HDU 1542 Atlantis(扫描线)题解

    题意:给n个可能相交的矩形,问你不重复的总面积 思路:扫描线,一边扫一边加. 扫描线:图片来源:理解扫描线 假设我们要算以下四个矩形面积,显然中间深色的是重复的.我们按照x的大小,从左往右扫,然后用线 ...

  9. hdu 1542 Atlantis (线段树扫描线)

    大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...

随机推荐

  1. 数据结构实现(四)二叉查找树java实现

    转载 http://www.cnblogs.com/CherishFX/p/4625382.html 二叉查找树的定义: 二叉查找树或者是一颗空树,或者是一颗具有以下特性的非空二叉树: 1. 若左子树 ...

  2. 学一下HDFS,很不错(大数据技术原理及应用)

    http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.

  3. 用​M​y​E​c​l​i​p​s​e​ ​打​包​J​A​R文件

    用​M​y​E​c​l​i​p​s​e​ ​将自己定义标签打​成​J​A​R​包 1.新建一个javaproject 2.将标签有关的java代码拷贝到新建javaproject的一个包中,这时会报错 ...

  4. POJ 1281 MANAGER

    MANAGER Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u Description O ...

  5. Python 获取Google+特定用户最新动态

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...

  6. 深入分析Java中的I/O类的特征及适用场合

    Java中有40多个与输入输出有关的类.假设不理清它们之间的关系.就不能灵活地运用它们. 假设从流的流向来分,可分为输入流和输出流,而输入流和输出流又都可分为字节流和字符流.因而可将Java中的I/O ...

  7. Objective-c 中如何重写父类的初始化方法

    在我们的日常开发中我们经常会定义一些自己的子类继承一些UIKit 库中的类,那我们应该如何重写的这些初化方法呢?那我们先看看这些类有哪些初初化方法吧.(这里就用UIView为例) - (id)init ...

  8. (转载)详解7.0带来的新工具类:DiffUtil

    [Android]详解7.0带来的新工具类:DiffUtil 标签: diffutil 2017-04-17 18:21 226人阅读 评论(0) 收藏 举报  分类: Android学习笔记(94) ...

  9. 关于js里的document.compatmode

    document.compatmode为获取页面的渲染模式. 其中有两个渲染模式 1.CSS1Compat(标准模式).浏览器宽度:document.documentElement.clientHei ...

  10. 网页结构的简介和Xpath语法的入门教程

    相信很多小伙伴已经听说过Xpath,之前小编也写过一篇关于Xpath的文章,感兴趣的小伙伴可以戳这篇文章如何利用Xpath抓取京东网商品信息以及Python网络爬虫四大选择器(正则表达式.BS4.Xp ...