热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

题目:图像处理(HDU1828,计算周长并集,利用线段树与离散化技术进行扫描)

//http://acm.hdu.edu.cn/showproblem.php?pid=1828http://acm.hdu.edu.cn/webcontest/contest_showpro

/

/http://acm.hdu.edu.cn/showproblem.php?pid=1828

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1003&ojid=0&cid=5361&hide=0

Picture

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 1

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 <&#61; 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

 

Source

IOI 1998

 

Statistic | Submit | Back

线段树&#43;扫描线&#xff1b;

如果坐标是double 类型的话需要将其变离散化

总体思想&#xff1a;看了诸多大神的博客之后才有一点点感觉

1&#xff0c;将每个矩形用两条线段表示&#xff0c;并且这些线段按照x坐标从小到大顺序排列好&#xff0c;这样就把一个个矩形变成了一条条线段

2.y坐标存入一组数据中&#xff0c;并且按照从小到大顺序排序之后&#xff0c;构建树状数组&#xff1b;

3&#xff0c;最重要的就是对树状数组进行维护和查询工作&#xff0c;主要是维护&#xff0c;覆盖次数和有效长度

这是难点也是重点

Submit Time Judge Status Exe.Time Exe.Memory Language

2013-07-27 15:46:44 Accepted 15 MS 1404 KB GNU C&#43;&#43;

解析&#xff1a;

求周长并&#xff1a;

线段树&#43;离散话&#43;扫描&#xff1b;

下面是搬了模板的

*/

#include
#include
#include
#include
#include
using namespace std;
const int maxn&#61;5000&#43;10;
const int inf&#61;100000000;
struct node
{
int s,t,m,lb,rb;//m表示有效投影长度
int sl;
int cnt;//表示被覆盖次数
}tr[maxn*8];
struct Line
{
int x,y1,y2;
bool d;//标记矩形的左右边
}line[maxn*2];
void build(int k,int s,int t)//建树
{
tr[k].s&#61;s;
tr[k].t&#61;t;
tr[k].m&#61;tr[k].lb&#61;tr[k].rb&#61;0;
tr[k].sl&#61;tr[k].cnt&#61;0;//附加值初始化为0
if(t-s>1)
{
int mid&#61;(s&#43;t)/2;
build(k*2&#43;1,s,mid);
build(k*2&#43;2,mid,t);
}
}
bool cmp(Line a,Line b)//为了便于将坐标从左到右排列
{
return a.x}
void update(int k).//更新结点k
{
if(tr[k].cnt>0)//当存在覆盖时{tr[k].m&#61;tr[k].t-tr[k].s;tr[k].lb&#61;tr[k].rb&#61;1;tr[k].sl&#61;1;return;}if(tr[k].t-tr[k].s&#61;&#61;1)//到达叶子结点时清0{tr[k].m&#61;0;tr[k].lb&#61;tr[k].rb&#61;0;tr[k].sl&#61;0;}else{//当前结果为左右子树之和int ll&#61;k*2&#43;1;int rr&#61;k*2&#43;2;tr[k].m&#61;tr[ll].m&#43;tr[rr].m;
tr[k].sl&#61;tr[ll].sl&#43;tr[rr].sl-(tr[ll].rb&tr[rr].lb);tr[k].lb&#61;tr[ll].lb;tr[k].rb&#61;tr[rr].rb;}
}
void insert(int s,int t,int k)
{
if(s<&#61;tr[k].s&&t>&#61;tr[k].t)//当区间被覆盖时
{
tr[k].cnt&#43;&#43;;
update(k);//一旦发生变化需要进行维护
return;
}
int mid&#61;(tr[k].s&#43;tr[k].t)/2;
if(sif(t>mid)insert(s,t,k*2&#43;2);
update(k);
}
void Delete(int s,int t,int k)
{
if(s<&#61;tr[k].s&&t>&#61;tr[k].t)//同插入操作
{
tr[k].cnt--;
update(k);
return;
}
int mid&#61;(tr[k].s&#43;tr[k].t)/2;
if(sif(t>mid)Delete(s,t,k*2&#43;2);
update(k);
}
void cal(int n)//求周长并
{
int i,t2,sum&#61;0;
t2&#61;0;
line[n]&#61;line[n-1];
for(i&#61;0;i{
if(line[i].d&#61;&#61;1)//右边时加入
insert(line[i].y1,line[i].y2,0);
else//当属于矩形左边点时删除
Delete(line[i].y1,line[i].y2,0);
sum&#43;&#61;tr[0].sl*(line[i&#43;1].x-line[i].x)*2;
sum&#43;&#61;abs(tr[0].m-t2);
t2&#61;tr[0].m;
}
printf("%d\n",sum);
}
int main()
{
int i,j,n;
while(scanf("%d",&n)!&#61;EOF)
{ int x1,x2,y1,y2;
j&#61;0;
int max&#61;-inf,min&#61;inf;
for(i&#61;0;i{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[j].x&#61;x1;//线段的坐标与线段树每个结点的区间范围相对应
line[j].y1&#61;y1;
line[j].y2&#61;y2;
line[j].d&#61;1;
line[&#43;&#43;j].x&#61;x2;
line[j].y1&#61;y1;
line[j].y2&#61;y2;
line[j].d&#61;0;
j&#43;&#43;;
if(min>y1)min&#61;y1;
if(max}
sort(line,line&#43;j,cmp);
build(0,min,max);
cal(j);
}
return 0;
}






推荐阅读
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文介绍如何使用Java中的正则表达式来提取字符串中的特定值。通过示例代码和详细解释,帮助开发者掌握正则表达式的使用方法,尤其是如何匹配和提取复杂模式中的数据。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 本文介绍如何解决在 IIS 环境下 PHP 页面无法找到的问题。主要步骤包括配置 Internet 信息服务管理器中的 ISAPI 扩展和 Active Server Pages 设置,确保 PHP 脚本能够正常运行。 ... [详细]
  • 尽管某些细分市场如WAN优化表现不佳,但全球运营商路由器和交换机市场持续增长。根据最新研究,该市场预计在2023年达到202亿美元的规模。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
author-avatar
可爱沉默999
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有