Something Like Disjoint Sparse Table.

Revision en8, by MIRAJ12, 2026-07-19 10:46:28

For some time, I have been using a specific kind of data structure (I created it while solving a problem without any prior knowledge of the Disjoint Sparse Table). I am pretty sure it is basically a Disjoint Sparse Table, but implemented in a simpler way, so I thought of sharing it.

Prerequisites

The main idea comes from the sliding window technique. Suppose you have q queries where every query has a fixed length (r - l) = k. You can answer each query by combining information from two blocks of size k-1 use the suffix left block and the prefix right block. Then, you can compute the ans using those two values.

Here n = 12, k = 4, and one of the queries is: l = 6, r = 9

So, we create prefix and suffix blocks of size 3. Then, we can computed information from these two blocks to answer the query in O(1).

One example problem: CSES Sliding Window Or

One very important thing to notice here k-1 makes sure that all l, r are in adjacent blocks. So the block size is not the important part, the main point is that l and r have to belong to some adjacent blocks.

DSL like idea:

Build structure:

Create suffix and prefix blocks with sizes that are powers of 2, So, have to repeat log(n) times. The memory complexity is also O(2 * n log n), and the time complexity is the same.

For n = 12

Build code

Query structure:

For any query l, r, let x = r - l. l and r will always belong to adjacent blocks, where the block size is either the closest power of 2 to <=x or the next larger power of 2.

Example problem and code:

Static Range Sum Queries

Code

Count maxima on an interval

code

You might need to join the Codeforces group to access this problem

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en8 English MIRAJ12 2026-07-19 10:46:28 26
en7 English MIRAJ12 2026-07-19 09:30:26 56
en6 English MIRAJ12 2026-07-19 09:28:34 4 Tiny change: 'r of 2 to `x` or the' -> 'r of 2 to <=`x` or the' (published)
en5 English MIRAJ12 2026-07-19 09:26:16 3195 Tiny change: 'ructure:** For any qu' -> 'ructure:**\n\nFor any qu'
en4 English MIRAJ12 2026-07-19 09:03:54 551 Tiny change: 'he same.\n\n' -> 'he same.\n![ ](https://codeforces.me/ba2581/Cp 3.jpeg)\n\n\n'
en3 English MIRAJ12 2026-07-19 08:37:51 840 Tiny change: 'e problem:[CSES Slid' -> 'e problem: [CSES Slid'
en2 English MIRAJ12 2026-07-19 08:11:39 352 Tiny change: 'ueries is:\n\n**l = 6, r = 9**\n\nSo, we c' -> 'ueries is:**l = 6, r = 9**\nSo, we c'
en1 English MIRAJ12 2026-07-19 07:48:26 354 Initial revision (saved to drafts)