Data Structures19 sections · 729 units
Open in Course

Point Updates

Updating one element

To update arr[i]arr[i] to a new value vv:

1.1. Find which block ii belongs to: b=i/Bb = \lfloor i/B \rfloor

2.2. Update the block sum: blockSum[b]=blockSum[b]arr[i]+vblockSum[b] = blockSum[b] - arr[i] + v

3.3. Update the array: arr[i]=varr[i] = v That's it. One block update, one array update. Time: O(1)O(1). Compare this to segment trees where updates take O(logn)O(\log n). For problems with many updates, sqrt decomposition has faster updates but slower queries. The tradeoff depends on your query-to-update ratio.