HDU-1828(扫描线+线段树)

Problem 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.

Please process to the end of file.

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

题目描述:有n个矩形,然后让我们求这n个矩形组成的最终的多边形的边长,样例演示的就是题目中图片的情况,7个图片一那样的矩形最后会形成图片二那样的多边形。

解题思路:边长我们可以理解为至少覆盖了一次的长度。我们用扫描线扫过线段时,如果我们线段的长度发生了改变,那么就表示扫到了一个新的矩形或者离开了这个矩形,那么边长就会发生改变,我们就将这个改变值加上就可以了,这就将我们横向边加上了(如果你的扫描线是从左往右或者从右往左扫的,那么就是计算了所有竖向边的边长对边长的贡献。),那么竖向边怎么计算呢?我们思考一下,扫描线是将这整个图形分成了一层一层的矩形,如果在这一层中有x个矩形,那么竖向边就有2*x条,然后再乘以长度,就是竖向边对边长的贡献啦,对于x的计算就是计算在这一层有多少条线段。用区间合并就可以了,然后今天我发现了一个bug,就是在update的时候,如果有边的高度是相同的,需要先update下边,然后再update上边,因为如果先update上边的话,那么这条边就被加过了,然后再update下边时,就会再加一次这条边,就会错误,开始我一直想不明白,看到一个聚聚的博客看到的,这里附上一个链接:Picture hdu-1828 扫描线求面积+一组hack数据 为什么会wa?有可能是因为这个!两种方法!
代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
using namespace std;
#define int long long
#define ls root<<1
#define rs root<<1|1
const int maxn=1e5+7;
double pos[maxn]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值