|
| 1 | +# Square Root Decomposition |
| 2 | + |
| 3 | +Square root decomposition is a block-based range-query technique. Instead of processing every element inside a query range, split the array into blocks of size about `sqrt(n)` and precompute one answer per block. |
| 4 | + |
| 5 | +It belongs to the broader topic of data structures and range query optimization. |
| 6 | + |
| 7 | +## What it does |
| 8 | + |
| 9 | +- Break an array into blocks of size about `sqrt(n)`. |
| 10 | +- Store precomputed information for each block. |
| 11 | +- Answer queries by combining: |
| 12 | + - a partial prefix block |
| 13 | + - zero or more full blocks |
| 14 | + - a partial suffix block |
| 15 | + |
| 16 | +This is usually much faster than brute force and much easier to implement than a segment tree. |
| 17 | + |
| 18 | +## Where it fits |
| 19 | + |
| 20 | +- Data Structures |
| 21 | +- Range Queries |
| 22 | +- Query Processing |
| 23 | +- Block Decomposition |
| 24 | + |
| 25 | +Closely related topics: |
| 26 | + |
| 27 | +- Prefix Sum |
| 28 | +- Fenwick Tree |
| 29 | +- Segment Tree |
| 30 | +- Mo's Algorithm |
| 31 | + |
| 32 | +## When to use it |
| 33 | + |
| 34 | +Use sqrt decomposition when: |
| 35 | + |
| 36 | +- You have many range queries on an array. |
| 37 | +- Updates exist, but the problem does not justify a full segment tree. |
| 38 | +- `O(sqrt(n))` per query is fast enough for the given constraints. |
| 39 | +- You want a contest-safe implementation with low bug risk. |
| 40 | + |
| 41 | +Common use cases: |
| 42 | + |
| 43 | +- Range sum |
| 44 | +- Range min / max |
| 45 | +- Frequency counting per block |
| 46 | +- Jump queries |
| 47 | +- Range updates with block rebuild ideas |
| 48 | + |
| 49 | +## Complexity |
| 50 | + |
| 51 | +- Build: `O(n)` |
| 52 | +- Query: `O(sqrt(n))` |
| 53 | +- Point update: `O(1)` for block sums, sometimes `O(sqrt(n))` if block metadata must be rebuilt |
| 54 | +- Space: `O(n)` |
| 55 | + |
| 56 | +## Core intuition |
| 57 | + |
| 58 | +Array: |
| 59 | + |
| 60 | +```text |
| 61 | +[1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 62 | +``` |
| 63 | + |
| 64 | +Choose block size `3`: |
| 65 | + |
| 66 | +```text |
| 67 | +[1,2,3] [4,5,6] [7,8,9] |
| 68 | +``` |
| 69 | + |
| 70 | +Store block sums: |
| 71 | + |
| 72 | +```text |
| 73 | +[6, 15, 24] |
| 74 | +``` |
| 75 | + |
| 76 | +Now query sum from index `2` to `7`: |
| 77 | + |
| 78 | +- Take partial left block: `3` |
| 79 | +- Take full middle block: `4 + 5 + 6 = 15` |
| 80 | +- Take partial right block: `7 + 8` |
| 81 | + |
| 82 | +Total: `26` |
| 83 | + |
| 84 | +The key idea is that full blocks are answered in `O(1)` each. |
| 85 | + |
| 86 | +## Python template |
| 87 | + |
| 88 | +```python |
| 89 | +import math |
| 90 | + |
| 91 | + |
| 92 | +class SqrtDecomposition: |
| 93 | + def __init__(self, nums): |
| 94 | + self.n = len(nums) |
| 95 | + self.block_size = int(math.sqrt(self.n)) + 1 |
| 96 | + self.nums = nums[:] |
| 97 | + self.blocks = [0] * ((self.n + self.block_size - 1) // self.block_size) |
| 98 | + |
| 99 | + for i, val in enumerate(self.nums): |
| 100 | + self.blocks[i // self.block_size] += val |
| 101 | + |
| 102 | + def update(self, idx, val): |
| 103 | + block_idx = idx // self.block_size |
| 104 | + self.blocks[block_idx] += val - self.nums[idx] |
| 105 | + self.nums[idx] = val |
| 106 | + |
| 107 | + def query(self, left, right): |
| 108 | + total = 0 |
| 109 | + start_block = left // self.block_size |
| 110 | + end_block = right // self.block_size |
| 111 | + |
| 112 | + if start_block == end_block: |
| 113 | + for i in range(left, right + 1): |
| 114 | + total += self.nums[i] |
| 115 | + return total |
| 116 | + |
| 117 | + end_of_start = min(self.n, (start_block + 1) * self.block_size) |
| 118 | + for i in range(left, end_of_start): |
| 119 | + total += self.nums[i] |
| 120 | + |
| 121 | + for block in range(start_block + 1, end_block): |
| 122 | + total += self.blocks[block] |
| 123 | + |
| 124 | + start_of_end = end_block * self.block_size |
| 125 | + for i in range(start_of_end, right + 1): |
| 126 | + total += self.nums[i] |
| 127 | + |
| 128 | + return total |
| 129 | + |
| 130 | + |
| 131 | +nums = [1, 2, 3, 4, 5, 6, 7, 8] |
| 132 | +sq = SqrtDecomposition(nums) |
| 133 | + |
| 134 | +print(sq.query(2, 6)) # 25 |
| 135 | +sq.update(3, 10) # nums[3] = 10 |
| 136 | +print(sq.query(2, 6)) # 31 |
| 137 | +``` |
| 138 | + |
| 139 | +## Decision map |
| 140 | + |
| 141 | +### 1. Prefix Sum |
| 142 | + |
| 143 | +Use when: |
| 144 | + |
| 145 | +- There are only queries. |
| 146 | +- There are no updates. |
| 147 | +- The operation is additive and easy to prefix. |
| 148 | + |
| 149 | +Complexity: |
| 150 | + |
| 151 | +- Build: `O(n)` |
| 152 | +- Query: `O(1)` |
| 153 | + |
| 154 | +### 2. Fenwick Tree |
| 155 | + |
| 156 | +Use when: |
| 157 | + |
| 158 | +- You need point updates. |
| 159 | +- Queries are prefix sums or reducible to prefix sums. |
| 160 | +- You want `O(log n)` with a compact implementation. |
| 161 | + |
| 162 | +Complexity: |
| 163 | + |
| 164 | +- Query: `O(log n)` |
| 165 | +- Update: `O(log n)` |
| 166 | + |
| 167 | +### 3. Segment Tree |
| 168 | + |
| 169 | +Use when: |
| 170 | + |
| 171 | +- Queries are more complex: min, max, gcd, custom merge. |
| 172 | +- You need many updates and many queries. |
| 173 | +- You may need lazy propagation. |
| 174 | + |
| 175 | +Complexity: |
| 176 | + |
| 177 | +- Query: `O(log n)` |
| 178 | +- Update: `O(log n)` |
| 179 | + |
| 180 | +### 4. Sqrt Decomposition |
| 181 | + |
| 182 | +Use when: |
| 183 | + |
| 184 | +- You want simpler code. |
| 185 | +- Constraints are moderate. |
| 186 | +- `O(sqrt(n))` is acceptable. |
| 187 | + |
| 188 | +Complexity: |
| 189 | + |
| 190 | +- Query: `O(sqrt(n))` |
| 191 | +- Update: `O(1)` or `O(sqrt(n))` |
| 192 | + |
| 193 | +### 5. Mo's Algorithm |
| 194 | + |
| 195 | +Use when: |
| 196 | + |
| 197 | +- Queries are offline. |
| 198 | +- The answer depends on frequencies or distinct counts. |
| 199 | +- Reordering queries helps maintain a moving window. |
| 200 | + |
| 201 | +Complexity: |
| 202 | + |
| 203 | +- About `O((n + q) * sqrt(n))` |
| 204 | + |
| 205 | +## Quick contest checklist |
| 206 | + |
| 207 | +Ask: |
| 208 | + |
| 209 | +1. Are there updates? |
| 210 | +2. Are the queries online or offline? |
| 211 | +3. Is the operation simple sum/frequency, or a custom merge? |
| 212 | +4. Is implementation speed more important than asymptotic optimality? |
| 213 | + |
| 214 | +Typical choices: |
| 215 | + |
| 216 | +- No updates: prefix sum |
| 217 | +- Sum with point updates: Fenwick tree |
| 218 | +- Complex range query: segment tree |
| 219 | +- Simple and good enough: sqrt decomposition |
| 220 | +- Offline distinct/frequency query: Mo's algorithm |
| 221 | + |
| 222 | +## Interview and contest notes |
| 223 | + |
| 224 | +- Sqrt decomposition is often the easiest correct solution under time pressure. |
| 225 | +- It is a strong stepping stone before learning segment trees and Mo's algorithm. |
| 226 | +- If `O(sqrt(n))` is too slow, upgrade to Fenwick tree or segment tree. |
| 227 | +- If the problem is offline and frequency-based, think about Mo's algorithm immediately. |
| 228 | + |
| 229 | +## Practice ideas |
| 230 | + |
| 231 | +- Range sum query with point update |
| 232 | +- Range minimum query with updates |
| 233 | +- Distinct elements in range |
| 234 | +- Frequency of a value in range |
| 235 | +- Jump query / teleport blocks |
| 236 | +- Offline range query reordering with Mo's algorithm |
| 237 | + |
| 238 | +## Recommended study order |
| 239 | + |
| 240 | +Prefix Sum -> Fenwick Tree -> Sqrt Decomposition -> Segment Tree -> Mo's Algorithm |
0 commit comments