题目大意就是:去一个地方探险,然后给你一些地图描写叙述这个地方,每一个描写叙述是一个矩形的右下角和左上角。地图有些地方是重叠的。所以让你求出被描写叙述的地方的总面积。

扫描线的第一道题,想了又想,啸爷还给我讲了讲,最终有点理解了啊。

先说扫描线:书上说扫描线不是一个物体。而是一个概念。

在计算几何中的作用类似于图论中的bfs与dfs。所以还是须要多做题目来体会一下啊。

这道题目的做法是:离散化x坐标。然后依照y坐标的大小进行排序,每一条保存它的左边界的位置与右边界的位置。以及自身的高度。

还有就是假设是下边初始为1。上边初始为-1。

接下来就是扫描线了:依照y值排序后的数组,開始遍历。

先二分查找它在离散数组中下表的位置。找到之后按他保存的边界的标记。进行更新。这里的区间更新用的是线段树的维护。我们以离散化后数组建树。

每一个节点保存它此时有多少个上界与下界的和,表示他是否存在矩形。

假设存在的话每一个节点中用sun数组保存这个矩形x值的差值(通过这个离散区间的下标,做减法就是离散的x的差值)。最后的时候乘上y的差值。

就是扫描到的矩阵的面积。

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17207   Accepted: 6549

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.

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.

Sample Input

  1. 2
  2. 10 10 20 20
  3. 15 15 25 25.5
  4. 0

Sample Output

  1. Test case #1
  2. Total explored area: 180.00

Source

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <iomanip>
  6. #include <stdio.h>
  7. #include <string>
  8. #include <queue>
  9. #include <cmath>
  10. #include <stack>
  11. #include <ctime>
  12. #include <map>
  13. #include <set>
  14. #define eps 1e-12
  15. ///#define M 1000100
  16. #define LL __int64
  17. ///#define LL long long
  18. ///#define INF 0x7ffffff
  19. #define INF 0x3f3f3f3f
  20. #define PI 3.1415926535898
  21. #define zero(x) ((fabs(x)<eps)?0:x)
  22.  
  23. using namespace std;
  24.  
  25. const int maxn = 5010;
  26.  
  27. struct node
  28. {
  29. double l, r, h;
  30. int x;
  31. } f[maxn];
  32.  
  33. double sum[maxn<<2];
  34. int cnt[maxn];
  35. double dc[maxn];
  36.  
  37. bool cmp(node a, node b)
  38. {
  39. return a.h < b.h;
  40. }
  41.  
  42. int Find(double x, double a[], int n)
  43. {
  44. int l = 0;
  45. int r = n-1;
  46. while(l <= r)
  47. {
  48. int mid = (l+r)/2;
  49. if(a[mid] == x) return mid;
  50. if(a[mid] > x) r = mid-1;
  51. else l = mid+1;
  52. }
  53. return -1;
  54.  
  55. }
  56.  
  57. void Up(int l, int r, int site)
  58. {
  59. if(cnt[site]) sum[site] = dc[r+1]-dc[l];
  60. else if(l == r) sum[site] = 0;
  61. else sum[site] = sum[site<<1]+sum[site<<1|1];
  62. }
  63.  
  64. void Update(int l, int r, int L, int R, int d, int site)
  65. {
  66. if(L <= l && r <= R)
  67. {
  68. cnt[site] += d;
  69. Up(l, r, site);
  70. return;
  71. }
  72. int mid = (l+r)>>1;
  73. if(L <= mid) Update(l, mid, L, R, d, site<<1);
  74. if(R > mid) Update(mid+1, r, L, R, d, site<<1|1);
  75. Up(l, r, site);
  76. }
  77.  
  78. int main()
  79. {
  80. int n;
  81. int Case = 1;
  82. while(cin >>n)
  83. {
  84. if(!n) break;
  85. double x1, y1, x2, y2;
  86. int m = 0;
  87. for(int i = 0; i < n; i++)
  88. {
  89. scanf("%lf %lf %lf %lf",&x1, &y1, &x2, &y2);
  90. dc[m] = x1;
  91. f[m].l = x1;
  92. f[m].r = x2;
  93. f[m].h = y1;
  94. f[m++].x = 1;
  95. dc[m] = x2;
  96. f[m].l = x1;
  97. f[m].r = x2;
  98. f[m].h = y2;
  99. f[m++].x = -1;
  100. }
  101. sort(dc, dc+m);
  102. sort(f, f+m, cmp);
  103. int k = unique(dc, dc+m)-dc;
  104. memset(cnt, 0 , sizeof(cnt));
  105. memset(sum, 0 , sizeof(sum));
  106. double ans = 0;
  107. for(int i = 0; i < m-1; i++)
  108. {
  109. int l = Find(f[i].l, dc, k);
  110. int r = Find(f[i].r, dc, k)-1;
  111. if(l <= r) Update(0, k-1, l, r, f[i].x, 1);
  112. ans += sum[1]*(f[i+1].h-f[i].h);
  113. }
  114. printf("Test case #%d\n",Case++);
  115. printf("Total explored area: %.2f\n\n",ans);
  116. }
  117. return 0;
  118. }

POJ 1151 HDU 1542 Atlantis(扫描线)的更多相关文章

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

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

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

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

  3. HDU 1542 Atlantis(矩形面积并)

    HDU 1542 Atlantis 题目链接 题意:给定一些矩形,求面积并 思路:利用扫描线,因为这题矩形个数不多,直接暴力扫就能够了.假设数据大.就要用线段树 代码: #include <cs ...

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

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

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

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

  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(段树&amp;扫描线&amp;面积和)

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

  8. (中等) HDU 1542 Atlantis,扫描线。

    Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...

  9. HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

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

随机推荐

  1. Oracle常用查询语句

    "ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己 ...

  2. [转]Python 之 使用 PIL 库做图像处理

    Python 之 使用 PIL 库做图像处理 1. 简介. 图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴.PIL (Python Imaging ...

  3. 四丶人生苦短,我用python【第四篇】

    1 基本数据类型 数字    int 字符串   str 布尔值   bool 列表       list 元组       tuple 字典       dict >>>type( ...

  4. Codeforces Round #402 (Div. 2) A+B+C+D

    Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...

  5. eclipse逆向生成实体类

    (转自:http://blog.csdn.net/wangpeng047/article/details/6877720) 做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理 ...

  6. 周赛Problem 1108: 蛋糕(二分)

    1108: 蛋糕 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 Description 杨神打代码打得有点疲倦,于是他想要 ...

  7. BZOJ 1855 [Scoi2010]股票交易 ——动态规划

    DP方程是比较简单的,主要有三种:什么都不做.买入.卖出. 发现买入卖出都是$\Theta (n^3)$但是转移方程都是线性的,而且决策和当前的情况是分开的. 所以可以单调队列优化. 复杂度$\The ...

  8. 刷题总结——分糖果(bzoj2330)

    题目: Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖 ...

  9. 【扫描线或树状数组】CSU 1335 高桥和低桥

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1335 [题意] 给定n座桥的高度,给定m次洪水每次的涨水水位ai和退水水位bi 询问有多少座桥 ...

  10. poj 1061 青蛙的约会(二元一次不定方程)

      Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...