How square-root decomposition sums a range by blocks

Short answer: Square-root decomposition breaks an array into contiguous blocks of length about √n. Each block stores the sum of its cells. A point update changes one cell and one block sum. A range sum adds the complete blocks in the middle and walks the loose cells at each end. With block length B the query touches O(n/B + B) cells, which is O(√n) when B is √n. A Fenwick tree or a segment tree answers the same sum in O(log n) and is the usual replacement when the operation is a prefix sum.


What the problem actually asks

A range-sum query can be answered from a prefix-sum array in constant time, until an update arrives. A single assignment then rewrites every prefix to the right. Rebuilding from scratch is linear per update. Blocks are the compromise that predates, and sits beside, the tree structures: most of a long range is a run of whole blocks, and only two partial blocks need to be read cell by cell.

The block length is a trade. Shorter blocks make the partial scans cheap and the block scan long. Longer blocks do the opposite. √n balances them for an array that receives a mixture of updates and queries.


What the algorithm guarantees

Point assignment and range sum are both O(√n) with B = ⌈√n⌉, or O(1) for the update if the block sum is adjusted by the difference rather than recomputed. The answers are exact for integer or exact arithmetic. Extra memory is the block array, Θ(n/B) words. Building the blocks is a linear pass.

The same layout supports other block summaries: a block minimum, a sorted copy of a block for order statistics, a frequency table. The query cost stays O(√n) only while a partial block can be scanned in O(B) and a whole block can be applied in O(1). A general associative operation with lazy range writes is what a segment tree is for. Square-root blocks can also carry a lazy add, and the constant factors differ.


The working steps

  1. Choose B near √n. Block i covers indices iB through the cell before the next block, clipped at n.
  2. Store the array and an array of block sums. Fill the block sums in one pass.
  3. To add a value at index i, add it to the cell and to block i//B.
  4. To sum the inclusive range L through R, sum the cells from L to the end of its block, add each fully covered block sum, and sum the cells of the last partial block.
  5. If L and R fall in the same block, sum that slice directly and skip the block array.

O(√n) is the cost of this layout for point updates and range sums. A Fenwick tree answers the same problem in O(log n) with a smaller constant and less code once the bit step is familiar. Square-root decomposition remains the simple exact method, and the right method for some block summaries that do not fit a prefix tree.


Approaches that look simpler and fail

Approach Why it fails when every query walks the whole array
Store block sums and still scan every cell of the query The scan is linear. The block sum exists so a covered block contributes in constant time.
Set the block length to n There is one block. A query that starts in the middle scans almost every cell. The balance required a length near √n.
Update the cell and leave the block sum stale The next range that covers the block returns the old total. The difference has to land in both places.
Quote O(log n) because a tree can also sum a range This structure is not that tree. Its query walks O(√n) blocks and cells. The logarithmic bound belongs to a Fenwick tree or a segment tree.

Prefix sums with point updates are a Fenwick tree. A general associative range with lazy writes is a segment tree. Square-root decomposition is the block array.


A working example

The Bash script and the Python script keep a block sum for every two cells. A point update touches the cell and its block. The range from index 1 through 4 sums to 14.

a=(1 2 3 4 5 6)
B=2
n=${#a[@]}
nb=$(( (n + B - 1) / B ))
for ((b=0; b<nb; b++)); do
  blocks[b]=0
  for ((j=0; j<B; j++)); do
    i=$((b*B+j))
    if (( i < n )); then blocks[b]=$((blocks[b]+a[i])); fi
  done
done
L=1
R=4
s=0
while (( L <= R && L % B != 0 )); do s=$((s+a[L])); L=$((L+1)); done
while (( L + B - 1 <= R )); do s=$((s+blocks[L/B])); L=$((L+B)); done
while (( L <= R )); do s=$((s+a[L])); L=$((L+1)); done
echo "$s"
a = [1, 2, 3, 4, 5, 6]
B = 2
blocks = [sum(a[i:i + B]) for i in range(0, len(a), B)]

def add(i, delta):
    a[i] += delta
    blocks[i // B] += delta

def rangesum(L, R):
    s = 0
    while L <= R and L % B and L < len(a):
        s += a[L]
        L += 1
    while L + B - 1 <= R:
        s += blocks[L // B]
        L += B
    while L <= R:
        s += a[L]
        L += 1
    return s

add(2, 0)
assert rangesum(1, 4) == sum(a[1:5]) == 14
print(rangesum(1, 4))

Where it is used

Square-root decomposition is a block array for range sums when the update rate does not justify a segment tree. It is mostly a teaching and contest structure. Analytics systems store the same totals in a segment tree or in a database.


When you can drop this algorithm

Drop blocks for plain point-update range-sum once a Fenwick tree is acceptable. Keep blocks when the summary is a small table per block, or when a transparent O(√n) scan is the intended design.


Why this stays published

We publish this so a search for square-root decomposition returns the block sum, the partial end scans, and the √n balance. Square-root decomposition answers a range sum in O(√n) by adding whole block sums across the middle and scanning only the partial blocks at the two ends.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *