Square Root Decomposition
Author: Benjamin Qi
Splitting up data into smaller chunks to speed up processing.
Focus Problem – read through this problem before continuing!
You should already have done this problem in Point Update Range Sum, but here we'll present two more approaches. Both run in time.
Resources | |||
---|---|---|---|
CPH | |||
CF | Blocking, Mo's Algo |
Blocking
See the first example in CPH.
1int n,q;2vi x;3ll block[450];4int BLOCK;56ll sum(int a) { // sum of first a elements of array in O(sqrt(n)) time7 ll ans = 0;8 F0R(i,a/BLOCK) ans += block[i];9 FOR(i,a/BLOCK*BLOCK,a) ans += x[i];10 return ans;
Batching
See the CPH section on "batch processing."
Maintain a "buffer" of the latest updates (up to ). The answer for each sum query can be calculated with prefix sums and by examining each update within the buffer. When the buffer gets too large (), clear it and recalculate prefix sums.
1int n,q;2vi x;3vl cum;45void build() {6 cum = {0};7 trav(t,x) cum.pb(cum.bk+t);8}910int main() {
Mo's Algorithm
See CPH 27.3 and the CF link above.
Resources | |||
---|---|---|---|
CF | very brief description |
Additional Notes
Low constraints (ex. ) and/or high time limits (greater than 2s) can be signs that sqrt decomp is intended.
In practice, it is not necessary to use the exact value of as a parameter, and instead we may use parameters and where is different from . The optimal parameter depends on the problem and input. For example, if an algorithm often goes through the blocks but rarely inspects single elements inside the blocks, it may be a good idea to divide the array into blocks, each of which contains elements.
As another example, if an update takes time proportional to the size of one block () while a query takes time proportional to the number of blocks times () then we can set to make both updates and queries take time .
Solutions with worse complexities are not necessarily slower (at least for problems with reasonable input sizes, ex. ). I recall an instance where a fast solution passed (where came from a BIT) while an solution did not ... So constant factors are important!
On Trees
The techniques mentioned in the blogs below are extremely rare but worth a mention.
Resources | |||
---|---|---|---|
CF | |||
CF |
Some more discussion about how sqrt decomp can be used:
Resources | |||
---|---|---|---|
CF | format isn't great but tree example is ok |
Problems
A
Problems where the best solution I know of involves sqrt decomp.
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
JOI | Easy | View Solution | ||||
POI | Easy | View Solution | ||||
YS | Normal | |||||
APIO | Hard | |||||
JOI | Hard | Show TagsSOS DP | View Solution | |||
Plat | Very Hard | External Sol | ||||
DMOJ | Very Hard | |||||
DMOJ | Very Hard | Check DMOJ |
B
Problems that can be solved without it. But you might as well try to use it!!
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
JOI | Normal | Show TagsMo's algorithm, 2D SRQ | View Solution | |||
IOI | Normal | External Sol | ||||
Plat | Hard | External Sol | ||||
CF | Hard | Check CF | ||||
TLX | Hard | Check TLX | ||||
CSA | Hard | Check CSA | ||||
Old Gold | Hard | Show TagsConvex Hull | External Sol | |||
CF | Very Hard | Show TagsConvex Hull | Check CF | |||
IOI | Very Hard | External Sol | ||||
Plat | Very Hard | External Sol | ||||
IOI | Very Hard | Show Tags2D SRQ | External Sol |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!