Lazy Propagation in Segment Tree | Point and Range Updates | Live Coding

Поділитися
Вставка
  • Опубліковано 3 жов 2024

КОМЕНТАРІ • 86

  • @kaushikm3810
    @kaushikm3810 4 роки тому +37

    Hi Striver ,
    There is an error in line no : 30 at 3:08 timestamp of video which should be seg[segInd] += val;

    • @takeUforward
      @takeUforward  4 роки тому +14

      Oops yes.. thanks! Typo :(

    • @chaitanyakumar9229
      @chaitanyakumar9229 3 роки тому +1

      and a[low] += val;

    • @gurmeetsingh2016
      @gurmeetsingh2016 2 роки тому +2

      @@takeUforward Bhaiya , is there any need to add a condition of low>high since if we go out of range then r

    • @arten8281
      @arten8281 6 місяців тому +1

      Aren't all same? Low=high=ind?

    • @Entertainmentexe
      @Entertainmentexe 3 місяці тому

      @@arten8281 No

  • @takeUforward
    @takeUforward  4 роки тому +43

    Thank you for the constant support ! :)

  • @shreyasnagabhushan4918
    @shreyasnagabhushan4918 Рік тому +25

    Hi Striver ☺ , There are errors in LINE NO : 68 and 69 :
    in the rangeUpdate() function , it should be :
    if(low != high){
    lazy[2*ind+1] += val ;
    lazy[2*ind+2] += val ;
    }
    This can be verified by taking the following test case :
    say we have 2 queries ,
    1st query (update query) :
    l = 0, r = 2, val = 1
    so the array becomes -> 3 4 2 4 2 5 2 3 1 5
    2nd query (range sum query) :
    l = 0, r = 1
    correct output should be 3 + 4 = 7 , BUT the code shown will give the sum as 5 I.e (2 + 3)
    The reason for that error is because if you notice in the function rangeUpdate() , first we check if there is lazy[ind] > 0 , if it is we update the segment[ind] and then carry forward the lazy[ind] to left and right nodes and THEN set the lazy[ind] = 0 . So every time we enter the rangeUpdate() function, we make sure lazy[ind] becomes 0
    Now suppose we have met the condition that the interval(low and high) entirely lies within l and r, so we update the segment[ind] as in line no 66 , BUT THEN while we are setting the lazy[left_ind] and lazy[right_ind] we set it to lazy[ind[ , which is ALWAYS 0, hence not updating the lazy[left] and lazy[right]
    So we make lazy[left_ind] += val and lazy[right_ind] += val ;
    BTW thank you so much for your crystal clear explanation !!

    • @parthhh3589
      @parthhh3589 9 місяців тому

      Instead of +=val shouldn't it be += (range of left node) * val?

  • @ashishsingh-ty6kf
    @ashishsingh-ty6kf 4 роки тому +60

    Hii I guess There is a slight error at 18:37 in rangeUpdate when segment completely lies inside range it shuold be lazy[2*ind+1] += val lazy[2*ind+2] += val

    • @jethalalnhk2409
      @jethalalnhk2409 4 роки тому +2

      yes you are right but he hasn't read your comment yet ?

    • @codenrun6013
      @codenrun6013 4 роки тому +1

      yeah, correct.

    • @ramanthakur74
      @ramanthakur74 3 роки тому +1

      he should update this, i think he hasn't read this comment yet!

    • @manaskumarpanda3343
      @manaskumarpanda3343 2 роки тому

      He consider 0-based indexing of segTree and LazyTree and thats why he done this :)

    • @mayankpushpjeet399
      @mayankpushpjeet399 Рік тому

      @@manaskumarpanda3343 No read the comment carefully at the second step it should add val not the previous lazy value which already added above.

  • @agamjain8976
    @agamjain8976 4 роки тому +11

    This channel is my Constant motivation source!!

  • @divy04
    @divy04 2 місяці тому +2

    in the querysum upadte, the val parameter passed in the function, was of no use

  • @viditbhardwaj3061
    @viditbhardwaj3061 4 роки тому +23

    on line no. 68 & 69 it should be +=val not +=lazy[indx] in range update

  • @Fictional_universe
    @Fictional_universe 4 роки тому +10

    hye striver?? could u plz make some vdos in xor properties?? like:--
    1. all pair xor sum in binary sequence / any sequence
    2. all subarray xor sum in any sequence
    3.prefix xor/and/or...
    plzzzzzz,,

  • @devanshmesson2777
    @devanshmesson2777 3 роки тому +5

    Thank you so much Striver, Its the best explanation on the internet!

  • @schan263
    @schan263 5 місяців тому +3

    The video started flashing green colour around 10:55 and it made me unwell.

  • @rakeshpandey9730
    @rakeshpandey9730 4 роки тому +5

    Hey ,Ur videos are amazing.. they are helping a lot .. keep posting such videos..is it possible for u to make video on DSU on trees and HL decomposition on trees... that would be a great help... Thanks

  • @tarunpatel8168
    @tarunpatel8168 4 роки тому +9

    Slight error in updatepoint code
    if(low==high)
    Update arr[node]+=val and seg[ind]+=val

  • @rijumondal6876
    @rijumondal6876 4 місяці тому

    After working whole day your video give me a hope for a future where I will be happy. Thanks a lot

  • @ashutoshdixit2806
    @ashutoshdixit2806 3 роки тому +1

    finally samaj aagya ....
    not need to watch another video
    thnx for concise and great content
    :)))

  • @ashutoshdubey8050
    @ashutoshdubey8050 Рік тому +1

    Bhaiya there is a slight mistake
    In point update code in the first if condition it should be seg[ind]+=val, not seg[low]+=val;

  • @girjashankar7925
    @girjashankar7925 3 роки тому +2

    for storing lazy update info ,we require extra space O(n),right?

  • @farhantanmoy1325
    @farhantanmoy1325 4 роки тому

    Sir, I am from Bangladesh, your video is very helpful to me.....

  • @koushiksahu68
    @koushiksahu68 4 роки тому +1

    In querySumLazy function I believe we don't need the int val parameter

    • @jethalalnhk2409
      @jethalalnhk2409 4 роки тому

      yes i was thinking the same it should by only lazysum(ind, low, high, L, R)

  • @himanshunegi5228
    @himanshunegi5228 3 роки тому +1

    if we have to update constant value from l to r then??

  • @aadityaupadhyay6264
    @aadityaupadhyay6264 4 роки тому +3

    Where does the index 3 lies on the left or on the right 😂
    That rhyme

  • @harshkanani6605
    @harshkanani6605 Рік тому

    Amazing Explanation striver sir! Thank you so much!

  • @AmanBawane-o9q
    @AmanBawane-o9q 4 місяці тому +1

    Loved your Accent bhai

  • @aadarshgupta5950
    @aadarshgupta5950 4 роки тому +1

    please make a video on - "how to optimize the JAVA code for Competitive Programming"

  • @jethalalnhk2409
    @jethalalnhk2409 4 роки тому +1

    didn't understood when 21 minutes were over. thank you so much

  • @AyushKumar-vh7pg
    @AyushKumar-vh7pg 4 роки тому +1

    Hello bhaiya I wanted to ask that are questions in coding round and interview of same type or different because in every site we get questions as interview questions and no site give questions as coding round questions. Pls reply and clear this doubt

  • @tamphamle7676
    @tamphamle7676 2 роки тому

    Thanks for your explaination . it helps me a lot

  • @ganeshkamath89
    @ganeshkamath89 2 роки тому

    If different nodes have different update values, like updating the value { 2,4,1,6,3,5} for the range [5 - 10], is it possible to use lazy update?

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    Very well explained. Understood

  • @codechefgeek5471
    @codechefgeek5471 3 роки тому

    Can u plz explain how to perform lazy propagation on segment tree ITERATIVELY

  • @shivdhandesh2002
    @shivdhandesh2002 4 роки тому +1

    Hello Bhaiya, I'm an SYBCA student but am determined to work at a big product based company. Will DSA and CP along with a few projects be enough?? Please guide.

    • @yashaggarwal825
      @yashaggarwal825 11 місяців тому +1

      bro did you get into a product based company?

  • @harshitjain128
    @harshitjain128 4 роки тому +1

    Hi bro, please try to upload the atcoder dp series'.

  • @oo7338
    @oo7338 4 роки тому

    Hey bro please make a video on vscode setup... for competitive programming...

  • @rockstarscientist2797
    @rockstarscientist2797 4 роки тому

    Bhaiya Can you make video on timing of HackerRank Contests Through which Product Based Comp
    Hire

  • @prakharjain3211
    @prakharjain3211 3 роки тому

    Please tell the function call statements

  • @parikshitrathore1510
    @parikshitrathore1510 4 роки тому

    Bhai aapki video private kyon ho gayi.. QNA wali ek ghante pahele aayi thi??

  • @technologybaba192
    @technologybaba192 3 місяці тому

    Understood

  • @priyankarrajgupta4198
    @priyankarrajgupta4198 4 роки тому

    There is no code link?

  • @vaalarivan_p
    @vaalarivan_p Рік тому

    0:00 - 1:15
    6:29 -

  • @nishantmohanty2147
    @nishantmohanty2147 4 роки тому

    Sir when will we get the update of cp 5 gfg live classes.

  • @ravi4519
    @ravi4519 3 роки тому

    Code link bro

  • @manikantabandla3923
    @manikantabandla3923 3 роки тому

    Please don't drag the words

  • @daruiraikage
    @daruiraikage 4 роки тому

    bro QnA video public kardo, i joined in late

  • @AslamMahmudSiam
    @AslamMahmudSiam 4 роки тому

    How to use segmented tree in 10^9 range

  • @lakhannad
    @lakhannad 4 роки тому

    Awesome Video😀😀

  • @gamingwitheng1459
    @gamingwitheng1459 4 роки тому +5

    Bhaiya tcs digital par koi banao

  • @Gaurav_63
    @Gaurav_63 2 місяці тому

    vo sab to thik hai lekin ye bol kese raha hai so funny lol

  • @AshwaniKumar-tw8vp
    @AshwaniKumar-tw8vp 3 роки тому

    BINGO

  • @chirayuchoudhary9727
    @chirayuchoudhary9727 6 місяців тому

    bhai accent mei mt bol

  • @umangmalhotra1222
    @umangmalhotra1222 3 місяці тому

    Abe yaar ye kese bolta hai, chu

  • @hwaiting4696
    @hwaiting4696 2 роки тому +5

    C++ Working Code
    #include
    using namespace std;
    // TC: O(NlogN);
    // SC: 2*(4*N) ~ O(N);
    // Segement Trees : Point Update & Range Update
    int n; // size of the array...
    vector arr, segment, lazy;
    void buildSegementTree(int ind, int left, int right){
    // Base Case
    if(left == right){
    segment[ind] = arr[left];
    return ;
    }
    int mid = (left + right) / 2;
    buildSegementTree(2*ind+1, left, mid);
    buildSegementTree(2*ind+2, mid+1, right);
    segment[ind] = segment[2*ind+1] + segment[2*ind+2];
    }
    void pointUpdate(int ind, int left, int right, int node, int val){
    if(left == right){
    arr[left] += val;
    segment[ind] += val;
    }else{
    int mid = (left + right)/2;
    if(left =l && right right) return 0;
    // If range is completely inside...
    if(left >= l && right

    • @faiazmahmud6277
      @faiazmahmud6277 3 місяці тому

      can you pls explain why the arr[left]+=x part in pointupdate..is it necessary ? i mean we are not using the array anywhere except in the building part