The disjoint sparse table returns the product of each range full
This proof shows: correctness, complexity.
Let (S,⋅) be a semigroup, A[0…n−1] over S with n≥1, K=⌈lgn⌉ and N=2K. Write A[i…j]=A[i]⋅A[i+1]⋯A[j] for i≤j; by associativity it does not depend on the bracketing, and A[i…j]=A[i…k]⋅A[k+1…j] for i≤k<j (the split law).
For 1≤h≤K and 0≤i<n, let b=2h⌊i/2h⌋ and m=b+2h−1. The table entry is T[h][i]=A[i…m−1] if i<m and T[h][i]=A[m…i] if i≥m.
Lemma 1 (build). BUILD sets every T[h][i] with i<n to its definition.
Proof. Fix a block [b,b+2h) with middle m. If m−1<n, line L7 sets T[h][m−1]=A[m−1]=A[m−1…m−1]. For i=m−2 down to b, if T[h][i+1]=A[i+1…m−1], then line L8 sets T[h][i]=A[i]⋅A[i+1…m−1]=A[i…m−1]. So all of the left half is right when m−1<n. If m−1≥n, no query reads the left half of this block, since a query of level h in this block has m≤r<n by Lemma 2, and these entries may hold anything. The right half is the same with T[h][i]=A[m…i−1]⋅A[i]=A[m…i]; it stops being written at i≥n, and later entries of the block are never read. □
Lemma 2 (level). Let 0≤l<r<N and let h be the bit length of l⊕r. Then 1≤h≤K, and with b=2h⌊l/2h⌋ and m=b+2h−1, we have b≤l<m≤r<b+2h.
Proof. l=r, so l⊕r=0 and h≥1; both are below 2K, so h≤K. The bits of l and r at positions ≥h are equal, so ⌊l/2h⌋=⌊r/2h⌋ and both lie in [b,b+2h). Bit h−1 is the highest bit where they differ, and l<r, so it is 0 in l and 1 in r. Inside the block, bit h−1 is 0 exactly on [b,m) and 1 exactly on [m,b+2h). □
Theorem. QUERY(l,r)=A[l…r] for 0≤l≤r<n, with at most one operation of S.
Proof. If l=r, line L1 returns A[l]. Otherwise, by Lemma 2 with the level h of line L2, l<m≤r, so by Lemma 1, T[h][l]=A[l…m−1] and T[h][r]=A[m…r], both with indices below n. By the split law, line L3 returns A[l…m−1]⋅A[m…r]=A[l…r]. □
Cost. Each level of BUILD performs at most one operation per position i<n and 2h−1 assignments without an operation per block, so at most n operations per level and nK in total; the table has at most nK entries. A query is O(1) word operations plus one operation of S.