[C++]: 307. Range Sum Query - Mutable | Accepted | w/ explanation | easy
Accumulate in the constructor.\n2. In update method, subtract the index valude from sum, update index value, add val in sum.\n3.  In sumRange method, initialize
1.  Accumulate in the constructor.\n2. In update method, subtract the index valude from sum, update index value, add val in sum.\n3.  In sumRange method, initialize res with sum, subtract all elements before left and all elements after right from res, return res.\n\n**Please upvote,** if you find the solution useful.\n\n\n```\n\n```\nclass NumArray {\n    vector<int> arr{};\n    int sum;\npublic:\n    NumArray(vector<int>& nums) {\n        arr = nums;\n        sum = accumulate(arr.begin(), arr.end(), 0);\n    }\n    \n    void update(int index, int val) {\n        sum -= arr[index];\n        arr[index] = val;\n        sum += val;\n    }\n    \n    int sumRange(int left, int right) {\n        int  res = sum;\n        for(int i = 0; i < left; ++i)\n            res -= arr[i];\n        for(int i = right+1; i < arr.size(); ++i)\n            res -= arr[i];\n        return res;\n    }\n};
['C']