一样的题:HDU 1542

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18148   Accepted: 6902

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
  3.  
  4. 求矩形并、线段树+扫描线
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. #define N 210
  7.  
  8. int n;
  9. int tot;
  10. double sum;
  11. double y[N];
  12. double yl[N<<];
  13. double yr[N<<];
  14. int cover[N<<];
  15. double height[N<<];
  16. struct X
  17. {
  18. int pos;
  19. double x,y1,y2;
  20. bool operator <(const X &t)const
  21. {
  22. return x<t.x;
  23. }
  24. }x[N];
  25.  
  26. void pushup(int l,int r,int rt)
  27. {
  28. if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
  29. else if(l+==r) height[rt]=;
  30. else height[rt]=height[rt<<]+height[rt<<|];
  31. }
  32. void build(int l,int r,int rt)
  33. {
  34. cover[rt]=;
  35. yl[rt]=y[l];
  36. yr[rt]=y[r];
  37. if(l+==r) return;
  38. int m=(l+r)>>;
  39. build(l,m,rt<<);
  40. build(m,r,rt<<|);
  41. }
  42. void update(int l,int r,int rt,double L,double R,int c)
  43. {
  44. if(yl[rt]==L && yr[rt]==R)
  45. {
  46. cover[rt]+=c;
  47. pushup(l,r,rt);
  48. return;
  49. }
  50. int m=(l+r)>>;
  51. if(R<=y[m]) update(l,m,rt<<,L,R,c);
  52. else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
  53. else
  54. {
  55. update(l,m,rt<<,L,y[m],c);
  56. update(m,r,rt<<|,y[m],R,c);
  57. }
  58. pushup(l,r,rt);
  59. }
  60. int main()
  61. {
  62. int i,iCase=;
  63. while(scanf("%d",&n),n)
  64. {
  65. tot=;
  66. sum=;
  67. memset(height,,sizeof(height));
  68. for(i=;i<n;i++)
  69. {
  70. double x1,y1,x2,y2;
  71. scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
  72. y[tot]=y1;
  73. x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
  74. y[tot]=y2;
  75. x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
  76. }
  77. sort(x,x+tot);
  78. sort(y,y+tot);
  79. build(,tot-,);
  80. for(i=;i<tot;i++)
  81. {
  82. update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
  83. sum+=height[]*(x[i].x-x[i-].x);
  84. }
  85. printf("Test case #%d\nTotal explored area: %.2f\n\n",iCase++,sum);
  86. }
  87. return ;
  88. }

一样的题:POJ 1389

Area of Simple Polygons
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3193   Accepted: 1627

Description

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates.

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point.

Example: Consider the following three rectangles:

rectangle 1: < (0, 0) (4, 4) >,

rectangle 2: < (1, 1) (5, 2) >,

rectangle 3: < (1, 1) (2, 5) >.

The total area of all simple polygons constructed by these rectangles is 18.

Input

The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

Output

For each test case, output the total area of all simple polygons in a line. 

Sample Input

  1. 0 0 4 4
  2. 1 1 5 2
  3. 1 1 2 5
  4. -1 -1 -1 -1
  5. 0 0 2 2
  6. 1 1 3 3
  7. 2 2 4 4
  8. -1 -1 -1 -1
  9. -1 -1 -1 -1 

