01A tree of range sums
Put the array in the leaves of a complete binary tree. Every internal node stores the sum of its two children, so each node is responsible for one contiguous range: the root covers everything, its children cover the halves, and so on.
To answer sum(l..r), combine the few nodes whose ranges lie fully inside [l, r] and together tile it exactly. There are at most two such nodes per level, so a query touches O(log n) nodes.
In the figure, sum(2..6) uses the nodes [2–3], [4–5] and [6]: three nodes instead of five numbers. On a million elements it is about 40 nodes instead of a million.
Store the tree in an array of size 4n: node v has children 2v and 2v+1, just like a binary heap. (Exactly 2n is enough only when n is a power of two, or with the iterative bottom-up layout.)