题目链接

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

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

Sample Output

228

Source

 
题意:有多个矩形,矩形的两边平行于坐标轴,这些矩形之间可能存在相互覆盖,求周长。
 
思路:记录每个矩形的两条竖边(x1,y1,y2)和(x2,y1,y2),将所有的竖边按照x从小到大排序,然后一条一条竖边开始计算周长,那么以竖边所在的垂直于x轴的直线即是扫描线。每次移动到一条新的竖边的时候,我们需要计算所在竖边扫描线上有用边长(即当前竖边的有用部分,可能当前的竖边被覆盖部分),以及加上当前扫描线与上一条扫描线之前的横边长 * 横边条数,一直计算到最后一条竖边,即是完整周长了。
 
代码如下:
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=;
struct Line{
int x,y1,y2;
int flag;
bool operator<(const Line s)
{
if(x==s.x) return flag>s.flag;
return x<s.x;
}
}line[N*]; struct Tree
{
int l,r;
bool lf,rf; ///左右边界点是否被覆盖;
int cover_len;
int cover_num;
int num; ///矩形数目;
}tr[N*]; vector<int>v; void build(int l,int r,int i)
{
tr[i].l=l; tr[i].r=r;
tr[i].cover_len=;
tr[i].cover_num=;
tr[i].num=;
tr[i].lf=tr[i].rf=false;
if(l+==r) return ;
int mid=(l+r)>>;
build(l,mid,i<<);
build(mid,r,i<<|);
}
void process(int i)
{
if(tr[i].cover_num>)
{
tr[i].cover_len=v[tr[i].r]-v[tr[i].l];
tr[i].lf=tr[i].rf=true;
tr[i].num=;
return ;
}
if(tr[i].l+==tr[i].r)
{
tr[i].cover_len=;
tr[i].num=;
tr[i].lf=tr[i].rf=false;
return ;
}
int ls=(i<<);
int rs=(i<<|);
tr[i].cover_len=tr[ls].cover_len+tr[rs].cover_len;
tr[i].num=tr[ls].num + tr[rs].num - (tr[ls].rf & tr[rs].lf);
tr[i].lf=tr[ls].lf; tr[i].rf=tr[rs].rf;
}
void update(int i,Line t)
{
if(t.y1<=v[tr[i].l] && v[tr[i].r]<=t.y2)
{
tr[i].cover_num+=t.flag;
process(i);
return ;
}
int mid=(tr[i].l+tr[i].r)>>;
if(t.y1<v[mid]) update(i<<,t);
if(t.y2>v[mid]) update(i<<|,t);
process(i);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
v.clear();
for(int i=;i<n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[i*].x=x1; line[i*].y1=y1; line[i*].y2=y2; line[i*].flag=;
line[i*+].x=x2; line[i*+].y1=y1; line[i*+].y2=y2; line[i*+].flag=-;
v.push_back(y1);
v.push_back(y2);
}
sort(line,line+*n);
sort(v.begin(),v.end());
int num=unique(v.begin(),v.end())-v.begin(); build(,num-,);
int ans=,len=;
for(int i=;i<*n;i++)
{
if(i>) ans+=tr[].num**(line[i].x-line[i-].x);
update(,line[i]);
ans+=abs(tr[].cover_len-len);
len=tr[].cover_len;
}
printf("%d\n",ans);
}
return ;
}

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. leetcode 算法分类

    来源  https://www.bilibili.com/video/av42947553 advance 二分查找 bst dp  敲黑板 重点 图 graph 查找  search 很多都和DP吻 ...

  2. core里使用log4net

    1. nuget 里安装 log4net 2. startup.cs里配置读取配置文件 public static ILoggerRepository repository { get; set; } ...

  3. 编写一个javscript函数 fn,该函数有一个参数 n(数字类型),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。

    function fn(n){ if(n<2 || n>32) { return; }  if(!n) { return;}  //判断n是否为数字  if(!/^[0-9]+.?[0-9 ...

  4. 【Selenium】【BugList7】执行driver.find_element_by_id("kw").send_keys("Selenium"),报错:selenium.common.exceptions.InvalidArgumentException: Message: Expected [object Undefined] undefined to be a string

    [版本] selenium:3.11.0 firefox:59.0.3 (64 位) python:3.6.5 [代码] #coding=utf-8 from selenium import webd ...

  5. "车羊门问题"作业

    作业完成人: 学号:20171301008 潘松泉: 学号:20171301022 陈霖彬: 1.按照你的第一感觉回答,你觉得不换选择能有更高的几率获得汽车,还是换选择能有更高的几率获得汽车?或几率没 ...

  6. 两条比较实用的mysql导入导出命令

    开发lamp程序,对mysql数据库的导入导出是经常的事情,我就遇到这个问题,不能很方便的将数据库导入导出.今天整理了两条比较实用的命令,轻松搞定导入导出问题. 首先是导出命令 1.导出数据库 mys ...

  7. LOJ-10092(最大半连通子图)

    题目连通:传送门 思路: 题目定义很清晰,然后就不会了QAQ…… 后来看了书,先缩点,然后再用拓扑排序找到最长的链子的节点数(因为缩点后所有点都是一个强连通分量,所以找最长的链子就是最大限度包含 点的 ...

  8. VS2017离线安装入门与出家

    重做系统,并且VS2017也发布有一段时间了,可以试试了. 于是网上搜了下,离线安装要下载他的安装工具. https://www.visualstudio.com/zh-hans/downloads/ ...

  9. linux时区和时间设置

    1,修改时区 调整时区使用tzselect [root@lyn ~]# hwclock Tue Nov :: PM AST -0.198205 seconds [root@lyn ~]# tzsele ...

  10. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...