Segment Tree Implementation of this problem(

I am new to segment trees and was trying this problem. After implementing the tree and getting correct answer on the given case, I submitted my solution ( but it gave wrong answer.The same code gives correct answer on Hackerearth( Please point out if I am doing something wrong