Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21734   Accepted: 8179

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


[10-13-2016]
有一条线从下往上扫,维护当前的x轴覆盖总长度
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <map>
  6. using namespace std;
  7. const int N=;
  8. #define m (l+r)/2
  9. #define lson o<<1,l,m
  10. #define rson o<<1|1,m+1,r
  11. #define lc o<<1
  12. #define rc o<<1|1
  13. inline int read(){
  14. char c=getchar();int x=,f=;
  15. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  16. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  17. return x*f;
  18. }
  19. int n,cnt=;
  20. double x1,y1,x2,y2,mp[N];
  21. struct seg{
  22. double l,r,h;
  23. int f;//1 or -1
  24. seg(double a=,double b=,double c=,int d=):l(a),r(b),h(c),f(d){}
  25. bool operator <(const seg &r)const{return h<r.h;}
  26. }a[N];
  27. struct node{
  28. double sum;
  29. int cov;
  30. }t[N<<];
  31. inline int Bin(double v){
  32. int l=,r=cnt;
  33. while(l<=r){
  34. int mid=(l+r)>>;
  35. if(mp[mid]==v) return mid;
  36. else if(v<mp[mid]) r=mid-;
  37. else l=mid+;
  38. }
  39. return -;
  40. }
  41. inline void pushUp(int o,int l,int r){
  42. if(t[o].cov) t[o].sum=mp[r+]-mp[l];
  43. else if(l==r) t[o].sum=;
  44. else t[o].sum=t[lc].sum+t[rc].sum;
  45. }
  46. void update(int o,int l,int r,int ql,int qr,int v){
  47. if(ql<=l&&r<=qr){
  48. t[o].cov+=v;
  49. pushUp(o,l,r);
  50. }else{
  51. if(ql<=m) update(lson,ql,qr,v);
  52. if(m<qr) update(rson,ql,qr,v);
  53. pushUp(o,l,r);
  54. }
  55. }
  56. int cas=;
  57. int main(int argc, const char * argv[]) {
  58. while((n=read())){
  59. double ans=;
  60. for(int i=;i<=n;i++){
  61. scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
  62. a[i*-]=seg(x1,x2,y1,);
  63. a[i*]=seg(x1,x2,y2,-);
  64. mp[*i-]=x1;
  65. mp[*i]=x2;
  66. }
  67. sort(mp+,mp++*n);
  68. sort(a+,a++*n);
  69. cnt=;mp[++cnt]=mp[];
  70. for(int i=;i<=*n;i++)
  71. if(mp[i]!=mp[i-]) mp[++cnt]=mp[i];
  72. memset(t,,sizeof(t));
  73. for(int i=;i<=*n-;i++){//最后一个不用
  74. int ql=Bin(a[i].l),qr=Bin(a[i].r)-;
  75. if(ql<=qr) update(,,cnt,ql,qr,a[i].f);
  76. ans+=t[].sum*(a[i+].h-a[i].h);
  77. }
  78. printf("Test case #%d\n",++cas);
  79. printf("Total explored area: %.2f\n\n",ans);
  80. }
  81.  
  82. return ;
  83. }

线段树需要插入线段,删除线段,求线段覆盖的总长度,貌似还是用标记永久化比较方便,否则删(我)除(没)很(写)麻(出)烦(来)

注意这个线段树节点是一段区间哦

