[英]Query min value at x of many equations in the form |x-a| + b
Kind of unrelated to what I was doing, but I stumbled on this interesting data structure.有点与我正在做的事情无关,但我偶然发现了这个有趣的数据结构。 I can't figure out a few details.
我想不通一些细节。
Suppose I have like N<10^5 equations in the form y = |xa|假设我有 N<10^5 形式的方程 y = |xa| + b, with 0 < x, a, b < 10^5.
+ b, 0 < x, a, b < 10^5。 After O(N) preprocessing, for any x, I want to find the minimum value out of all these equations in constant time.
在 O(N) 预处理之后,对于任何 x,我想在恒定时间内从所有这些方程中找到最小值。
Here's what I have so far: add the functions one by one in increasing values of a, somehow find the intersection between the new function and the min existing function , then store intersections.到目前为止,这是我所拥有的:将函数一个一个地添加到 a 的递增值中,以某种方式找到新的 function 和最小的现有 function 之间的交集,然后存储交集。 The intersections can tell us what equation to use for the min.
交叉点可以告诉我们使用什么方程来计算最小值。 Then we can use binary search for log time queries, but since x<10^5, we can just go through all values and precompute the correct min value for each x
然后我们可以对日志时间查询使用二进制搜索,但是由于 x<10^5,我们可以通过所有值只 go 并预先计算每个 x 的正确最小值
Visualization, where the black is the min values可视化,黑色是最小值
The basic property of modulus is模量的基本性质是
1. |x| = x if x>=0
2. |x| = -x if x<0
Using this, the equation y = |xa|+b
can be written as使用这个,方程
y = |xa|+b
可以写成
1. y = x + (b-a) if x >= a
2. y = (b+a) - x if x<a
Since b
and a
are constants, we can use pre-computation to get answers per query in constant time.由于
b
和a
是常数,我们可以使用预计算在常数时间内获得每个查询的答案。
For a given x
, there are two cases:对于给定的
x
,有两种情况:
x
(ie a < x
).x
的左侧(即a < x
)。 If we know the minimum value of ba
for all lines with a < x
, we can get the answer in O(1) time.a < x
的所有行的ba
的最小值,我们可以在 O(1) 时间内得到答案。x
(ie a >= x
), if we know the minimum value of b+a
for all lines with a >= x
, we can get the answer in O(1) time.x
的右侧(即a >= x
)时,如果我们知道所有a >= x
行的b+a
的最小值,我们可以在 O(1) 时间内得到答案。 To summarise, we need to know the minimum value of ba
for all a
on the left of x
and the minimum value of b+a
for all a
on the right of x
.总而言之,我们需要知道
x
左侧所有a
的ba
最小值和x
右侧所有a
的b+a
最小值。 This can easily be done using a simple prefix and/or suffix array implementation.这可以使用简单的前缀和/或后缀数组实现轻松完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.