Play With Numbers Programming Challenge (mean value of subarrays) I'm trying to solve this  Play With Numbers. I have passed the test cases but, I kept getting time limit exceeded.
Can someone help me improve its performance in order to pass the time limit, please?
Problem

You are given an array of n numbers and q queries. For each query you have to print the floor of the expected value(mean) of the subarray from L to R.

Input

First line contains two integers N and Q denoting number of array elements and number of queries.
Next line contains N space seperated integers denoting array elements.
Next Q lines contain two integers L and R(indices of the array).

Output

print a single integer denoting the answer.

Time Limit:
1.5 sec(s) for each input file.
Result Time:
Input 1: 1.999309
Input 2: 1.998019
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;math.h&gt;

using namespace std;

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int n = 0, q = 0;
cin &gt;&gt; n &gt;&gt; q;

vector&lt;int&gt; nums(n);

for (size_t i = 0; i &lt; n; i++)
{
    cin &gt;&gt; nums[i];
}

for (size_t i = 0; i &lt; q; i++)
{
    float ans = 0.0f;
    int leftIndex = 0, rightIndex = 0;

    cin &gt;&gt; leftIndex &gt;&gt; rightIndex;

    leftIndex = leftIndex - 1;
    rightIndex = rightIndex - 1;

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

    ans = ans / (rightIndex - leftIndex) + 1;
    cout &lt;&lt; floor(ans) &lt;&lt; &quot;\n&quot;;
    }
    system(&quot;pause&quot;);
 }
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.