```
class NumArray {
public:
vector<int> res;
NumArray(vector<int> &nums) {
res = nums;
for(int i = 1; i < res.size(); ++i){
res[i] += res[i-1];
}
}
int sumRange(int i, int j) {
if(i < 0 || j > res.size() - 1 || i > j) return 0;
int ival = res[i] - (i > 0 ? (res[i - 1]) : 0);
return res[j] - res[i] + ival;
}
};
```