Play With Numbers Programming Challenge (mean value of subarrays)
You are running this O(R - L) loop for each query:


for (size_t i = leftIndex; i &lt;= rightIndex; i++)
{
    ans = ans + nums[i];
}



You could obtain each sum in O(1) time if you stored the array as cumulative sums instead.

$$
\begin{align}
S_1 =&amp;\ A_1 \\
S_2 =&amp;\ S_1 + A_2 \\
S_3 =&amp;\ S_2 + A_3 \\
 \vdots&amp; \\
S_N =&amp; S_{N-1} + A_N
\end{align}
$$

The tricky part is that \$S_N\$ could be as large as \$10^{15}\$, which requires more than 32 bits.