Sample Output

  1. 18
  2. 10
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. #define N 210
  7.  
  8. int n;
  9. int tot;
  10. double sum;
  11. double y[N];
  12. double yl[N<<];
  13. double yr[N<<];
  14. int cover[N<<];
  15. double height[N<<];
  16. struct X
  17. {
  18. int pos;
  19. double x,y1,y2;
  20. bool operator <(const X &t)const
  21. {
  22. return x<t.x;
  23. }
  24. }x[N];
  25.  
  26. void pushup(int l,int r,int rt)
  27. {
  28. if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
  29. else if(l+==r) height[rt]=;
  30. else height[rt]=height[rt<<]+height[rt<<|];
  31. }
  32. void build(int l,int r,int rt)
  33. {
  34. cover[rt]=;
  35. yl[rt]=y[l];
  36. yr[rt]=y[r];
  37. if(l+==r) return;
  38. int m=(l+r)>>;
  39. build(l,m,rt<<);
  40. build(m,r,rt<<|);
  41. }
  42. void update(int l,int r,int rt,double L,double R,int c)
  43. {
  44. if(yl[rt]==L && yr[rt]==R)
  45. {
  46. cover[rt]+=c;
  47. pushup(l,r,rt);
  48. return;
  49. }
  50. int m=(l+r)>>;
  51. if(R<=y[m]) update(l,m,rt<<,L,R,c);
  52. else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
  53. else
  54. {
  55. update(l,m,rt<<,L,y[m],c);
  56. update(m,r,rt<<|,y[m],R,c);
  57. }
  58. pushup(l,r,rt);
  59. }
  60. int main()
  61. {
  62. int i;
  63. while()
  64. {
  65. tot=;
  66. sum=;
  67. memset(height,,sizeof(height));
  68. n=;
  69. double x1,y1,x2,y2;
  70. while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) && x1+x2+y1+y2!=-)
  71. {
  72. y[tot]=y1;
  73. x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
  74. y[tot]=y2;
  75. x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
  76. n++;
  77. }
  78. if(n==) break;
  79. sort(x,x+tot);
  80. sort(y,y+tot);
  81. build(,tot-,);
  82. for(i=;i<tot;i++)
  83. {
  84. update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
  85. sum+=height[]*(x[i].x-x[i-].x);
  86. }
  87. printf("%.0f\n",sum);
  88. }
  89. return ;
  90. }

[POJ 1151] Atlantis的更多相关文章

  1. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

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

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

  3. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  4. POJ 1151 Atlantis (扫描线+线段树)

    题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...

  5. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

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

  6. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

  7. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  9. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...

随机推荐

  1. 数据库不能用delete---index空间不足

    只有0702表是这样的情况,0101表可以使用like和between and

  2. 此一生 一个纯js的ajax

    /** * 得到ajax对象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp ...

  3. iOS 成员变量的作用范围

    /* 成员变量的作用范围: @public:在任何地方都能直接访问对象的成员变量 @private:只能在当前类的对象方法中直接访问,如果子类要访问需要调用父类的get/set方法 @protecte ...

  4. 在Mac OS X中使用VIM开发STM32(2)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在我先前的博文⎣在Mac OS X中使用VIM开发STM32(1)⎤中,我们安装完成了MACVIM,这一 ...

  5. [可拖动DIV]刚开通博客顺便就写了点东西!

    说说我自己的思路 首先需要一个初始div div { border: 1px #333 solid; width: 200px; height: 50px; } <div id="od ...

  6. 网站重构-你了解AJAX吗?

    AJAX是时下最流行的一种WEB端开发技术,而你真正了解它的一些特性吗?--IT北北报 XMLHTTPRequest(XHR)是目前最常用的技术,它允许异步接收和发送数据,所有的主流浏览器都对它有不错 ...

  7. switch...case 和 if...else

    switch...case与if...else的根本区别在于: switch...case会生成一个跳转表来指示实际的case分支的地址,而这个跳转表的索引号与switch变量的值是相等的,switc ...

  8. 使用vagrant作为开发环境后,js报错

    当你尝试修改一个js,并且用同样的方法更新之后,会遇到类似的问题,是的,就算重启VM上任何服务,甚至重启VM,依旧没有用,当然,比起其他资源文件,浏览器的反应会强烈一些,因为浏览器会提示未知错误,而你 ...

  9. YII2框架动态创建表模型

    YII2框架动态创建表模型 在YII2中,每个表对应一个model类 在开发过程中,我们在填写一个大型表单的时候,表单里有N个select下拉列表,每个下拉select来自于不同的表: 如果要在程序里 ...

  10. Popen No such file or directory 错误

    File , in reload_config stderr=PIPE, env={"PATH": '', "HOME": "/root"} ...