题目链接

Description

A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 

The corresponding boundary is the whole set of line segments drawn in Figure 2. 

The vertices of all rectangles have integer coordinates. 

Input

Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Output

Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

Sample Input

  1. 7
  2. -15 0 5 10
  3. -5 8 20 25
  4. 15 -4 24 14
  5. 0 -6 16 4
  6. 2 15 10 22
  7. 30 10 36 20
  8. 34 0 40 16

Sample Output

  1. 228

Source

 
题意:有多个矩形,矩形的两边平行于坐标轴,这些矩形之间可能存在相互覆盖,求周长。
 
思路:记录每个矩形的两条竖边(x1,y1,y2)和(x2,y1,y2),将所有的竖边按照x从小到大排序,然后一条一条竖边开始计算周长,那么以竖边所在的垂直于x轴的直线即是扫描线。每次移动到一条新的竖边的时候,我们需要计算所在竖边扫描线上有用边长(即当前竖边的有用部分,可能当前的竖边被覆盖部分),以及加上当前扫描线与上一条扫描线之前的横边长 * 横边条数,一直计算到最后一条竖边,即是完整周长了。
 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <vector>
  6. using namespace std;
  7. const int N=;
  8. struct Line{
  9. int x,y1,y2;
  10. int flag;
  11. bool operator<(const Line s)
  12. {
  13. if(x==s.x) return flag>s.flag;
  14. return x<s.x;
  15. }
  16. }line[N*];
  17.  
  18. struct Tree
  19. {
  20. int l,r;
  21. bool lf,rf; ///左右边界点是否被覆盖;
  22. int cover_len;
  23. int cover_num;
  24. int num; ///矩形数目;
  25. }tr[N*];
  26.  
  27. vector<int>v;
  28.  
  29. void build(int l,int r,int i)
  30. {
  31. tr[i].l=l; tr[i].r=r;
  32. tr[i].cover_len=;
  33. tr[i].cover_num=;
  34. tr[i].num=;
  35. tr[i].lf=tr[i].rf=false;
  36. if(l+==r) return ;
  37. int mid=(l+r)>>;
  38. build(l,mid,i<<);
  39. build(mid,r,i<<|);
  40. }
  41. void process(int i)
  42. {
  43. if(tr[i].cover_num>)
  44. {
  45. tr[i].cover_len=v[tr[i].r]-v[tr[i].l];
  46. tr[i].lf=tr[i].rf=true;
  47. tr[i].num=;
  48. return ;
  49. }
  50. if(tr[i].l+==tr[i].r)
  51. {
  52. tr[i].cover_len=;
  53. tr[i].num=;
  54. tr[i].lf=tr[i].rf=false;
  55. return ;
  56. }
  57. int ls=(i<<);
  58. int rs=(i<<|);
  59. tr[i].cover_len=tr[ls].cover_len+tr[rs].cover_len;
  60. tr[i].num=tr[ls].num + tr[rs].num - (tr[ls].rf & tr[rs].lf);
  61. tr[i].lf=tr[ls].lf; tr[i].rf=tr[rs].rf;
  62. }
  63. void update(int i,Line t)
  64. {
  65. if(t.y1<=v[tr[i].l] && v[tr[i].r]<=t.y2)
  66. {
  67. tr[i].cover_num+=t.flag;
  68. process(i);
  69. return ;
  70. }
  71. int mid=(tr[i].l+tr[i].r)>>;
  72. if(t.y1<v[mid]) update(i<<,t);
  73. if(t.y2>v[mid]) update(i<<|,t);
  74. process(i);
  75. }
  76. int main()
  77. {
  78. int n;
  79. while(scanf("%d",&n)!=EOF)
  80. {
  81. v.clear();
  82. for(int i=;i<n;i++)
  83. {
  84. int x1,y1,x2,y2;
  85. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  86. line[i*].x=x1; line[i*].y1=y1; line[i*].y2=y2; line[i*].flag=;
  87. line[i*+].x=x2; line[i*+].y1=y1; line[i*+].y2=y2; line[i*+].flag=-;
  88. v.push_back(y1);
  89. v.push_back(y2);
  90. }
  91. sort(line,line+*n);
  92. sort(v.begin(),v.end());
  93. int num=unique(v.begin(),v.end())-v.begin();
  94.  
  95. build(,num-,);
  96. int ans=,len=;
  97. for(int i=;i<*n;i++)
  98. {
  99. if(i>) ans+=tr[].num**(line[i].x-line[i-].x);
  100. update(,line[i]);
  101. ans+=abs(tr[].cover_len-len);
  102. len=tr[].cover_len;
  103. }
  104. printf("%d\n",ans);
  105. }
  106. return ;
  107. }

poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)的更多相关文章

  1. poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)

    题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...

  2. POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)

    题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...

  3. hdu1828 线段树扫描线求矩形面积的周长

    题意:       给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路:       线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...

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

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

  5. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  6. hdu1542 线段树扫描线求矩形面积的并

    题意:       给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路:       自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...

  7. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  8. UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]

    题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...

  9. 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交

    E - 秋实大哥与家 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

随机推荐

  1. java学习(一)

    目录 java简介 java基础 基本语法 java标识符 java变量 变量类型 变量声明 java常量 Java 基本数据类型 内置数据类型 引用数据类型 Java类型转换 java注释 操作符 ...

  2. java之路 把1到100之间的数的偶数相加

    /** *把1到100之间的数的偶数相加 */ class Demo{ public static void main(String[] args){ int i =1; int sum = 0; d ...

  3. js url参数解析获取

    function get_url_parm_list(str){ var params=str.substr(str.indexOf('?')+1); var param_list=[]; while ...

  4. Python 知识小tips

    python进制转换函数: 二进制转换成十进制:v = "0b1111011"    # int(v,2) 十进制转换成二进制:v = 18                   # ...

  5. 学以致用三十-----pycharm创建django项目忘记添加app

    记忆力有时候真的不是很好.因此有些操作步骤还是记录下来好了. pycharm版本-----2018.2.4 创建django项目 file-----newproject----- 创建的时候,appl ...

  6. REdis Asynchronous AOF fsync is taking too long

    redis.conf中的no-appendfsync-on-rewrite默认值为no,表示在重写AOF文件或RDB文件时阻塞fsync. 如果重写AOF或RDB文件时长过长,则在日志中可以看到如下信 ...

  7. java.lang.RuntimeException: Invalid action class configuration that references an unknown class name

    ---恢复内容开始--- 转自 : https://www.cnblogs.com/javawebsoa/archive/2013/05/25/3098190.html java.lang.Runti ...

  8. VS中Debug与Release、_WIN32与_WIN64的区别

    一.Debug与Release 1.  区别 Debug——调试版,生成的.exe中包含很多调试信息,若直接发包,比较大: Release——发布版 2.  如何区分是Debug编译还是Release ...

  9. <笔记>原生PHP访问TP模板变量

    在模板中,原生PHP可以直接访问模板变量,不过如果模板变量是数组,要访问数组中元素时不能使用"."符号,只能通过数组['元素名']的方式

  10. python基础自学 第四天

    break和continue break:某一条件满足,退出循环,不在执行后续重复代码 continue:某一条件满足时,不执行后续重复的代码 注意:在循环中,如果使用continue这个关键字,使用 ...