Set A0β=U0β=V0β=0. Let Uiβ be the number of operations of size 1 covering position i, and let Viβ be the number of operations of size X covering position i. Then
Uiβ+XViβ=Aiβ,
where Uiβ and Viβ are nonnegative integers.
For fixed sequences U,V, the required number of operations is
i=1βNβmax(0,UiββUiβ1β)+i=1βNβmax(0,ViββViβ1β).
This value is attainable by starting new intervals whenever a count increases and ending existing intervals whenever it decreases.
Let Fiβ(c) be the minimum cost for positions 1 through i when Viβ=c. Put Ξiβ=AiββAiβ1β. If the previous state is d and e=cβd, the transition cost is
hΞiββ(e)=max(0,ΞiββXe)+max(0,e).
The state must also satisfy 0β€cβ€βAiβ/Xβ. Thus, Fiβ is an integer discrete convex function.
For an integer discrete convex function f, define its conjugate by
fβ(s)=zmaxβ(szβf(z)).
For 1β€jβ€X+1, define
Djβ(f)=fβ(βX+j)βfβ(βX+jβ1).
This is the smallest optimal position at slope βX+j.
We use the following properties.
- Conjugates add under infimal convolution, so the values Djβ also add.
- Restricting the domain to an integer interval [0,q] changes Djβ to clamp(Djβ,0,q).
- hΞββ(βX)=βΞ.
Write Ξ=Xq+r with 0β€r<X. Let djβ(Ξ)=Djβ(hΞβ). If Ξβ₯0, then
d1β=0,djβ=q+[rβ₯Xβj+2](2β€jβ€X+1).
If Ξ<0, then
djβ=q+[rβ₯Xβj+1](1β€jβ€X),dX+1β=0.
The bracket is 1 when its condition is true and 0 otherwise. These formulas follow by examining the discrete slopes of hΞβ(e)β(βX+j)e.
Let zjβ=Djβ(Fiβ1β)+djβ(Ξiβ) and qiβ=βAiβ/Xβ. Then
Djβ(Fiβ)=clamp(zjβ,0,qiβ).
If Ξ±iβ=Fiββ(βX), then
Ξ±iβ=Ξ±iβ1ββΞiβ+j=1βX+1βmin(zjβ,0).
We always have D1β(Fiβ)=0 and DX+1β(Fiβ)=qiβ. The state j=X+1 never adds a negative term to Ξ±. Therefore, only the Xβ1 states j=2,3,β―,X must be stored. The contribution of state j=1 can be included in a position-dependent constant.
Fix one stored state j. One position transforms an input state x as
y=clamp(x+d,0,q)
and adds min(x+d,0) to Ξ±.
The effect of any contiguous segment can be represented by five integers:
y=clamp(x+a,l,r),t=min(x+p,c),
where t is the segment's contribution to Ξ±. A single position has summary
(a,l,r,p,c)=(d,0,q,d,0).
Suppose the left summary is (a1β,l1β,r1β,p1β,c1β) and the right summary is (a2β,l2β,r2β,p2β,c2β). Applying the left segment and then the right segment gives
alrpcβ=a1β+a2β,=clamp(l1β+a2β,l2β,r2β),=clamp(r1β+a2β,l2β,r2β),=p1β+min(l1β+p2β,c2β),=c1β+min(r1β+p2β,c2β).β
The states are independent, so merging two segment tree nodes takes O(X) time.
At the root, every input state is 0. The final value of a state is clamp(a,l,r), and its contribution to Ξ± is min(p,c). Finally, output
cminβFNβ(c)=βFNββ(0)=β(Ξ±Nβ+j=2βXβDjβ(FNβ)).
Read all queries in advance. If a position p is ever updated, only transitions p and p+1 can change. Let S be the set of all such transition positions. Then β£Sβ£β€2Q.
Positions outside S never change. Initially merge each maximal contiguous segment of such positions into one summary, while every position in S remains a summary of length 1. If the compressed sequence has length K, then
Kβ€2β£Sβ£+1β€4Q+1.
Build a segment tree on this compressed sequence. An update rebuilds only the compressed leaves corresponding to transitions p and p+1.
The preprocessing time is O(X(N+Q)+QlogQ), the time per query is O(XlogQ), and the memory usage is O(N+Q+XQ).
Solution written by GPT5