🔥Easiest Segment Tree Implementation | Clean Code | C++ |
Code\n\nclass NumArray {\npublic:\n    int N = 0;\n    vector<int> sgt;\n    NumArray(vector<int>& nums) {\n        N = nums.size();\n        sgt.resize(2 * N +
# Code\n```\nclass NumArray {\npublic:\n    int N = 0;\n    vector<int> sgt;\n    NumArray(vector<int>& nums) {\n        N = nums.size();\n        sgt.resize(2 * N + 1);\n        \n        for (int i = 0; i < N; i++)\n        {\n            sgt[N + i] = nums[i];\n        }\n\n        for (int i = N - 1; i > 0; i--)\n        {\n            sgt[i] = sgt[2 * i] + sgt[2 * i + 1];\n        }\n    }\n    \n    void update(int index, int val) {\n        index += N;\n        sgt[index] = val;\n\n        while (index > 1)\n        {\n            index /= 2;\n\n            sgt[index] = sgt[2 * index] + sgt[2 * index + 1];\n        }\n    }\n    \n    int sumRange(int left, int right) {\n        left += N;\n        right += N;\n\n        int sum = 0;\n        while (left <= right)\n        {\n            if (left & 1)\n            {\n                sum += sgt[left];\n                left++;\n            }\n            if (!(right & 1))\n            {\n                sum += sgt[right];\n                right--;\n            }\n\n            left = left >> 1;\n            right = right >> 1;\n        }\n\n        return sum;\n    }\n};\n\n/**\n * Your NumArray object will be instantiated and called as such:\n * NumArray* obj = new NumArray(nums);\n * obj->update(index,val);\n * int param_2 = obj->sumRange(left,right);\n */\n```
['C++']