Heavy light decomposition: The hardest competitive programming algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ม.ค. 2025
  • Heavy light decomposition is an advanced efficient range queries technique on trees. The algorithm uses least common ancestors and segment trees to answer queries.
    We use binary lifting and segment trees as prerequisites for this video. Feel free to post your doubts or suggestions on the comments.
    References:
    HLD Problem Statement: www.spoj.com/p...
    HLD blog: blog.anudeep20...
    LCA using Binary Lifting: • Least Common Ancestor ...
    Segment Trees: • Segment Tree - Range Q...
    Code: ideone.com/EeDdrA
    System Design Playlist: • System Design Playlist
    You can follow me on:
    LinkedIn: / gaurav-sen-56b6a941
    Twitter: / gkcs_

ความคิดเห็น • 51

  • @raghavbansal4403
    @raghavbansal4403 5 ปีที่แล้ว +24

    Thanks a lot bro everytime waiting for heavy light decomposition from you.

  • @tppt3987
    @tppt3987 5 ปีที่แล้ว +8

    This is the only place where I could find such an easy explanation to HLD Algorithm. I had learnt it the hard way.

  • @snider16661
    @snider16661 4 หลายเดือนก่อน

    Really concise and to the point without any rambling like most videos on this topic.

  • @ale9507
    @ale9507 3 ปีที่แล้ว +3

    Absolutely fantastic. You explained this brilliantly. Very surprised I was actually able to get this with one watch-through. The pacing was great too.
    Thank you.

  • @tejassardana6266
    @tejassardana6266 5 ปีที่แล้ว +7

    Quality, Quality and more quality.....in every damn video on this channel.

  • @AshiqurRahman
    @AshiqurRahman 3 ปีที่แล้ว

    Very nice tutorial. You put a lot of efforts creating this video. Thank you...

  • @animeshkumar_corporatevlogger
    @animeshkumar_corporatevlogger 5 ปีที่แล้ว +1

    amandeep bhaiya rocksssss!!! making BIT MESRA proud!!

  • @ritiksingla9946
    @ritiksingla9946 4 ปีที่แล้ว

    Respect and love. Keep making advance videos in the future for the ones in need like me.

  • @DarshanSenTheComposer
    @DarshanSenTheComposer 5 ปีที่แล้ว +2

    Really well explained! Liked before watching it. Didn't regret it at all. :)

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Thanks 😁

  • @shotakhakhishvili8640
    @shotakhakhishvili8640 2 ปีที่แล้ว

    That intro was golden

  • @ranjeetupadhyay7486
    @ranjeetupadhyay7486 4 ปีที่แล้ว +3

    Anudeep nekkanti!!, i am curious to know more about him, i had watched his few videos long back and then still in a question... What he's doing now!!
    (His profile says he left Goole long back)
    And the most interesting part is that you, akshay, anudeep, none of you are from IITs still so better than many.
    And the best part is that you always mention your references & give them credits. :)
    You're really great 👍

  • @raghavendrac4710
    @raghavendrac4710 7 หลายเดือนก่อน

    Amazing explanation Sir!

  • @abdullahalnayem1849
    @abdullahalnayem1849 4 ปีที่แล้ว

    You made it so simple! Thanks brother.

  • @amangupta687
    @amangupta687 5 ปีที่แล้ว +4

    Hey Gaurav, I really like your videos. I want to know how you make those videos. I want to start a YT channel about teaching C/C++ but I am confused about all initial setup that needs to be done. Specifically, I want to know the following things:
    1. I want to know which whiteboard are you using (type and size) and where did you get it? (I couldn't find any good ones on Amazon and don't have any whiteboard store in my city)
    2. Are you using any special camera or is it just a phone camera?
    3. What lights and microphone? Are you using any softlights?
    4. Which software do you use to edit your videos?
    I know there are a ton of content available regarding this but I really like your channel and wants to know what you use. I would really really appreciate your help. Thanks a ton!!

  • @paragggoyal1552
    @paragggoyal1552 ปีที่แล้ว

    for this particular question why don't we use Euler tour and store sum from root, and segment tree in Euler tour.
    basically what we can do:
    1. store the distances and sums from the root to the node on the node.
    1. three Euler tours, - 1st for distances. 2nd for sums, and third for node values itself.
    create a segment tree on Euler tour of sums.
    for first query.
    just get lca from distance Euler tour and RMQ, and sum between two (a, b) can be done using in O(1) using sums.
    time complexity O(1) or O(logn)
    for second query.
    just update the values in the subtree of sums Euler tour.
    time complexity O(logn)
    P.S. my explanation is horrible and you will only be able to understand if you know how to Euler tour, LCA from Euler tour and segment tree on Euler tour.

  • @pepehimovic3135
    @pepehimovic3135 8 หลายเดือนก่อน +1

    Around 8:22, should it not be the height of each sub tree, not the size? I reason that that should be more efficient since we want to maximize the size of each chain

    • @pepehimovic3135
      @pepehimovic3135 8 หลายเดือนก่อน

      8:46, yes, we want to attach as many nodes to any given segtree (greedily), so why use size of sub trees and not height?

    • @gkcs
      @gkcs  8 หลายเดือนก่อน

      I think it has to do with the worst case query time if we go down a node. Can't think of any other reason.
      Maybe you'd like to benchmark this version with the standard one? I'd love to see the results!

  • @yashjain3393
    @yashjain3393 5 ปีที่แล้ว +1

    excellent explanation.
    keep doing the great work.

  • @saurabhsharma7123
    @saurabhsharma7123 5 ปีที่แล้ว +1

    Very very very thankful to you!!

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      😁

  • @AkashKumar-wu4sv
    @AkashKumar-wu4sv ปีที่แล้ว

    We can use to binary lifting to up word traversal of a node

  • @aashishsingh6148
    @aashishsingh6148 5 ปีที่แล้ว

    most awaited video

  • @chahatagrawal1441
    @chahatagrawal1441 4 ปีที่แล้ว +3

    sir your code is giving TLE on spoj. U can check !!

  • @maxgoldwell9525
    @maxgoldwell9525 4 ปีที่แล้ว +1

    Awesome video! What's the best way to support range updates? Just standard lazy propagation or is there something better?

    • @gkcs
      @gkcs  4 ปีที่แล้ว +2

      Lazy propagation keeps it down to O(logN) per chain. That's good enough :)

  • @mamtachahal1277
    @mamtachahal1277 4 ปีที่แล้ว

    This some amazing editing :

  • @abcsumits
    @abcsumits 11 หลายเดือนก่อน

    Why not just dsu , instead of dfs ,chain can be denoted by ultimate parent,and union by size will keep track of size

  • @JavaAidTutorials
    @JavaAidTutorials 5 ปีที่แล้ว +1

    *Interesting stuff*..👍

  • @chiragchatwani9124
    @chiragchatwani9124 5 ปีที่แล้ว +4

    Can you please make videos on design patterns in oops
    [

  • @HimanshuSingh-rc8zy
    @HimanshuSingh-rc8zy 5 ปีที่แล้ว +12

    Gaurav:
    Memer>Programmer
    lol;

  • @sleepntsheep1169
    @sleepntsheep1169 ปีที่แล้ว

    Thank you!

    • @gkcs
      @gkcs  ปีที่แล้ว

      😁

  • @kristyburton6507
    @kristyburton6507 4 ปีที่แล้ว +1

    Awesome 👏

  • @prajawalpandey7261
    @prajawalpandey7261 4 ปีที่แล้ว

    Doubt in the code...
    Hey Gaurav,
    In the code is subtree_size array getting updated in dfs function right? It gets incremented only by one for each node whereas it should get calculated from downwards and size of each child should be added na?

  • @pankeshgupta4006
    @pankeshgupta4006 5 ปีที่แล้ว +23

    Trees everywhere trees , what the hell is this 😂

    • @KdPisda
      @KdPisda 5 ปีที่แล้ว

      :joy exactly

    • @prabhatism
      @prabhatism 5 ปีที่แล้ว +1

      DS class

  • @sujoyseal195
    @sujoyseal195 4 ปีที่แล้ว

    Hi Gaurav, can you please suggest a good resource for DP on trees. TH-cam is not helping me that much

  • @pawankumarmeena6737
    @pawankumarmeena6737 5 ปีที่แล้ว +2

    charlie got me! :)

  • @lokeshreddy7755
    @lokeshreddy7755 5 ปีที่แล้ว +1

    Thank you!!! gaurav

  • @amber9643
    @amber9643 9 หลายเดือนก่อน

    best video

  • @vijaykumarsubrambani1464
    @vijaykumarsubrambani1464 5 ปีที่แล้ว +1

    Updating value will not change the segment?

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      The segment tree will handle it in O(logN) time.

  • @ArshdeepSingh-rh3zb
    @ArshdeepSingh-rh3zb 4 ปีที่แล้ว +1

    thankyou

  • @ДенисГрулев
    @ДенисГрулев 5 ปีที่แล้ว

    Ah... There are link-cut trees also and they are a step harder to my mind.

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Yes...😛

  • @Sagar-dh1si
    @Sagar-dh1si 2 ปีที่แล้ว +1

    Idk why i am watching this..

  • @jayantprakash6425
    @jayantprakash6425 4 ปีที่แล้ว +2

    Advanced algo? TH-cam? ---> Gaurav Sen

  • @vivekchoudhary8745
    @vivekchoudhary8745 5 ปีที่แล้ว

    nice