链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542

题意:

求所给矩形的覆盖面积

题解:

利用扫描线的思想,先将坐标离散化,之后以y轴分成多个矩形求解,可以让下边界+1上边界-1

问题就转化为了:求区间中有多少个非0数,要求支持区间+1 -1操作

我们可以通过维护区间最小值以及最小值的个数来完成这件事情

代码:

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include <iostream>
  6. #define maxn 5000
  7. #define INF 59999999
  8. #define eps 1e-6
  9. #define mid (h+t)/2
  10. using namespace std;
  11. struct re
  12. {
  13. double a;int b;
  14. }a[maxn],b[maxn];
  15. struct ree
  16. {
  17. int h,t,x,lazy;
  18. double sum,tot;
  19. }p[maxn*];
  20. double c[maxn];
  21. int a1[maxn];
  22. bool cmp(re x,re y)
  23. {
  24. if (x.a<y.a) return(true); else return(false);
  25. }
  26. void build(int x,int h,int t)
  27. {
  28. p[x].h=h; p[x].t=t; p[x].x=;
  29. if (h==t)
  30. {
  31. p[x].sum=p[x].tot=c[h+]-c[h];
  32. return;
  33. }
  34. build(x*,h,mid);
  35. build(x*+,mid+,t);
  36. p[x].sum=p[x].tot=p[x*].sum+p[x*+].sum;
  37. }
  38. void updata(int x)
  39. {
  40. p[x].x=min(p[x*].x,p[x*+].x);
  41. if (p[x*].x==p[x*+].x)
  42. {
  43. p[x].sum=p[x*].sum+p[x*+].sum;
  44. }
  45. else
  46. {
  47. if (p[x*].x<p[x*+].x) p[x].sum=p[x*].sum;
  48. else p[x].sum=p[x*+].sum;
  49. }
  50. return;
  51. }
  52. void down(int x)
  53. {
  54. if (p[x].lazy==) return;
  55. p[x].x+=p[x].lazy;
  56. p[x*].lazy+=p[x].lazy;
  57. p[x*+].lazy+=p[x].lazy;
  58. p[x].lazy=;
  59. }
  60. void insert(int x,int h,int t,int sum)
  61. {
  62. down(x);
  63. if (p[x].h>t|| p[x].t<h) return;
  64. if (h<=p[x].h &&p[x].t<=t)
  65. {
  66. p[x].lazy+=sum; down(x); return;
  67. }
  68. insert(x*,h,t,sum); insert(x*+,h,t,sum);
  69. updata(x);
  70. }
  71. double query(int x,int h,int t)
  72. {
  73. down(x);
  74. if (p[x].h>t||p[x].t<h) return();
  75. if (h<=p[x].t && p[x].t<=t)
  76. {
  77. if (p[x].x==) return(p[x].tot-p[x].sum); else return(p[x].tot);
  78. }
  79. return(query(x*,h,t)+query(x*+,h,t));
  80. }
  81. int main()
  82. {int n,o=;
  83. while (cin>>n&&n!=)
  84. {
  85. o++;
  86. memset(p,,sizeof(p));
  87. for (int i=;i<=*n;i++) a[i].b=i,b[i].b=i;
  88. for (int i=;i<=n;i++)
  89. {
  90. cin>>a[*i-].a>>b[*i-].a>>a[*i].a>>b[*i].a;
  91. }
  92. sort(a+,a++*n,cmp);
  93. sort(b+,b++*n,cmp);
  94. int ll=; a[].a=INF;
  95. for (int i=;i<=*n;i++)
  96. {
  97. if (abs(a[i].a-a[i-].a)>eps) ll++;
  98. a1[a[i].b]=ll;
  99. c[ll]=a[i].a;
  100. }
  101. double ans=;
  102. build(,,ll-);
  103. for (int i=;i<*n;i++)
  104. {
  105. int pp,tmp=b[i].b;
  106. if (tmp%==) pp= ;else pp=-;
  107. insert(,a1[(tmp+)/*-],a1[(tmp+)/*]-,pp);
  108. ans+=(b[i+].a-b[i].a)*query(,,ll-);
  109. }
  110. cout<<"Test case #"<<o<<endl<<"Total explored area: " ;
  111. printf("%.2f\n\n",ans);
  112. }
  113. }

hdu 1542的更多相关文章

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

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

  2. ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)

    ●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...

  3. hdu 1542 线段树扫描(面积)

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

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

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

  5. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

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

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

  7. 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543

    学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...

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

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

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

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

随机推荐

  1. bzoj千题计划301:bzoj4259: 残缺的字符串

    https://www.lydsy.com/JudgeOnline/problem.php?id=4259 令通配符=0 f[i+m-1]=Σ (a[i+j]-b[m-1-j])^2 * a[i+j] ...

  2. java 多线程下载功能

    import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...

  3. 顺序列表(栈/队列等)ADT[C++]

    #include<iostream> using namespace std; //ADT template<class T> class SeqList{ public: / ...

  4. java先导课程学习总结

    经过两个星期四节课的java学习,我也对java这门语言有了一定的认识.刚开始上课的时候,我认为java把C语言中老师所说的模块化编程进行了强调,进行一个类,一个类的编程,在类中构造相应的方法,使用的 ...

  5. C - Little Jumper (三分)

    题目链接:https://cn.vjudge.net/contest/281961#problem/C 题目大意:青蛙能从一个点跳到第三个点,如图,需要跳两次.问整个过程的最大起跳速度中的最小的. 具 ...

  6. CodeForces Contest #1137: Round #545 (Div. 1)

    比赛传送门:CF #1137. 比赛记录:点我. 每次都自闭的 div1 啊,什么时候才能上 IM 呢. [A]Skyscrapers 题意简述: 有一个 \(n\times m\) 的矩阵 \(a_ ...

  7. AutoMapper中用户自定义转换

    Custom Type Converters Sometimes, you need to take complete control over the conversion of one type ...

  8. 二、Linear Regression 练习(转载)

    转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言 本文是多元线性回归的练习,这里练习的是最简单的二元 ...

  9. UML和模式应用5:细化阶段(6)---操作契约

    1.前言 操作契约使用前置和后置条件,描述领域模型里对象的详细变化,作为系统操作的结果. 操作契约可以作为有用的OOA相关的制品. 操作契约可以视为UP用例模型的一部分,它是对用例之处的系统操作的效用 ...

  10. 3种shell自动交互的方法【转】

    一.背景 shell脚本在处理自动循环或大的任务方面可节省大量的时间,通过创建一个处理任务的命令清单,使用变量.条件.算术和循环等方法快速创建脚本以完成相应工作,这比在命令行下一个个敲入命令要省时省力 ...