感觉这道题没有什么难想到的地方,为什么会当场0ac。。。。。
显然dp,设(f_i)表示前缀(i)最小代价考虑以一个(1)连续段和插入一个(0)划分状态。
1.(f_i=max(f_i,f_{i-1}))
2.(f_i=max(f_i,f_j+frac{(j-i+1)(j-i)}{2})-s_{i-1}+s_j),(s)是(a)的前缀和。
把后面括号拆开后就是经典的半平面交斜率优化。
如果有修改,考虑枚举中间的点是否选择。
如果不选择代价显然是(f_{x-1}+g_{x+1}),其中(g)是后缀dp数组。
否则考虑枚举中线延伸到左/右的(l,r),代价是(f_{l-1}+g_{r+1}+frac{(r-l+1)(r-l)}{2}-s_r+s_l-a_x+v),(v)是修改值。
事实上可以把(max)的(v-a_x)值拿出来,这样子我们只需要求(f_{l-1}+g_{r+1}+frac{(r-l+1)(r-l)}{2}-s_r+s_l)
考虑经典的过中线分治,钦定区间选择(md,md+1),到分治树叶子节点特判。
考虑从左往右扫数组,求出以(i)为左端点的最大值,右端点同理。
然后在一个数组(a)上打标记,如果(i)处最大值为(v),然后(a_i=max(v,a_{i-1}))
这事实上是(f_{i-1}+g_{r+1}+frac{(r-l+1)(r-l)}{2}-s_r+s_i)
拆开括号后依旧是个半平面交,可以先插入([md+1,r])的所有直线后查询最大值。
右端点为(r)同理。
时间复杂度(O(nlog_2n))