P4231 三步必杀 题解

· · 题解

感觉题解区讲的并不清楚,因此为增进对此题的理解写了一篇题解,并无要提交的意愿。

题意不再赘述。

由于等差数列差分后为 \{s,d,d,d,...,-e\},s,d,e 分别为首项、公差、末项 \left(d=\dfrac{e-s}{r-l}\right),考虑对本题所求的序列 a 进行差分。目标序列为

a=\begin{Bmatrix} \text{value} & ... & 0 & s & s+d & s+2d & ... & e & 0 & 0 & ... &\\ \text{index} & ... & l-1 & l & l+1 & l+2 & ... & r & r+1 & r+2 & ... & \end{Bmatrix}

差分,得

\Delta a=\begin{Bmatrix} \text{value} & ... & 0 & s & d & d & ... & d & -e & 0 & ... &\\ \text{index} & ... & l-1 & l & l+1 & l+2 & ... & r & r+1 & r+2 & ... & \end{Bmatrix}

显然,再次差分可约去一些 d。对序列 \Delta a 再次差分,得

\Delta a'=\begin{Bmatrix} \text{value} & ... & 0 & \color{red}s & \color{red}d-s & 0 & ... & 0 & \color{red}-e-d & \color{red}e & ... &\\ \text{index} & ... & l-1 & \color{red}l & \color{red}l+1 & l+2 & ... & r & \color{red}r+1 & \color{red}r+2 & ... & \end{Bmatrix}

只有 l,l+1,r+1,r+2 四项需要修改,其余均为 0。因此对于每次操作 (l,r,s,e),设 d=\dfrac{e-s}{r-l},该操作等价于修改二次差分序列:

\begin{aligned} \Delta a'_{\color{red}l} \gets & \Delta a'_{\color{red}l}+\color{red}s \\ \Delta a'_{\color{red}l+1} \gets & \Delta a'_{\color{red}l+1}+\color{red}d-s \\ \Delta a'_{\color{red}r+1} \gets & \Delta a'_{\color{red}r+1}\color{red}-e-d\\ \Delta a'_{\color{red}r+2} \gets & \Delta a'_{\color{red}r+2}+\color{red}e \end{aligned}

最后对 \Delta a' 做两次前缀和即可得到原序列 a。复杂度是线性的。