Time complexity : time per query, time pre-computation.
The pre-computation done in the constructor takes time. Each sumRange query's time complexity is as the hash table's look up operation is constant time.

Space complexity : .
The extra space required is as there are candidates for both and .

Approach #3 (Caching) [Accepted]

The above approach takes a lot of space, could we optimize it?

Imagine that we pre-compute the cummulative sum from index to . Could we use this information to derive ?