C++ Binary Indexed Tree and Segment Tree Solution
Binary Indexed Tree Solution:\n\n    class NumArray {\n    public:\n    \tNumArray(vector &nums):v(nums) {\n    \t\tint len = nums.size();\n    \t\tbitree.resiz
Binary Indexed Tree Solution:\n\n    class NumArray {\n    public:\n    \tNumArray(vector<int> &nums):v(nums) {\n    \t\tint len = nums.size();\n    \t\tbitree.resize(len + 1, 0);\n    \n    \t\tfor (int i = 0; i < len; i++) {\n    \t\t\tint idx = i + 1;\n    \t\t\twhile (idx < len + 1) {\n    \t\t\t\tbitree[idx] += nums[i];\n    \t\t\t\tidx = idx + (idx & -idx);\n    \t\t\t}\n    \t\t}\n    \t}\n    \n    \tvoid update(int i, int val) {\n    \t\tint len = bitree.size();\n    \t\tint idx = i + 1;\n    \n    \t\twhile (idx < len) {\n    \t\t\tbitree[idx] += val - v[i];\n    \t\t\tidx = idx + (idx & -idx);\n    \t\t}\n    \n    \t\tv[i] = val;\n    \t}\n    \n    \tint sumRange(int i, int j) {\n    \t\tint sumi = 0, sumj = 0;\n    \t\tint idxi = i, idxj = j + 1;\n    \n    \t\twhile (idxi) {\n    \t\t\tsumi += bitree[idxi];\n    \t\t\tidxi = idxi - (idxi & -idxi);\n    \t\t}\n    \n    \t\twhile (idxj) {\n    \t\t\tsumj += bitree[idxj];\n    \t\t\tidxj = idxj - (idxj & -idxj);\n    \t\t}\n    \n    \t\treturn sumj - sumi;\n    \t}\n    private:\n    \tvector<int> bitree;\n    \tvector<int>& v;\n    };\n    \n    \n    \nSegment Tree Solution:\n\n    class NumArray {\n    public:\n    \tNumArray(vector<int> &nums) {\n    \t\tint size = nums.size();\n    \t\tsegtree.resize(2 * size);\n    \t\tfor (int i = size; i < 2 * size; i++) segtree[i] = nums[i - size];\n    \t\tfor (int i = size - 1; i > 0; i--) segtree[i] = segtree[i * 2] + segtree[i * 2 + 1];\n    \t}\n    \n    \tvoid update(int i, int val) {\n    \t\tint pos = i + segtree.size() / 2;\n    \t\tint diff = val - segtree[pos];\n    \t\twhile (pos) {\n    \t\t\tsegtree[pos] += diff;\n    \t\t\tpos /= 2;\n    \t\t}\n    \t}\n    \n    \tint sumRange(int i, int j) {\n    \t\tint n = segtree.size() / 2;\n    \t\ti += n;\n    \t\tj += n;\n    \t\tint sum = 0;\n    \t\twhile (i <= j) {\n    \t\t\tif ((i % 2) == 1) sum += segtree[i];\n    \t\t\tif ((j % 2) == 0) sum += segtree[j];\n    \t\t\ti = (i + 1) / 2;\n    \t\t\tj = (j - 1) / 2;\n    \t\t}\n    \t\treturn sum;\n    \t}\n    private:\n    \tvector<int> segtree;\n    };
[]