POJ 1177 PICTURE

本文介绍了一种计算多个矩形拼接后形成的不规则图形周长的方法,利用线段树数据结构处理矩形边界,实现高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Picture
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 5093 Accepted: 2644

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

 

/*

第一个线短树, 对于输入矩形,将其转换为所有竖边进行存储,然后将竖边进行排序

线段树的存储按照所有点y轴坐标递增序列的下标来组织

 

然后一次处理所有竖线,如果是矩形的左竖边,则将其加入线段树,如果是矩形的右竖边,则从线段树中减去

(注意,amountLen是指区域的覆盖长度,而不是总长度,即重复的区域只统计一次),然后统计当前值,当前

值的统计分两块:(1)竖线长度 = 当前的amountLen - 上一个amountLen (2)横线长度 = (当前竖线x坐标 - 上一个竖线x坐标)× 当前的段总数 × 2; 最后 总长+= (1) + (2)即可

 

注意这道题的与star之类的题的不同点在于,star的amountLen是总长度,即包括重复区域,而这道题的总长度是覆盖长度,而不包括区域长度的重复统计

*/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值