Skip to content
Snippets Groups Projects
Commit 13bac959 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add sqrt decomposition

parent a437d0fb
Branches
No related tags found
No related merge requests found
/**
* Sqrt Decomposition
*
* Complexity (time):
* Preprocess -> O(n)
* Query -> O(sqrt(n))
* Update -> O(1)
* Complexity (space): O(n)
*/
int v[MAX];
int block[MAX];
int block_size;
/**
* Update v[idx] with val.
* @param idx index of v
* @param val new value of v[idx]
*/
void update(int idx, int val) {
block[idx / block_size] += val - v[idx];
v[idx] = val;
}
/**
* Range sum query of v[l..r].
* @param l,r range
*/
int query(int l, int r) {
int ans = 0;
// Query sum of elements in case l is inside a block
for (; l < r && ((l % block_size) != 0); ++l)
ans += v[l];
// Query sum of each block between l and r
for (; l + block_size <= r; l += block_size)
ans += block[l / block_size];
// Query sum of remaining blocks (e.g. r is inside a block)
for (; l <= r; ++l)
ans += v[l];
return ans;
}
/**
* Fills block array with necessary data to perform update and query in
* less than linear time.
* @param n number of elements of v
*/
void preprocess(int n) {
block_size = sqrt(n);
int idx = -1;
for (int i = 0; i < n; ++i) {
if (i % block_size == 0)
block[++idx] = 0;
block[idx] += v[i];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment