- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试了解可用于计算一组轴对齐矩形的并集面积的算法。
我遵循的解决方案在这里:http://tryalgo.org/en/geometry/2016/06/25/union-of-rectangles/
我不明白的部分是:
The segment tree is the right choice for this data structure. It has complexity O(logn) for the update operations and O(1) for the query. We need to augment the segment tree with a score per node, with the following properties.
- every node corresponds to a y-interval being the union of the elementary y-intervals over all the indices in the span of the node.
- if the node value is zero, the score is the sum of the scores of the descendants (or 0 if the node is a leaf).
- if the node value is positive, the score is the length of the y-interval corresponding to the node.
我们如何在 O(n log n) 中实现这一目标?
我的想法是创建一个线段树,并在扫线时遇到范围(y 范围为矩形的高度)时更新每个范围的值。然后对于每个区间(排序后的 x 数组中的两个连续元素,通过查看线段树中所有元素的总和,将 Δx 乘以该区间中活跃的 y 范围的总长度)
这仍然会导致我们在线段树的基础中有 max(y) - min(y) 个元素。
因此,我不确定这是 O(n log n) - 其中 n 是矩形的数量。
非常感谢此处的任何帮助。
谢谢!
最佳答案
根据您的理解,您将在基础上创建具有 11 - 1 = 10 个节点的线段树,因此如下所示:
注意我们在 base 中只有 9 个节点,因为第一个节点用于区间 [1,2],下一个用于区间 [2,3] 等等
当您输入某个矩形时,您会根据它的 y 坐标更新它的范围,因此在 x=0 上遇到第一个矩形后,您的线段树将如下所示:
我们还需要使用一种叫做惰性传播的东西来更新树上的事件间隔,因此所有事件间隔都会对总和贡献 1。
因此,您当前方法的复杂性类似于 O(K log K),其中 K = max(y)-min(y)
我们可以轻松地将其减少到 O(n log n),其中 n 是矩形的数量。
请注意,只有重要的 y 坐标是存在的,因此在本例中为 1,3,6,11
还要注意最多有 2*n 个这样的坐标
因此我们可以将所有坐标映射到一些整数,以便它们更好地适合线段树。
这被称为坐标压缩,它可以通过这样的方式完成:
因此在我们的示例中它将是:
[1,3,6,11]
[1,3,6,11]
mp[1]=1, mp[3]=2, mp[6]=3, mp[11]=4
所以现在算法保持不变,但我们可以使用其基础中最多只有 2*n 个节点的线段树。
我们还需要稍微修改一下线段树,我们现在将保持 y 坐标的间隔开/关,而不是保持 y 坐标打开或关闭
所以我们将有间隔 [y0,y1],[y1,y2], ... 的节点,用于所有唯一的排序值 y。
此外,所有节点都将为总和贡献 y[i]-y[i-1](如果它们在范围内且处于事件状态)而不是一个。
所以我们的新线段树应该是这样的:
关于algorithm - 使用线段树的矩形并集面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55702005/
我正在开发一个企业名录网站,其搜索将由 Google map 驱动。用户将能够根据各种标准搜索他们所在地区的企业,但主要的想法是,如果您搜索例如“新泽西州的水管工”,您将获得新泽西州所有水管工的结果。
我得到了一条任意形状的曲线,包围了一些区域。 我想估计曲线在 iPhone/iPad 屏幕上包围的像素数。我该怎么做? 曲线被定义为点的连续 x/y 坐标。 闭合曲线。 通过用户的触摸(touches
我想删除 R 在点阵图周围的默认边距。这意味着我想摆脱红色矩形之外的所有空白。这是示例: library (raster) library(rasterVis) f <- system.file("e
无法找到任何直接的解决方案来计算 GMSPolygon 对象面积。有什么方法可以做到这一点,或者我必须用边长和一些数学计算来计算它? 最佳答案 感谢@Larme; GMSGeometryArea 就是
假设例如我想将标准正态分布的密度曲线下方的面积着色为十分。我希望最左边 10% 的区域具有与接下来的 10% 不同的阴影,依此类推。 这是问题“Shading a kernel density plo
我正在为 Extjs 开发一个混合图表组件,并且曲线太尖锐了。我找不到曲线具有半径的配置。如果你处理过类似的事情,你能提供一些方法让我的曲线变得平滑一点吗?这是我的代码: Ext.define('Ex
上下文 我有一个 3D 对象,我有它的坐标。然后我将对象旋转 n 次,当对象投影到网格上时,我想计算对象的 2D 面积(以纳米为单位)。 例如, 我在下面有一张图片描述了我的问题。我有相同的对象,但在
当我知道我需要的地 block 总数并且我希望排列是一个正方形(可能有一些空的子地 block )时,我正在尝试弄清楚如何计算子地 block 尺寸。 例如,如果我需要 22 个子图,那么我会为总共
我是一名数据科学家。主要使用Python和SQL来编写代码。我使用data studio进行可视化。所以我对JS不熟悉。我的诀窍data studio community visualizations
我有 1797 张 Mnist 图像,为此我需要提取两个特征(FilledArea、EulerNumber)。我知道如何在 Matlab 中做到这一点。我的特征矩阵在 Matlab 中具有(并且是正确
我希望能够在 Google map 上绘制形状(圆形、多边形和矩形),但我想限制可以绘制的形状的大小(面积)。因此,以圆圈为例,期望的行为是当用户开始从 map 上的某个点拖动鼠标以形成圆圈时,圆圈会
我是一名优秀的程序员,十分优秀!