python
class SegmentTree:
def __init__(self, arr):
self.arr = arr
self.tree = [0] * (4 * len(arr))
self.build(0, 0, len(arr)-1)

def build(self, node, start, end):
if start == end:
self.tree[node] = self.arr[start]
else:
mid = (start + end) // 2
left_node = 2 * node + 1
right_node = 2 * node + 2
self.build(left_node, start, mid)
self.build(right_node, mid+1, end)
self.tree[node] = self.tree[left_node] + self.tree[right_node]

def update(self, node, start, end, idx, val):
if start == end:
self.arr[idx] = val
self.tree[node] = val
else:
mid = (start + end) // 2
left_node = 2 * node + 1
right_node = 2 * node + 2
if start <= idx <= mid:
self.update(left_node, start, mid, idx, val)
else:
self.update(right_node, mid+1, end, idx, val)
self.tree[node] = self.tree[left_node] + self.tree[right_node]

def query(self, node, start, end, l, r):
if r < start or end < l:
return 0
if l <= start and end <= r:
return self.tree[node]
mid = (start + end) // 2
left_node = 2 * node + 1
right_node = 2 * node + 2
sum_left = self.query(left_node, start, mid, l, r)
sum_right = self.query(right_node, mid+1, end, l, r)
return sum_left + sum_right
