Data Structures19 sections · 729 units
Open in Course

Range Sum I - Analysis

Complexity comparison

Compare approaches: Segment tree: O(logn)O(\log n) per operation, total O(qlogn)O(q \log n) Sqrt decomposition: O(n)O(\sqrt{n}) per query, O(1)O(1) per update For n=q=2105n = q = 2 \cdot 10^5: segment tree does about 3.41063.4 \cdot 10^6 operations, sqrt does about 8.91078.9 \cdot 10^7 query operations plus qq update operations.

Both work here. Segment trees are faster asymptotically, but sqrt decomposition is simpler to implement. In contests, simpler often means fewer bugs.