a tree solution:
create a full tree, whose leaves are your entrees: 1,2,...,n
node.value = inserted value [if node is a leaf]
node.value = node.left.value + node.right.value [if node is not a leaf]
so, actually - for each subtree, its root's value is the sum of the values of all its nodes.
updating an entree is O(logn) since you need to update sums all the way up to the root.
for sum(i,j) we will find the sum of all elements left to i, and all elements right to j, and subtract it from root.value, and we'll get what we needed.
so:
sum(root,i,j) = root.value - sumLeft(i,root) - sumRight(j,root)
the calculation of sumLeft() and sumRight() is simple, [for sumLeft:] just start from the root down to the lower bound, and every time you go left, subtract the right son value, and by doing so, you cut from sum the not needed nodes. [convince yourself why this is true]. at the end, the sum has only the relevat nodes. [same principle for sumRight, but we subtract left sons instead].
pseudo code:
sumLeft(x,root):
curr <- root
sum <- root.value
while (curr is not a leaf):
if (x is in curr.right):
curr <- curr.right
else:
sum <- sum - curr.right.value
curr <- curr.left
return sum
sumRight(x,root):
curr <- root
sum <- root.value
while (curr is not a leaf):
if (x is in curr.left):
curr <- curr.left
else:
sum <- sum - curr.left.value
curr <- curr.right
return sum
so, sum(root,i,j) requires you twice to go down the tree, which is still O(logn), as needed.
EDIT1 :
this is how you add x to entree i, on root: [the question indicates there the oporation is adding to the entree and not replacing it, so this is what this solution assumes. it can be easily modified to insertion as well].
add(root,i,x):
root.value <- root.value + x
if root is leaf:
return
else if i is in root.left:
add(root.left,i,x)
else:
add(root.right,i,x)
note that you can know exactly which subtree (left or right) of the root, the entree i belongs to [if i < n/2 to the left, else to the right]. explain yourself how it is done for any subtree, and not only the root.
EDIT 2:
example for tree with 8 elements:
21
10 11
4 6 7 4
1 3 2 4 6 1 2 2
summarize example:
sum(4,6) should provide 4 + 6 + 1 = 11.
sumLeft(4) should provide 1 + 3 + 2 = 6 and it provides: sumLeft(4) = 21 - 11 - 4 = 6 as expected.
sumRight(6) should provide 2 + 2 = 4 and it provides sumLeft(6) = 21 - 11 - 7 =4. [as expected]
so, at overall: sum(4,6) = 21 - 6 - 4 = 11 as expected.
add example:
add(2,4) [adding 4 to the 2nd element] will result in modifying the tree to:
25
14 11
8 6 7 4
1 7 2 4 6 1 2 2