Sparse Table & RMQ (Range Minimum Query)

Sdílet
Vložit
  • čas přidán 14. 05. 2024
  • Tutorial on Sparse Table data structure. We use it to solve Range Minimum Query by first storing minimum for every interval with a length equal to some power of 2.
    problem links: www.spoj.com/problems/RMQSQ/ & cses.fi/problemset/task/1647
    code github.com/Errichto/youtube/b...
    Coding live streams - / errichto
    FAQ - github.com/Errichto/youtube/w...
    Dsicord server - / discord
    Subscribe for more educational videos on algorithms, coding interviews and competitive programming.

Komentáře • 133

  • @ayushgaba7089
    @ayushgaba7089 Před 3 lety +201

    He's the best competitive programmer in my opinion. He devotes so much to the community. Thankyou errichto!

  • @utkarshdevgan6199
    @utkarshdevgan6199 Před 3 lety +85

    This is the greatest tutorial I have ever seen, soo detailed and crystal clear!!!!!!

  • @saikumarganganapalli5957
    @saikumarganganapalli5957 Před 3 lety +41

    The transition from logorithm to constant complexity for each query had put a smile. That was so cool.😎. Excellent explanation. Keep it going.

    • @Errichto
      @Errichto  Před 3 lety +14

      I agree that the transition is nice and satisfying.

  • @vishnusingh4118
    @vishnusingh4118 Před 3 lety +50

    This is a work of art and beauty! There are people who can do but can't teach. Then there are those who can maybe teach but can't do. Then there are legends, who can do both at a mediocre level. And then God said, "Let Errichto be". 🙌. You make complex stuff seem so mind-blowingly simple! Can't thank you enough!

    • @Errichto
      @Errichto  Před 3 lety +39

      I think you're exaggerating but still - thank you!

  • @rajd7002
    @rajd7002 Před 3 lety +1

    Finally a video that perfectly explains the core concept. I was able to come up with log n approach before you explained and was so proud of myself. And then you dropped constant time which blew my mind. :D Thanks Errichto!

  • @nighteagle6297
    @nighteagle6297 Před 2 lety

    You are the best competitive programmer. Very thanks. I understood it very clearly. Thanks for spending a lot of time to explain us. Thank you Errichto!

  • @asifanwarsajid8332
    @asifanwarsajid8332 Před 3 lety

    Read about sparse table 2 days back. I checked if you had made any video on it. And here it is. 💯

  • @hitesh6856
    @hitesh6856 Před 3 lety

    Video quality and explanation is soo good.
    Thank-you!!

  • @mrdude1084
    @mrdude1084 Před 2 lety

    I was looking for sparse table tutorial. When I saw Errichto's video in the list, I knew I couldn't get any luckier.

  • @kxb6098
    @kxb6098 Před 3 lety +1

    Clear, concise, and perfect explanation

  • @loonshott
    @loonshott Před 2 měsíci +1

    Thanks for this amazing course. Your explanation is just in another level.

  • @sameerbamnaha2189
    @sameerbamnaha2189 Před 3 lety

    Amazing tutorial!!! Really enjoyed it and learned a lot!

  • @sureshchaudhari4465
    @sureshchaudhari4465 Před 2 lety

    Errichto this is so nice and simple explanation I read it in book but did not understand you are really nice at explaining

  • @CrystalSergeant
    @CrystalSergeant Před 3 lety

    I love even more your recent videos

  • @sourandbitter3062
    @sourandbitter3062 Před 3 lety

    This is very cool. Eager to learn about the segment tree.
    I didn't know about this. There surely are good use cases for this algorithm. For sure segment trees, or a similar structure, are used in databases.

  • @MehbubulHasanAlQuvi
    @MehbubulHasanAlQuvi Před 3 lety

    Keep up these good tutorials. Thanks a lot

  • @farhan787
    @farhan787 Před 3 lety +7

    Finally Errichto is back :)

  • @__agpatel__
    @__agpatel__ Před 3 lety

    He is really great and down to earth person....always willing to do something good for community....👍👍👍

  • @rishabhkalra9505
    @rishabhkalra9505 Před 2 lety

    that was indeed a really good explanation to sparse tables. Thank you for this video. :)

  • @beaujamo
    @beaujamo Před 4 měsíci

    Thank you! Got mine working now! Awesome tutorial!

  • @gauravupreti9340
    @gauravupreti9340 Před rokem

    Best Explanation, you made it so easy to understand. Please keep up the good work 😊

  • @joserenteria5899
    @joserenteria5899 Před 2 lety

    i love learning from Errichto more than my own profs

  • @manjuender6286
    @manjuender6286 Před 3 lety

    Woah explained so simple, seems like a complex math but it is not once you understand the intent behind thanks a lot for the video

  • @ayamtaken2580
    @ayamtaken2580 Před 3 lety +15

    I don't understand anything you're saying, but your voice is so soothing like ASMR to me

  • @ashisharyan3028
    @ashisharyan3028 Před 3 lety

    Thank,I just needed this perfect and Crystal clear explanation .Orz

  • @coefficient1359
    @coefficient1359 Před 3 lety

    Very nice explanation Errichto

  • @magnuseifr
    @magnuseifr Před 3 lety +13

    c++ also has builtin function __lg for base-2 logs with integers. Nice and clear explanation (as always)!

    • @Errichto
      @Errichto  Před 3 lety +10

      Oh, right.

    • @GGxEpsilon
      @GGxEpsilon Před 3 lety

      I think log2 does the same, doesnt it?

    • @magnuseifr
      @magnuseifr Před 3 lety

      @@GGxEpsilon I think __lg belongs to the gcc compiler and is designed to work on integers (or long longs) specifically. log2 should have the same behaviour, but as Errichto mentions in the video: floating point values are scary to work with.

  • @AnkitJosh
    @AnkitJosh Před 3 lety +1

    Thanks ☺️ , this lecture was really helpful.

  • @anowlwithinternet9125
    @anowlwithinternet9125 Před měsícem

    You never dissappoint!

  • @plosslaw
    @plosslaw Před 3 lety

    Amazing stuff, good explanation

  • @hiderr6805
    @hiderr6805 Před 3 lety

    Dzięki za pomoc w przygotowaniu do Olimpiady Informatycznej Juniorów (:

  • @ejackkulation7377
    @ejackkulation7377 Před rokem

    Thank you, Errichto.

  • @subarnodatta2181
    @subarnodatta2181 Před 3 lety +1

    Wonderful video. Helped me a lot sir

  • @iftekharfahim
    @iftekharfahim Před 3 lety

    Please do some Videos on Data Structure.
    Your explanations are simple and precise.

  • @anshusingh2473
    @anshusingh2473 Před 2 lety

    today i am practice codeforces Div2 C problem but i am not able to solve efficiently and seen editorial sparse table algorithm is used then learn this from your lecture then simply solve and learn this concept smart way very thankful for you explanation

  • @BrunoSouza-wy2et
    @BrunoSouza-wy2et Před 3 lety +2

    amazing dude, I've been following your videos for a year now and you are always improving the way u teach. thanks !

    • @Errichto
      @Errichto  Před 3 lety +7

      Let's hope that I will keep improving then :)

  • @5590priyank
    @5590priyank Před 3 lety

    Amazing tutorial, for so long I was not sure how queries take constant time and not logN time. I always thought we need to answer each queries such that we divide range into powers of 2... finally your tutorial solved that for me that we need to take largest power of 2 which fits in given range and check from L and R end point and hence it is O(1). thank you so much for clearing this :)
    Also this clears why we can't use sparse table for answering range sum queries in O(1) because we are using overlapping ranges, right?

  • @simonstrandgaard5503
    @simonstrandgaard5503 Před 3 lety +1

    Great explanation.

  • @mjKeszely
    @mjKeszely Před 3 lety

    Duma rozpiera, że rodak jest takim mózgiem :)

  • @glaucoa.9214
    @glaucoa.9214 Před 2 lety

    Erichto you are the best person in the world!

  • @vwv.1d
    @vwv.1d Před rokem

    may you get all of what you want in life..i respect you

  • @ahmmedsakibnoman2849
    @ahmmedsakibnoman2849 Před 3 lety +11

    plz do segment tree.. waiting for a long time..

  • @siddharthmagadum16
    @siddharthmagadum16 Před 3 lety

    He's sooo clever. Thanks for the video

  • @vineethkumar8132
    @vineethkumar8132 Před 3 lety

    Wow! its so clear even a 5 year old IQ guy lile me understands , Thank you for the wonderful content Ericchto !

  • @Vikash_Art
    @Vikash_Art Před 2 lety

    For computing "k" in the query function we can do k=floor(log2(len)) then we can have 2^k

  • @vali8924
    @vali8924 Před 3 lety

    Very nice explanation

  • @shivam6829
    @shivam6829 Před 3 lety

    Please make detailed videos on segment tree and it various applications( like, min , max, updating, sum, frequency of max frequent element in the asked range etc...)

  • @ankitvats3295
    @ankitvats3295 Před 2 lety

    What an explanation! op

  • @simranvedpathak7112
    @simranvedpathak7112 Před 2 lety

    Thank you !

  • @priceandpride
    @priceandpride Před 3 lety +1

    The brain on this man

  • @Dima-uz8gi
    @Dima-uz8gi Před 3 lety

    Many thanks!

  • @josealejandrovaroncarreno1692

    the best youtuber

  • @apoorvmehra121
    @apoorvmehra121 Před 3 lety

    Amazing👍🏽👍🏽

  • @srishtikdutta8946
    @srishtikdutta8946 Před 3 lety +1

    Awesome!!

  • @rishabhpurwar4875
    @rishabhpurwar4875 Před 3 lety

    You're amazing

  • @p4rzival127
    @p4rzival127 Před 3 lety

    This reminds of Reversort in Codejam Quali

  • @shaoxiongduan565
    @shaoxiongduan565 Před 3 lety

    legend.

  • @kongzilla2897
    @kongzilla2897 Před 3 lety

    Nice!! Binary is always Beautiful :)

  • @GGxEpsilon
    @GGxEpsilon Před 3 lety +2

    Errichto orz ❤️

  • @ZakiFootballVideos
    @ZakiFootballVideos Před 2 lety

    Thank you

  • @i_am_wiz
    @i_am_wiz Před 9 měsíci

    Won’t the time complexity for answering a single query still logN as we are computing log2(len) where len is R - L + 1 which in worst case is of the order (N). So won’t the time complexity be logN per query?

  • @madhvansharma5917
    @madhvansharma5917 Před rokem

    Can we use sparse table for sum queries if we use the method where we convert ranges to binary and add it like we do in binary lifting (query time = logN)?

  • @nileshchandra6435
    @nileshchandra6435 Před 2 lety

    Should have checked this out before, not knowing sparse tables cost me yesterday as the seg tree failed in time.

  • @atharvasalokhe5168
    @atharvasalokhe5168 Před 3 lety

    Hello guys, I am errichto, is so cool to hear

  • @tripleaplusb2214
    @tripleaplusb2214 Před 3 lety +1

    Hi , can I know the name of program used to explain pls?

  • @manas_singh
    @manas_singh Před 3 lety

    How do you find problems on SPOJ? It seems so difficult to find by tags.

  • @learning7517
    @learning7517 Před rokem

    I agree that using log2(x) is something to be avoided generally, however, if x < 2^31, it is guaranteed that the result of (int)log2(x) will be the same of any the bit tricks, so it might be a good choice if it's faster to code.

  • @darshankubavat1764
    @darshankubavat1764 Před 3 lety

    Errichto can you please make a video on how to deal with burnouts we face while constant coding?

  • @userrand4900
    @userrand4900 Před 10 měsíci

    Is the condition in line 30, correct? It does not reflect m[i + (1

  • @isma--
    @isma-- Před 2 lety

    Hey Errichto, one question, what do you use in order to write your notes, i think you use like a tablet or something

  • @therealcdubbb8040
    @therealcdubbb8040 Před 2 lety

    Can you have multiple overlapping intervals?

  • @andrijaciric4661
    @andrijaciric4661 Před 3 lety

    It would be nice if you covered CSES problemset. It has a lot of educational problems that are hard to solve if you don't know the right technique.

    • @KuaisArts
      @KuaisArts Před 3 lety

      check out CPH. CSES is a supplementary problem set to that book. Good luck!

  • @gnet888
    @gnet888 Před 3 lety

    Thanks You

  • @tihomirjovicicify
    @tihomirjovicicify Před 3 lety

    awesome

  • @jakoolaboo
    @jakoolaboo Před 3 lety +1

    Thank you for this great video
    BTW : Is the __builtin function O(1) or O(log) or what ??

    • @hitesh6856
      @hitesh6856 Před 3 lety

      It is O(number of bits), so it is essentially O(1)

    • @Errichto
      @Errichto  Před 3 lety +1

      @@hitesh6856 It's O(1) rather than O(number of bits). CPU doesn't need to iterate bits one by one, it does it faster just like other basic operations like addition.

    • @hitesh6856
      @hitesh6856 Před 3 lety

      ​@@Errichto ohh. I didn't knew that. Thanks! big fan..!

    • @jakoolaboo
      @jakoolaboo Před 3 lety

      @@Errichto thx for answering

  • @subarnodatta2181
    @subarnodatta2181 Před 3 lety +1

    I don't know why people disliked this video.🤷‍♂️🤷‍♂️

  • @shnerdz
    @shnerdz Před 3 lety

    please do segment trees next

  • @pavankumar-cy6mg
    @pavankumar-cy6mg Před 3 lety +1

    why it is k

  • @koushiksaigopalamalladi5994

    pls do video on disjoint set union

  • @sahilanand30
    @sahilanand30 Před 5 měsíci +1

  • @aakashvishwakarma4653
    @aakashvishwakarma4653 Před 3 lety

    💯💯💯

  • @asdfinonebody4103
    @asdfinonebody4103 Před 3 lety

    good !

  • @joshuawilson6907
    @joshuawilson6907 Před 11 měsíci

    I got WA with this code on spoj, do you guys have any ideas?

  • @rohitoze9359
    @rohitoze9359 Před 2 lety

    Line 27, shouldn't it be i

  • @QngTu1159
    @QngTu1159 Před 3 lety +1

    Can someone explain me what is “1

    • @leviackerman9882
      @leviackerman9882 Před 3 lety

      Search bitwise left shift and right shift on CZcams
      Also i think errichto has a video on bit manipulation where he explained left shift and right shift

    • @leviackerman9882
      @leviackerman9882 Před 3 lety +1

      Here you go :
      czcams.com/video/xXKL9YBWgCY/video.html

    • @QngTu1159
      @QngTu1159 Před 3 lety

      @@leviackerman9882 Tks u so much

    • @apoorvmehra121
      @apoorvmehra121 Před 3 lety

      It means 2 to the power k

  • @Steve168xyz
    @Steve168xyz Před 3 lety +1

    Thanks for this vid - i like your background - so the goal to the get the O(N*log((N) + Q) though N is increasing tremendously ?

    • @Errichto
      @Errichto  Před 3 lety +1

      It's O(N*log(N) + Q) so it makes a lot of sense if Q is huge.

  • @BGOPC
    @BGOPC Před 3 měsíci

    2 year's in and still waiting for the segment tree video

  • @nalinnishant5249
    @nalinnishant5249 Před 2 lety

    We can also solve this problem using sliding window concept in O(n) time

  • @shrikantpadhy6125
    @shrikantpadhy6125 Před 2 lety

    Segment tree can also do the same right? So whats the advantage here?

    • @mohansharma-pt6yk
      @mohansharma-pt6yk Před 9 měsíci

      Time complexity becomes O(N+Q*logN) and improves space complexity too

    • @skt7088
      @skt7088 Před 9 měsíci

      @@mohansharma-pt6yk in segment tree time complexity is O(QlogN)?

  • @anshusingh2473
    @anshusingh2473 Před 3 lety +3

    your balance bracket concept apply div2 B problem in contest get Accepted and my rank under 2500 thanks for you good job

    • @Errichto
      @Errichto  Před 3 lety +3

      :)

    • @puneetgoel7416
      @puneetgoel7416 Před 3 lety

      What balance bracket concept?
      Could u pls share the link of that video 🙏🙏

    • @anshusingh2473
      @anshusingh2473 Před 3 lety

      czcams.com/video/piT58dNLPhg/video.html

  • @parashararamesh4252
    @parashararamesh4252 Před 3 lety +1

    How would we handle updates?

    • @Errichto
      @Errichto  Před 3 lety +2

      You need a segment tree for that.

  • @MehbubulHasanAlQuvi
    @MehbubulHasanAlQuvi Před 3 lety

    First View, Like and Comment

  • @mdshahadatkabir8329
    @mdshahadatkabir8329 Před 3 lety

    Please do make how and when u started doing cp. Love your videos from Bangladesh

  • @skullcode8856
    @skullcode8856 Před 2 lety

    10:00 this was when I started thinking that sparse tables are clever.

  • @Shirolicious
    @Shirolicious Před 2 lety

    i dnno, this guy is godtier. I cannot compute. syntax error

  • @dhakad22klx
    @dhakad22klx Před rokem

    Easily explained.

  • @joshuawilson6907
    @joshuawilson6907 Před 11 měsíci

    I can AC the cses problem but cant for the spoj :))

  • @tanaykulkarni822
    @tanaykulkarni822 Před 3 lety

    It is like segment tree

  • @daark3113
    @daark3113 Před 3 lety

    I have no idea what queries are, and i am trying to learn

  • @anonymoussloth6687
    @anonymoussloth6687 Před rokem

    I saw this somewhere to find largest power of 2 less than or equal to n:
    x=n
    While(x&(x-1))
    x&=x-1;
    Now x will be the answer