简体   繁体   English

以 |xa| 形式查询多个方程在 x 处的最小值 + b

[英]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.由于ba是常数,我们可以使用预计算在常数时间内获得每个查询的答案。
For a given x , there are two cases:对于给定的x ,有两种情况:

  1. The minima lies to the left of 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) 时间内得到答案。
  2. Similarly, when the minima lies on the right of 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左侧所有aba最小值和x右侧所有ab+a最小值。 This can easily be done using a simple prefix and/or suffix array implementation.这可以使用简单的前缀和/或后缀数组实现轻松完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 寻找 f(x) = a*min(b, x) 形式的函数最大值的算法? - Algorithm for finding max value of functions of the form f(x) = a*min(b, x)? 是否可以微观优化“x = max(a,b); y = min(a,b);“? - Is it possible to micro-optimize “x = max(a,b); y = min(a,b);”? 形式x ^ a + b的数字的素数分解,其中x是素数 - Prime Factorization of numbers of form x^a + b where x is prime 从形式为b ^ x的数字中找到一个数字的最小绝对差,其中b,x&gt; 1 - Find the minimum absolute difference of a number from a number which in the form b^x where b,x > 1 如何用方程式要求解决这个3x3矩阵 - How to resolve this 3x3 matrix with equations requirements 找到a,b和c的值,使得X ^ a * Y ^ b * Z ^ c最接近N,而a + b + c最小 - Find value of a, b and c such that X^a * Y^b * Z^c is closest to N and a+b+c is mimimum 递归计算 x 是否为 b 的幂 - recursively calculate if x is power of b 对于给定的两个整数A和B,找到一对数字X和Y,使得A = X * Y和B = X x或Y - For given two integers A and B, find a pair of numbers X and Y such that A = X*Y and B = X xor Y PYTHON:解决 y=a+b*x 其中 x 是预定义列表 - PYTHON: solving y=a+b*x where x is a predefined list 连续 x 没有。 A,然后在 (nx) 个 B 之后 - Continuous x no. of A and then after (n-x) no.s of B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM