Square Root Decomposition
Authors: Benjamin Qi, Neo Wang
Splitting up data into smaller chunks to speed up processing.
Focus Problem – try your best to solve 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
We partition the array into blocks of size . Each block stores the sum of elements within it, and allows for the
creation of corresponding update
and query
operations.
Update Queries:
To update an element at location , first find the corresponding block using the formula .
Then, apply the corresponding difference between the element currently stored at and the element we want to change it to.
Sum Queries:
To perform a sum query from , calculate
where represents the total sum of the -th block, the -th block represents the sum of the elements from the range , and .
Finally, is the difference between the two sums and , which each are calculated in .
C++
#include <bits/stdc++.h>using namespace std;struct Sqrt {int block_size;vector<int> nums;vector<long long> blocks;Sqrt(int sqrtn, vector<int> &arr) : block_size(sqrtn), blocks(sqrtn, 0) {nums = arr;for (int i = 0; i < nums.size(); i++) {
Java
import java.io.*;import java.util.*;public class DRSQ {public static void main(String[] args) throws IOException {BufferedReader br =new BufferedReader(new InputStreamReader(System.in));PrintWriter pw = new PrintWriter(System.out);StringTokenizer st = new StringTokenizer(br.readLine());
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.
C++
#include <bits/stdc++.h>using namespace std;int n, q;vector<int> arr;vector<long long> prefix;/** Build the prefix array for arr */void build() {prefix[0] = 0;
Java
Warning: TLE
Due to tight time constraint on CSES, the Java implementation might get TLE.
import java.io.*;import java.util.*;public class DRSQ {static int[] arr;static List<Long> prefix;public static void main(String[] args) throws IOException {BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
Mo's Algorithm
Resources | ||||
---|---|---|---|---|
CF | very brief description | |||
HE | elaborate description with proof | |||
CPH |
Additional Notes
Low constraints (ex. ) and/or high time limits (greater than 2s) can be signs that square root decomposition 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.
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. 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 square root decomposition can be used:
Resources | ||||
---|---|---|---|---|
CF | format isn't great but tree example is ok |
Problems
Set A
Problems where the best solution involves square root decomposition.
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CF | Easy | Show TagsSqrt | |||
JOI | Easy | Show TagsSqrt | |||
POI | Easy | Show TagsSqrt | |||
CF | Easy | Show TagsSqrt | |||
CF | Easy | Show TagsSqrt | |||
YS | Normal | Show TagsSqrt | |||
CF | Normal | Show TagsMo's Algorithm | |||
APIO | Hard | Show TagsSqrt | |||
JOI | Hard | Show TagsSOS DP | |||
Plat | Very Hard | Show TagsSqrt | |||
DMOJ | Very Hard | Show TagsSqrt | |||
DMOJ | Very Hard | Show TagsSqrt |
Set B
Problems that can be solved without it. But you might as well try to use it!
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
JOI | Normal | Show Tags2DRQ, Mo's Algorithm | |||
IOI | Normal | Show TagsSqrt | |||
Plat | Normal | Show TagsSqrt | |||
Plat | Hard | Show TagsSqrt | |||
CF | Hard | Show TagsSqrt | |||
CF | Hard | Show TagsHLD | |||
TLX | Hard | Show TagsSqrt | |||
CSA | Hard | Show TagsSqrt | |||
Old Gold | Hard | Show TagsConvex | |||
CF | Very Hard | Show TagsConvex | |||
IOI | Very Hard | Show TagsSqrt | |||
Plat | Very Hard | Show TagsSqrt | |||
IOI | Very Hard | Show Tags2DRQ |
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!