0303.cpp (326B)
1 class NumArray { 2 vector<int> prefix; 3 4 public: 5 NumArray(const vector<int> &nums) : prefix(size(nums) + 1) { 6 for (int i = 0, acc = 0; i < size(nums); i++) { 7 prefix[i + 1] = acc += nums[i]; 8 } 9 } 10 11 int sumRange(int left, int right) const { return prefix[right + 1] - prefix[left]; } 12 };