{"id":259,"date":"2026-11-13T12:36:10","date_gmt":"2026-11-13T04:36:10","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-square-root-decomposition-sums-a-range-by-blocks\/"},"modified":"2026-11-13T21:53:53","modified_gmt":"2026-11-13T13:53:53","slug":"how-square-root-decomposition-sums-a-range-by-blocks","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-square-root-decomposition-sums-a-range-by-blocks\/","title":{"rendered":"How square-root decomposition sums a range by blocks"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Square-root decomposition<\/strong> breaks an array into contiguous blocks of length about \u221an. 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 <strong>O(\u221an)<\/strong> when B is \u221an. A Fenwick tree or a segment tree answers the same sum in <strong>O(log n)<\/strong> and is the usual replacement when the operation is a prefix sum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The block length is a trade. Shorter blocks make the partial scans cheap and the block scan long. Longer blocks do the opposite. \u221an balances them for an array that receives a mixture of updates and queries.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Point assignment and range sum are both O(\u221an) with B = \u2308\u221an\u2309, 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, \u0398(n\/B) words. Building the blocks is a linear pass.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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(\u221an) 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose B near \u221an. Block i covers indices iB through the cell before the next block, clipped at n.<\/li>\n<li>Store the array and an array of block sums. Fill the block sums in one pass.<\/li>\n<li>To add a value at index i, add it to the cell and to block i\/\/B.<\/li>\n<li>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.<\/li>\n<li>If L and R fall in the same block, sum that slice directly and skip the block array.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">O(\u221an) 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when every query walks the whole array<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Store block sums and still scan every cell of the query<\/td>\n<td>The scan is linear. The block sum exists so a covered block contributes in constant time.<\/td>\n<\/tr>\n<tr>\n<td>Set the block length to n<\/td>\n<td>There is one block. A query that starts in the middle scans almost every cell. The balance required a length near \u221an.<\/td>\n<\/tr>\n<tr>\n<td>Update the cell and leave the block sum stale<\/td>\n<td>The next range that covers the block returns the old total. The difference has to land in both places.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(log n) because a tree can also sum a range<\/td>\n<td>This structure is not that tree. Its query walks O(\u221an) blocks and cells. The logarithmic bound belongs to a Fenwick tree or a segment tree.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>14<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=(1 2 3 4 5 6)\nB=2\nn=${#a[@]}\nnb=$(( (n + B - 1) \/ B ))\nfor ((b=0; b&lt;nb; b++)); do\n  blocks[b]=0\n  for ((j=0; j&lt;B; j++)); do\n    i=$((b*B+j))\n    if (( i &lt; n )); then blocks[b]=$((blocks[b]+a[i])); fi\n  done\ndone\nL=1\nR=4\ns=0\nwhile (( L &lt;= R &amp;&amp; L % B != 0 )); do s=$((s+a[L])); L=$((L+1)); done\nwhile (( L + B - 1 &lt;= R )); do s=$((s+blocks[L\/B])); L=$((L+B)); done\nwhile (( L &lt;= R )); do s=$((s+a[L])); L=$((L+1)); done\necho &quot;$s&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [1, 2, 3, 4, 5, 6]\nB = 2\nblocks = [sum(a[i:i + B]) for i in range(0, len(a), B)]\n\ndef add(i, delta):\n    a[i] += delta\n    blocks[i \/\/ B] += delta\n\ndef rangesum(L, R):\n    s = 0\n    while L &lt;= R and L % B and L &lt; len(a):\n        s += a[L]\n        L += 1\n    while L + B - 1 &lt;= R:\n        s += blocks[L \/\/ B]\n        L += B\n    while L &lt;= R:\n        s += a[L]\n        L += 1\n    return s\n\nadd(2, 0)\nassert rangesum(1, 4) == sum(a[1:5]) == 14\nprint(rangesum(1, 4))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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(\u221an) scan is the intended design.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for square-root decomposition returns the block sum, the partial end scans, and the \u221an balance. <strong>Square-root decomposition answers a range sum in O(\u221an) by adding whole block sums across the middle and scanning only the partial blocks at the two ends.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Square-root decomposition breaks an array into contiguous blocks of length about \u221an. 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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,62],"tags":[],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-range-queries"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions\/658"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}