先对数据进行处理,用 $d$ 表示到最下面的锯木厂的距离,对 $w$ 做前缀和。 $$ d _ i = \sum _ {j = 1} ^ n d _ j $$
$$ s _ i = \sum _ {i = 1} ^ i w _i $$
由题意可以得到答案为 $$ \begin {aligned} & \min _ {a < b} \{\sum _ {i = 1} ^ a w _ i(d _ i - d _ a) + \sum _ {i = a + 1} ^ b w _ i(d _ i - d _ b) + \sum _ {i = b +1} ^ n w _ id _ i \} \\ = & \sum _ {i = 1} ^ n w _ i * d _ i - \max _ {a < b} \{\sum _ {i = 1} ^ a w _ i d _ a + \sum _ {i = a + 1} ^ b w _ i d _ b \} \\ = & \sum _ {i = 1} ^ n w _ i * d _ i - \max _ {a < b} \{ s _ a d _ a + (s _ b - s _ i) d _ b \} \\ \end {aligned} $$ 如果现在确定了第二个锯木厂 $i$ ,则当前最小费用为 $\displaystyle \sum _ {i = 1} ^ n w _ i * d _ i -$ $$ f _ i = \max _ {j <i} \{ s _ j d _ j + (s _ i - s _ j) d _ i \} $$ 将其化为斜率优化的一般形式: $$ f _ i - s _ i d _ i = \max _ {j <i} \{ s _ j d _ j - s _ j d _ i \} $$ 维护上凸包即可。