I translated the algorithm presented at https://leetcode.com/discuss/79083/share-my-solution in C++ as below.

```
class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper) {
int n = nums.size();
vector< double > sums( n+1, 0 );
for( int i=0; i<n; i++ ){
sums[i+1] = sums[i] + nums[ i ];
}
return merge_s( sums, 0, n+1, lower, upper );
}
private:
int merge_s( vector<double> sums, int start, int end, int lower,int upper ){
if( end - start <= 1 ){
return 0;
}
int mid = (start + end) / 2;
int count = merge_s( sums, start, mid, lower, upper ) + merge_s( sums, mid, end, lower, upper );
int j = mid;
int k = mid;
int t = mid;
vector< double > cache( end - start );
for( int i = start, r = 0; i < mid; ++i, ++r ){
while( k < end && sums[ k ] - sums[ i ] < lower ){
k++;
}
while( j < end && sums[ j ] - sums[ i ] <= upper ){
j++;
}
while( t < end && sums[ t ] < sums[ i ] ){
cache[ r++ ] = sums[ t++ ];
}
cache[ r ] = sums[ i ];
count += j - k;
}
memcpy( &sums[0]+start, &cache[0], ( t - start ) * sizeof( double) );
/*
for( int i=0; i < t - start; i++ ){
sums[ start + i ] = cache[ i ];
}
*/
return count;
}
};
```

The OJ result is

```
Submission Result: Time Limit Exceeded
Last executed input:
[28,26,22,7,12,-26,25,11,-14,0,0,6,20,4,17,-7,4,-14,-19,-16,8,-21,-26,24,-29,-18,-13,2,-16,-14,-26,-14, ...// more numbers follow]
1
4
```

But the JAVA version at the above link is Accepted.

Can anybody tell me what is wrong/inefficient in my C++ implementation?

Thanks a lot in advance.