离散化m忘清0了 WA了几次

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <map>
  6. using namespace std;
  7. const int N=;
  8. #define lson x<<1,l,mid
  9. #define rson x<<1|1,mid+1,r
  10. #define lc x<<1
  11. #define rc x<<1|1
  12. inline int read(){
  13. char c=getchar();int x=,f=;
  14. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  15. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  16. return x*f;
  17. }
  18. int n;
  19. double x1,y1,x2,y2;
  20. struct Seg{
  21. double l,r,y;
  22. int f;
  23. Seg(double l=,double r=,double y=,int f=):l(l),r(r),y(y),f(f){}
  24. bool operator <(const Seg &a)const{return y<a.y;}
  25. }a[N];
  26. double mp[N];int m;
  27. void iniMP(){
  28. sort(mp+,mp++m);
  29. int p=;
  30. mp[++p]=mp[];
  31. for(int i=;i<=m;i++) if(mp[i]!=mp[i-]) mp[++p]=mp[i];
  32. m=p;
  33. }
  34. inline int Bin(double v){
  35. int l=,r=m;
  36. while(l<=r){
  37. int mid=(l+r)>>;
  38. if(v==mp[mid]) return mid;
  39. else if(v<mp[mid]) r=mid-;
  40. else l=mid+;
  41. }
  42. return ;
  43. }
  44. struct node{
  45. double sum;
  46. int cov;
  47. node():sum(),cov(){}
  48. }t[N<<];
  49. void pushUp(int x,int l,int r){
  50. if(t[x].cov) t[x].sum=mp[r+]-mp[l];
  51. else if(l==r) t[x].sum=;
  52. else t[x].sum=t[lc].sum+t[rc].sum;
  53. }
  54. void segCov(int x,int l,int r,int ql,int qr,int v){
  55. if(ql<=l&&r<=qr) t[x].cov+=v,pushUp(x,l,r);
  56. else{
  57. int mid=(l+r)>>;
  58. if(ql<=mid) segCov(lson,ql,qr,v);
  59. if(mid<qr) segCov(rson,ql,qr,v);
  60. pushUp(x,l,r);
  61. }
  62. }
  63.  
  64. int cas=;
  65. int main(int argc, const char * argv[]) {
  66. while((n=read())){
  67. m=;
  68. for(int i=;i<=n;i++){
  69. scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
  70. a[i*-]=Seg(x1,x2,y1,);
  71. a[i*]=Seg(x1,x2,y2,-);
  72. mp[++m]=x1;mp[++m]=x2;
  73. }
  74. iniMP();
  75. n<<=;
  76. sort(a+,a++n);
  77. memset(t,,sizeof(t));
  78. double ans=;
  79. for(int i=;i<=n-;i++){
  80. int ql=Bin(a[i].l),qr=Bin(a[i].r)-;
  81. if(ql<=qr) segCov(,,m,ql,qr,a[i].f);
  82. ans+=t[].sum*(a[i+].y-a[i].y);
  83. }
  84. printf("Test case #%d\n",++cas);
  85. printf("Total explored area: %.2f\n\n",ans);
  86. }
  87.  
  88. return ;
  89. }
 

POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]的更多相关文章

  1. POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  2. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  3. poj-1151矩形面积并-线段树

    title: poj-1151矩形面积并-线段树 date: 2018-10-30 22:35:11 tags: acm 刷题 categoties: ACM-线段树 概述 线段树问题里的另一个问题, ...

  4. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  5. HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...

  6. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

  7. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  8. hdu1542线段树+离散化+扫描线

    参考博客: http://blog.csdn.net/xingyeyongheng/article/details/8927732 总的来说就是用一条(假想的)线段去平行x轴从下往上扫描,扫描的过程中 ...

  9. Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长

    参考  https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...

随机推荐

  1. 【开发软件】推荐一款MAC OS X 下php集成开发环境mamp

      这里给大家推荐一款在mac上搭建WEB服务器环境的集成环境安装软件,非常的好用,需要的朋友可以拿去,不用谢 ^_^   之前苦于mac上搭建本地服务器之艰辛,找寻好久都没找到一款类似windows ...

  2. 使用jenkins配置.net mvc网站进行持续集成一

    最近好久没有更新文章了,因为好久没有写代码了,以至于我不知道同大家分享些什么,刚好,今天突然叫我学习下jenkins每日构建,我就把今天的学习笔记记录下来,这其中很多东西都是公司同事之前调研总结的,我 ...

  3. GJM:Unity导入百度地图SDK [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  4. 从CSS实现正片叠底看=>混合模式mix-blend-mode

    兼容性:这个东西说多了也没意思,像HTML5和CSS3这种兼容性时刻变化的东东,我们最好在自己支持的设备上实验,不支持,就在想办法呗,这个东西就是为了方便和好玩 所有属性: mix-blend-mod ...

  5. 使用原生JS实现一个风箱式的demo,并封装了一个运动框架

    声明,该DEMO依托于某个培训机构中,非常感谢这个培训结构.话不多说,现在开始改demo的制作. 首先,在前端的学习过程中,轮播图是我们一定要学习的,所以为了更加高效的实现各种轮播图,封装了一个运动的 ...

  6. 移动AD的计算机到对应的OU的powershell脚本

    #//************************************************************* #//编辑人: #//编辑单位: #//编辑作用:移动计算机到对应的O ...

  7. UITextView 开始编辑时,文字没有左上角对齐解决办法 tableview整体上移

    现实情况如上所示 我出现这种情况的原因有两种: 其一:没有给textview对齐方式: 其二:没有将BOOL类型的“ automaticallyAdjustsScrollViewInsets ”属性置 ...

  8. Servlet的生命周期+实现方式

    1.Servlet的生命周期:        (1)被创建:            默认情况下,Servlet第一次被访问时,被服务器创建.会调用init()方法.                一个 ...

  9. iOS 学习 - 19 结构体

    //创建新类型typedef struct { int age; ];//最大字节为 20 }Student; Student value2 = {,*strcpy(value2.name, &quo ...

  10. 关于Ruby常用语法案例累积

    变量问题: 类变量和方法变量的区别是什么? 类变量:可以直接使用 方法变量:需要实例化后,才能使用该变量 案例一: class Person @@name = "Tom" @@na ...