给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据. 
Output对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数. 
Sample Input

  1. 2
  2. 5
  3. 1 1 4 2
  4. 1 3 3 7
  5. 2 1.5 5 4.5
  6. 3.5 1.25 7.5 4
  7. 6 3 10 7
  8. 3
  9. 0 0 1 1
  10. 1 0 2 1
  11. 2 0 3 1

Sample Output

  1. 7.63
  2. 0.00
  3.  
  4. 这题是线段树扫描线水题
  5.  
  6. 扫描线一开始以为特别难
    但是用心去看还是比较容易的
    试着用自己的方法理解
    掌握思想
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <ctype.h>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <stack>
  10. #include <iostream>
  11. using namespace std;
  12. #define bug printf("******\n");
  13. #define rtl rt<<1
  14. #define rtr rt<<1|1
  15. typedef long long LL;
  16. const int maxn = 1e4 + ;
  17. struct LINE {
  18. double x, y1, y2;
  19. int flag;
  20. } line[maxn];
  21. int cmp(LINE a, LINE b) {
  22. return a.x < b.x;
  23. }
  24. struct node {
  25. double x, l, r, pre;
  26. int flag, cover;
  27. } tree[maxn << ];
  28. double y[maxn];
  29. void build(int l, int r, int rt ) {
  30. tree[rt].l = y[l], tree[rt].r = y[r];
  31. tree[rt].pre = , tree[rt].cover = , tree[rt].flag = ;
  32. if (l + == r) {
  33. tree[rt].flag = ;
  34. return ;
  35. }
  36. int m = (l + r) >> ;
  37. build(l, m, rtl);
  38. build(m, r, rtr);
  39. }
  40. double query(int rt, double x, double y1, double y2, int flag) {
  41. if (tree[rt].l >= y2 || tree[rt].r <= y1) return ;
  42. if (tree[rt].flag == ) {
  43. if (tree[rt].cover > ) {
  44. double pre = tree[rt].pre;
  45. double ans = (x - pre) * (tree[rt].r - tree[rt].l);
  46. tree[rt].pre = x;
  47. tree[rt].cover += flag;
  48. return ans;
  49. } else {
  50. tree[rt].cover += flag;
  51. tree[rt].pre = x;
  52. return ;
  53. }
  54. }
  55. return query(rtl, x, y1, y2, flag) + query(rtr, x, y1, y2, flag);
  56. }
  57. int main() {
  58. int t, n;
  59. scanf("%d", &t);
  60. while(t--) {
  61. scanf("%d", &n);
  62. int cnt = ;
  63. for (int i = ; i < n ; i++) {
  64. double x1, y1, x2, y2;
  65. scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
  66. y[cnt] = y1;
  67. line[cnt].x = x1;
  68. line[cnt].y1 = y1;
  69. line[cnt].y2 = y2;
  70. line[cnt++].flag = ;
  71. y[cnt] = y2;
  72. line[cnt].x = x2;
  73. line[cnt].y1 = y1;
  74. line[cnt].y2 = y2;
  75. line[cnt++].flag = -;
  76. }
  77. sort(y, y + cnt );
  78. sort(line, line + cnt, cmp);
  79. build(, cnt-, );
  80. double ans = ;
  81. for (int i = ; i < cnt ; i++)
  82. ans += query(, line[i].x, line[i].y1, line[i].y2, line[i].flag);
  83. printf("%.2f\n", ans);
  84. }
  85. return ;
  86. }

覆盖的面积 HDU - 1255 (线段树-扫描线)模板提的更多相关文章

  1. 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积

    #include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...

  2. 覆盖的面积(HDU 1255 线段树)

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem D ...

  3. hdu 1255(线段树 扫描线) 覆盖的面积

    http://acm.hdu.edu.cn/showproblem.php?pid=1255 典型线段树辅助扫描线,顾名思义扫描线就是相当于yy出一条直线从左到右(也可以从上到下)扫描过去,此时先将所 ...

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

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

  5. hdu 1828 线段树扫描线(周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  8. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

  9. HDU 5107 线段树扫描线

    给出N个点(x,y).每一个点有一个高度h 给出M次询问.问在(x,y)范围内第k小的高度是多少,没有输出-1 (k<=10) 线段树扫描线 首先离散化Y坐标,以Y坐标建立线段树 对全部的点和询 ...

随机推荐

  1. 157. Unique Characters 【LintCode by java】

    Description Implement an algorithm to determine if a string has all unique characters. Example Given ...

  2. 数据库Mysql的学习(三)-各种约束

    删除数据库表 drop table [if exists] 表一,表二.....; 表分区:比如图书信息表有1000万个图书信息,如何优化他,其中一种方式就是表分区.就是把一张表的数据分成多个区块,这 ...

  3. Prime Matrix(暴力出奇迹)

    Description You've got an n × m matrix. The matrix consists of integers. In one move, you can apply ...

  4. Thunder团队第一周 - Scrum会议2

    Scrum会议2 小组名称:Thunder 项目名称:待定 Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康(M ...

  5. 2017-2018-2 20172323 『Java程序设计』课程 结对编程练习_四则运算 2

    相关过程截图 关键代码解释 将运算式分开的代码 String[] result = num.split("\\s"); 将输入的num以空格为间隔符号分开,将每一个间隔开的字符存入 ...

  6. # ML学习小笔记—Classification

    关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 通过模型可以分类输入,此时根据分类结果的正确与否会有一个Loss函数.找 ...

  7. 异常--throw

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. [CLR via C#]值类型的装箱和拆箱

    我们先来看一个示例代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Array ...

  9. Sitemesh小记

    一.前言 因参与公司框架改造,接触到了Sitemesh这个用于网页布局和修饰的框架,因之前没有接触过(汗颜),但是发现其小巧好用,便以此文记之~ 二.正文 Sitemesh有什么作用呢?我相信很多人在 ...

  10. [C/C++] 结构体存储问题

    64位操作系统,不同类型变量对应的字节数为: char : 1个字节 char*(即指针变量) : 8个字节 //32位占4个字节 short int : 2个字节 int : 4个字节 unsign ...