DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Dadas and Queries

Back to problem

Editorial

Set A0=U0=V0=0A_0=U_0=V_0=0A0​=U0​=V0​=0. Let UiU_iUi​ be the number of operations of size 111 covering position iii, and let ViV_iVi​ be the number of operations of size XXX covering position iii. Then

Ui+XVi=Ai,U_i+XV_i=A_i,Ui​+XVi​=Ai​

where UiU_iUi​ and ViV_iVi​ are nonnegative integers.

For fixed sequences U,VU,VU,V, the required number of operations is

∑i=1Nmax⁡(0,Ui−Ui−1)+∑i=1Nmax⁡(0,Vi−Vi−1).\sum_{i=1}^{N}\max(0,U_i-U_{i-1})+\sum_{i=1}^{N}\max(0,V_i-V_{i-1}).i=1∑N​max(0,U

This value is attainable by starting new intervals whenever a count increases and ending existing intervals whenever it decreases.

Let Fi(c)F_i(c)Fi​(c) be the minimum cost for positions 111 through iii when Vi=cV_i=cV. Put . If the previous state is and , the transition cost is

hΔi(e)=max⁡(0,Δi−Xe)+max⁡(0,e).h_{\Delta_i}(e)=\max(0,\Delta_i-Xe)+\max(0,e).hΔi​​(e)=max(0,Δ

The state must also satisfy 0≤c≤⌊Ai/X⌋0\le c\le\lfloor A_i/X\rfloor0≤c≤⌊Ai​/X⌋. Thus, FiF_iFi​ is an integer discrete convex function.

For an integer discrete convex function fff, define its conjugate by

f∗(s)=max⁡z(sz−f(z)).f^*(s)=\max_z(sz-f(z)).f∗(s)=zmax​(sz−f(z)).

For 1≤j≤X+11\le j\le X+11≤j≤X+1, define

Dj(f)=f∗(−X+j)−f∗(−X+j−1).D_j(f)=f^*(-X+j)-f^*(-X+j-1).Dj​(f)=f∗(−X+j

This is the smallest optimal position at slope −X+j-X+j−X+j.

We use the following properties.

  • Conjugates add under infimal convolution, so the values DjD_jDj​ also add.
  • Restricting the domain to an integer interval [0,q][0,q][0,q] changes DjD_jDj to .

Write Δ=Xq+r\Delta=Xq+rΔ=Xq+r with 0≤r<X0\le r<X0≤r<X. Let dj(Δ)=Dj(hΔ)d_j(\Delta)=D_j(h_\Delta). If , then

d1=0,dj=q+[r≥X−j+2](2≤j≤X+1).d_1=0,\qquad d_j=q+[r\ge X-j+2]\quad(2\le j\le X+1).d1​=0,dj​=

If Δ<0\Delta<0Δ<0, then

dj=q+[r≥X−j+1](1≤j≤X),dX+1=0.d_j=q+[r\ge X-j+1]\quad(1\le j\le X),\qquad d_{X+1}=0.dj​=q+[r≥X−

The bracket is 111 when its condition is true and 000 otherwise. These formulas follow by examining the discrete slopes of hΔ(e)−(−X+j)eh_\Delta(e)-(-X+j)ehΔ​(e)−(−X+.

Let zj=Dj(Fi−1)+dj(Δi)z_j=D_j(F_{i-1})+d_j(\Delta_i)zj​=Dj​(F and . Then

Dj(Fi)=clamp⁡(zj,0,qi).D_j(F_i)=\operatorname{clamp}(z_j,0,q_i).Dj​(Fi​)=clamp(z

If αi=Fi∗(−X)\alpha_i=F_i^*(-X)αi​=Fi∗​(−X), then

αi=αi−1−Δi+∑j=1X+1min⁡(zj,0).\alpha_i=\alpha_{i-1}-\Delta_i+\sum_{j=1}^{X+1}\min(z_j,0).αi​=αi−1​−

We always have D1(Fi)=0D_1(F_i)=0D1​(Fi​)=0 and DX+1(Fi)=qiD_{X+1}(F_i)=q_iD. The state never adds a negative term to . Therefore, only the states must be stored. The contribution of state can be included in a position-dependent constant.

Fix one stored state jjj. One position transforms an input state xxx as

y=clamp⁡(x+d,0,q)y=\operatorname{clamp}(x+d,0,q)y=clamp(x+d,0,q)

and adds min⁡(x+d,0)\min(x+d,0)min(x+d,0) to α\alphaα.

The effect of any contiguous segment can be represented by five integers:

y=clamp⁡(x+a,l,r),t=min⁡(x+p,c),y=\operatorname{clamp}(x+a,l,r),\qquad t=\min(x+p,c),y=clamp(x+a,l,r),t=min(x+p

where ttt is the segment's contribution to α\alphaα. A single position has summary

(a,l,r,p,c)=(d,0,q,d,0).(a,l,r,p,c)=(d,0,q,d,0).(a,l,r,p,c)=(d,0,q,d,0).

Suppose the left summary is (a1,l1,r1,p1,c1)(a_1,l_1,r_1,p_1,c_1)(a1​,l1​,r1​, and the right summary is . Applying the left segment and then the right segment gives

a=a1+a2,l=clamp⁡(l1+a2,l2,r2),r=clamp⁡(r1+a2,l2,r2),p=p1+min⁡(l1+p2,c2),c=

The states are independent, so merging two segment tree nodes takes O(X)O(X)O(X) time.

At the root, every input state is 000. The final value of a state is clamp⁡(a,l,r)\operatorname{clamp}(a,l,r)clamp(a,l,r), and its contribution to α\alphaα is min⁡(p,c)\min(p,c)min(p,c). Finally, output

min⁡cFN(c)=−FN∗(0)=−(αN+∑j=2XDj(FN)).\min_c F_N(c)=-F_N^*(0)=-\left(\alpha_N+\sum_{j=2}^{X}D_j(F_N)\right).cmin​FN​(c)

Read all queries in advance. If a position ppp is ever updated, only transitions ppp and p+1p+1p+1 can change. Let SSS be the set of all such transition positions. Then ∣S∣≤2Q|S|\le2Q∣S∣.

Positions outside SSS never change. Initially merge each maximal contiguous segment of such positions into one summary, while every position in SSS remains a summary of length 111. If the compressed sequence has length KKK, then

K≤2∣S∣+1≤4Q+1.K\le2|S|+1\le4Q+1.K≤2∣S∣+1≤4Q+1.

Build a segment tree on this compressed sequence. An update rebuilds only the compressed leaves corresponding to transitions ppp and p+1p+1p+1.

The preprocessing time is O(X(N+Q)+Qlog⁡Q)O(X(N+Q)+Q\log Q)O(X(N+Q)+QlogQ), the time per query is O(Xlog⁡Q)O(X\log Q)O(XlogQ), and the memory usage is .

Solution written by GPT5

,
i​
−
Ui−1​)+
i=1∑N​max(0,Vi​−
Vi−1​).
i
​
=
c
Δi=Ai−Ai−1\Delta_i=A_i-A_{i-1}Δi​=Ai​−Ai−1​
ddd
e=c−de=c-de=c−d
i​
−
Xe)+
max(0,e).
)
−
f∗(−X+
j−
1).
​
clamp⁡(Dj,0,q)\operatorname{clamp}(D_j,0,q)clamp(Dj​,0,q)
  • hΔ∗(−X)=−Δh_\Delta^*(-X)=-\DeltahΔ∗​(−X)=−Δ.
  • dj​(Δ)=
    Dj​(hΔ​)
    Δ≥0\Delta\ge0Δ≥0
    q
    +
    [r≥
    X−
    j+
    2](2≤
    j≤
    X+
    1).
    j
    +
    1](1≤
    j≤
    X),dX+1​=
    0.
    j
    )
    e
    i
    −
    1
    ​
    )
    +
    dj​(Δi​)
    qi=⌊Ai/X⌋q_i=\lfloor A_i/X\rfloorqi​=⌊Ai​/X⌋
    j
    ​
    ,
    0
    ,
    qi​
    )
    .
    Δi​+
    j=1∑X+1​min(zj​,0).
    X+1​
    (
    Fi​
    )
    =
    qi​
    j=X+1j=X+1j=X+1
    α\alphaα
    X−1X-1X−1
    j=2,3,⋯ ,Xj=2,3,\cdots,Xj=2,3,⋯,X
    j=1j=1j=1
    ,
    c
    )
    ,
    p1​
    ,
    c1​
    )
    (a2,l2,r2,p2,c2)(a_2,l_2,r_2,p_2,c_2)(a2​,l2​,r2​,p2​,c2​)
    c1+min⁡(r1+p2,c2).\begin{align*} a&=a_1+a_2,\\ l&=\operatorname{clamp}(l_1+a_2,l_2,r_2),\\ r&=\operatorname{clamp}(r_1+a_2,l_2,r_2),\\ p&=p_1+\min(l_1+p_2,c_2),\\ c&=c_1+\min(r_1+p_2,c_2). \end{align*}
    alrpc​=a1​+a2​,=clamp(l1​+a2​,l2​,r2=clamp(r1​+a2​,l2​,r2=p1​+min(l1​+p2​,c2​=c1​+min(r1​+p2​,c2​​
    =
    −FN∗​(0)=
    −(αN​+j=2∑X​Dj​(FN​)).
    ≤
    2Q
    O(N+Q+XQ)O(N+Q+XQ)
    O(N+Q+XQ)
    ​
    )
    ,
    ​
    )
    ,
    )
    ,
    )
    .