### peltorator's blog

By peltorator, 15 months ago, translation,

Hi!

I made a video on the disjoint sparse table. It's a generalization of a sparse table that expands the possibilities of its application.

The video is in Russian but English subtitles are available. I'd be glad if you watch the video and leave a comment below with your impressions, thoughts, and ideas for future videos. You may also want to text me on telegram if you didn't understand something or you have any questions. I'll be glad to answer!

I'm gonna make more videos in the future. Both on basic algorithms such as prefix sums, binary search, sorting, etc., and also some advanced topics such as heavy-light decomposition, link-cut tree, lambda optimization, FFT, and so on. If you're interested, consider subscribing to my channel!

My disjoint sparse table realizations:

Easy one

Easy to use with templates

Without extending to the power of two

Codeforces group with contest

Good luck!

• +404

 » 15 months ago, # | ← Rev. 2 →   +44 Nice video! I'm glad that someone has finally told the wide audience about disjoint sparse tree (which I always called "non-idempotent sparse tree"). I have a couple of comments to add: I was initially told about this ds without a concept of segment tree. Instead, one can enumerate the (imaginary) bars between positions: 0 1 2 3 4 5 6 7 8 9 ... |3|1|4|1|5|9|2|6|5|3... Now it's easy to see that on each (nonempty) subsegment there is the only bar with the greatest power of two in its factorization (basically, the greater this power is, the higher is the level in your segment tree where you store it). This approach gives another way to think of a bit formula of access index. There is the exact same problem you mentioned (namely, product on a subsegment online with composite modulo): https://www.codechef.com/NOV17/problems/SEGPROD The manim creator you mentioned is Grant Sanderson, not Grant Sanders P.S.: it was not intentional that the numeration in this comment is broken
•  » » 15 months ago, # ^ | ← Rev. 2 →   +34 Thank you for your comment! Yeah, your approach is really nice. It's always interesting to watch something from different perspectives.And thank you for the name correction :) I fixed it now. Probably I should eat less often at KFC.
 » 15 months ago, # | ← Rev. 2 →   +34 At around minute 16 your (English) CC switch to Russian. Otherwise, great video! Never thought about this idea. By the way, how long did it take you to animate everything?
•  » » 15 months ago, # ^ | ← Rev. 3 →   +43 Oh wow! Thank you for noticing it. English subtitles were autogenerated from Russians. I'll investigate this problem.I don't know how long it took specifically to animate it but I spent around 80 hours overall on this video including learning how to use manim library.Thank you so much for your comment!UPD: I fixed English subtitles.
 » 15 months ago, # |   +30 In the future, will you consider making videos in english? I know that there are english subtitles, but I prefer to be focusing on the diagrams and animations instead of the subtitles and what they say. Well, I guess that this also depends on your viewer base.
•  » » 15 months ago, # ^ | ← Rev. 2 →   +31 Yeah, I understand that it's hard to watch a Russian video with English subtitles especially if they are autogenerated. But it's better than nothing I suppose.I'm definitely considering this opportunity. Maybe someday in the future, I'll do it. Thank you for your suggestion! Stay tuned.
 » 15 months ago, # |   +18 Some of your links are not clickable because of ​ character at the end, maybe retyping the last characters of the links would help.
•  » » 15 months ago, # ^ |   +8 Oh, yeah, it worked. Thanks!
 » 15 months ago, # |   +18 USACO Guide also has a blog that describes a similar idea (its pretty much the same, but they implement it differently): linkThe video was very well made btw.
•  » » 15 months ago, # ^ | ← Rev. 3 →   +18 Seems like different people know this idea but call it differently. Thanks for sharing! Maybe from this moment, the name will stick.
 » 15 months ago, # |   0 Awesome! Can you share your workflow to sync audio and video?
•  » » 15 months ago, # ^ |   +8 Hi! What do you mean by "sync audio and video"?
•  » » » 15 months ago, # ^ |   0 Like, how do you edit the video such that the right animation is played at the right time? What is your setup?
•  » » » » 15 months ago, # ^ | ← Rev. 2 →   +21 I use manim python library for animation. Then I record audio and after that, I can manage timings in my code so that they synchronize.
•  » » 15 months ago, # ^ | ← Rev. 2 →   0 I think he would let the animation play and voice-over it.
 » 12 months ago, # |   0 Nice tutorial.... learned a lot of new stuff.... One request. Can anyone provide the code for the last two problems showed in the video? Thank You.
 » 12 months ago, # |   0 Amazing tutorial! You should make more. I loved the way you explained with the visuals. Really great work there. Also thanks for preparing the contest.
 » 12 months ago, # |   0 I've somewhat simplified the implementation here, albeit making it a bit less readable by using shorter names :) Sharing in case it helps future readers.A few more things to note about this technique. Although it's a cool trick, it's also pretty much useless practically speaking. Because we can still use the regular sparse table to answer queries in O (log n) without partial overlaps, and that is pretty fast. I was able to pass all 4 problems in the contest this way, without using discrete sparse tables.The only valid use case I can think of is when there is going to be a large number of queries, like SEGPROD or also the first problem from the contest where Q=3*107. And even then, I believe most of the time we can use the regular sparse table as opposed to the discrete version.
•  » » 12 months ago, # ^ |   0 By that argument regular sparse tables are useless too, because you can just use segment trees (which is a good point). I personally think that disjoint sparse tables are kind of useless practically because Regular sparse table precalculation runs faster than the disjoint version Regular sparse table implementation is simpler In practice very few problems require the disjoint version. Sparse table is simply good enough for a lot of applications. However I still find the disjoint sparse table to be very interesting. I wonder if it is possible to make an implementation of disjoint sparse table that rivals the simplicity and speed of the regular sparse table.
•  » » » 12 months ago, # ^ | ← Rev. 4 →   0 Yes, those are good points too. As for segment trees vs sparse tables, sparse tables have significant advantages over segment trees. They are simpler, faster, and queries are O(1) if overlaps are not an issue that is.And like you said, for most problems overlaps are fine. For queries on a static array, where overlapping is undesirable, most of the time we can just use a prefix array instead of the disjoint sparse table. Think sum, xor, polynomial hash, etc.TLDR, I don't think disjoint sparse tables are overall very useful because I have seen only a handful of problems requiring them. And I can't think of a valid use case unless the problem setter makes the time limit unnecessarily strict with the sole intent of disallowing regular sparse tables. Apart from that, the only valid use case I can think of is when squeezing a sub-optimal solution to pass in the rare scenario where overlapping ranges may be problematic, and we cannot use a prefix array too.
•  » » » 12 months ago, # ^ | ← Rev. 2 →   0 Disjoint sparse tables are not that slow though. The implementation and simplicity do not differ significantly when compared with the normal one. Although, the non-disjoint version is very easy to understand and intuitive.I didn't do a thorough benchmark, but roughly my disjoint sparse table implementation is 1.5x slower than the normal one. Building the sparse table that is, queries should have the same performance.I think it can be improved but it won't add much value because if we need to use the disjoint version, usually building the table won't be the bottleneck.
•  » » » » 12 months ago, # ^ |   0 The way I think of it is that with segment trees, you do the majority of the work during queries. And for sparse tables you do a majority of the work at the precalculation. One big reason why sparse tables are so nice is because their precalculations can be done in a very fast and cache friendly way. (Unlike queries in a segment trees)