[C++] Simple Solution (97% Time/97% Space) - Explained in Comments
The approach is rather straight forward. First take the total sum of the original container. \n\nWhenever a sum is requested, if the length of this sum (right -
The approach is rather straight forward. First take the total sum of the original container. \n\nWhenever a sum is requested, if the length of this sum (right - left) is greater than half of the existing container length, it will remove the excess of this range from the current total. \n\nIf it is less than half the length, it will just calculate the total in the bounds of left to right. \n\nWhenever an index value is swapped, the running total is updated by subtracting the current value, updating the value in the vector and adding the new value to our running total.\n\n```\nclass NumArray {\npublic:\n    NumArray(vector<int>& nums) {\n        num = nums;\n        \n        for(int &x : num)\n            total += x;\n    }\n    \n    void update(int index, int val) {\n        total -= num[index];\n        num[index] = val;\n        total += val;\n    }\n    \n    int sumRange(int left, int right) {\n        int tmpSum = total;\n        \n        // the sum we are looking for is a majority of the container, so remove the excess\n        if(right - left > num.size() / 2)\n        {\n            for(int x = 0; x < left; ++x)\n                tmpSum -= num[x];\n            \n            for(int x = right + 1; x < num.size(); ++x)\n                tmpSum -= num[x];\n        }\n        else\n        {\n            // sum we are looking for is a minority of the container, so just add it\n            tmpSum = 0;\n            for(int x = left; x <= right; ++x)\n                tmpSum += num[x];\n        }\n        \n        return tmpSum;\n    }\n    \nprivate:\n    vector<int> num;\n    int total = 0;\n};\n```\n
['C', 'C++']