Data Structures19 sections · 729 units
Open in Course

The Core Idea

Dividing into blocks

Given an array of nn elements, divide it into blocks of size BnB \approx \sqrt{n}. For n=16n = 16, you'd have 44 blocks of size 44.

Each block precomputes something useful: sum, min, max, or whatever your query needs.

A range query then combines at most 22 partial blocks (at the ends) plus complete blocks in the middle. Why n\sqrt{n}?

It balances two costs: the number of blocks (n/Bn/B) and the block size (BB). When B=nB = \sqrt{n}, both are n\sqrt{n}, giving you O(n)O(\sqrt{n}) time for queries and updates. Any other block size would make one term dominate.