Start by scaling the axis to get rid of factors a and b . Define x' = a * x, y' = b * y' . Then the distance:
max(a*|x1-x2|,b*|y1-y2|) = max(|a*x1-a*x2|,|b*y1-b*y2|) = max(|x'1-x'2|,|y'1-y'2|)
Secondly, rotate the coordinate system 45 degrees to change it to taxi geometry. Define s = (x' + y')/2, t = (x' - y')/2 . Then we have x' = s + t, y' = s - t .
Then we can rewrite the definition of distance again:
max(|x'1-x'2|,|y'1-y'2|) = max(|s1 + t1 - s2 - t2|,|s1 - t1 - s2 + t2|) = max(|(s1 - s2) + (t1 - t2)|,|(s1 - s2) - (t1 - t2)|) = |s1 - s2| + |t1 - t2| -- last equation comes from the fact that max(|a + b|, |a - b|) = |a| + |b|
Using this definition, we can separately summarize the distances along the s axis and separately along the t axis and add the results.
The solution to the one-dimensional version of this problem is quite simple. You sort the points along the axis. Then each segment between the (0-based) i -th and i+1 1th point will contribute to (i + 1) * (N - i - 1) * distance . This is enough to summarize these values.
The general decision is made by O(n lg n) , since he needs to sort the points twice.
source share