Editorial: Amanojaku and Sequence (Easy Version)
E1 is the same combinatorial core as E0, but you are given a subarray $b=[a_l,\ldots,a_r]$ and a single query per test ($q=1$). We build the solution in three steps:
- All $-1$, full array — warmup $G(n,m)$.
- All $-1$, subarray — answer depends only on length.
- Fixed and free — DP on the subarray.
If $b=(-1,\ldots,-1)$ has length $n$, every valid $c$ is a composition of $m$ into $n$ non-negative parts. Define
$$G(n,m)=\sum_{c_1+\cdots+c_n=m} f(c),\qquad f(c)=\sum_{i=1}^{n} P_i^2,\quad P_i=\sum_{j=1}^{i}c_j.$$Then $g(b,m)=G(n,m)$. The four-table DP from E0 applies: for each $s\in[0,m]$, maintain $\mathrm{cnt}$, $\mathrm{pref}$, $\mathrm{pref2}$, $\mathrm{sq}$; one free position is a layer $T$; answer $G(n,m)=\mathrm{sq}[m]$.
See E0 editorial for the full transition formulas.
If every $a_i=-1$ on the whole array, any subarray $[a_l,\ldots,a_r]$ is $L=r-l+1$ copies of $-1$. Valid $c$ for the subarray are compositions of $m$ into $L$ parts, so
$$g([a_l,\ldots,a_r],m)=G(L,m).$$The sample block with $a=(-1,-1,-1,-1)$ and queries $(l,r,m)=(1,1,4),(1,2,5),(1,3,6),(1,4,7)$ returns $G(1,4)=16$, $G(2,5)=205$, $G(3,6)=1736$, $G(4,7)=12180$.
Let $R=m$ (no fixed values). A naïve free step loops $v$ for each $s$ and costs $O(R^2)$. It can be done in $O(R)$ using prefix sums:
- $\mathrm{cnt}'[k]=\sum_{s\le k}\mathrm{cnt}[s]$.
- $\mathrm{pref}'[k]=\sum_{s\le k}(\mathrm{pref}[s]+\mathrm{cnt}[s]\cdot(k-s))$.
- $\mathrm{pref2}'$ and $\mathrm{sq}'$ use the same prefix structure (see
model_sol.cpp).
When the subarray is all $-1$, you need $T^L(\mathrm{init})$ — apply the free layer $L$ times. Binary exponentiation on the map $T$:
- keep maps
mulandres(composition of layers); - if $L$ has a bit set,
res ← res ∘ mul; mul ← mul ∘ mul;- answer from
res(init).
Each composition is two $O(R)$ applications, so $O(R\log L)$ per all-free query. With $m\le 10^6$, $R\le m$ and this fits E1 limits.
For general $b=[a_l,\ldots,a_r]$:
$$F=\sum_{b_i\ge 0} b_i,\qquad R=m-F.$$If $R<0$, no valid $c$. Otherwise DP only on sums $s=0,\ldots,R$ (mass still to place on $-1$ slots).
Scan $b$ left to right (same as E0 on the whole array):
| Type | Effect on $s$ | Cost |
|---|---|---|
| $b_i=-1$ | free layer $T$ | $O(R)$ |
| $b_i=v\ge 0$ | add $v$ to prefix, same $s$ | $O(R)$ |
Fixed $v$ at sum $s$ (mass $c=\mathrm{cnt}[s]$):
$$\mathrm{sq}[s]\mathrel{+}=\mathrm{pref2}[s]+2v\,\mathrm{pref}[s]+c\,v^2,$$then update $\mathrm{pref}[s]$, $\mathrm{pref2}[s]$.
Free slot: use the $O(R)$ layer from Step 2.
Answer: $\mathrm{sq}[R]$ modulo $998\,244\,353$.
$F=6$, $R=2$. After fixed $6$: one assignment with prefix $6$, $\mathrm{sq}[0]=36$. One free layer gives $c=[6,2]$, $\mathrm{sq}[2]=100$.
$F=3$, $R=2$. Fixed $3$ in the middle, then free/free around it; answer $286$.
- Read $n$, array $a$, query $2\;l\;r\;m$.
- Build $b=a_l,\ldots,a_r$, $L=|b|$.
- If every $b_i=-1$: output $G(L,m)$ via $O(R\log L)$ exponentiation ($R=m$).
- Else: run the mixed DP on $b$ in $O(L\cdot R)$.
- If $R<0$, output $0$.
| Case | Time |
|---|---|
| Subarray all $-1$ | $O(R\log L)$, $R\le m$ |
| General subarray | $O(L\cdot R)$ |
Per test case one query; $\sum n\le 3\cdot 10^5$ over the input. All-free queries with large $L$ use logarithmic layering; mixed queries are $O(L\cdot R)$, and $R$ is often small when fixed values use most of $m$.
E2 keeps the same $g(b,m)$ but adds many queries and point updates on $a$ — you need a data structure on top of this DP core.
model_sol.cpp implements:
apply_free_layer— $O(R)$;pow_free_layers— binary exponentiation for all-free subarrays;apply_fixedand a linear scan for the general case.
Uses AtCoder modint998244353.