@skpro19 if you get the logic then please explain it. it will be a great help 
@taran_1407 thanks a lot!
one more thing please…it is written : “(min and max on the segment in the segment tree, dp[2][2], dp[2][2] and other things)”. What is the need to store min and max? We can calculate dp values by hand at base level and thus can combine them to get answrs for higher level. so, what is the need for them?
and last thing i have understood how to add elements(just add a node at last) but how to support erase operations? is min and max is something that requires for this one?
Mate, i haven’t myself implemented the solution yet. I knew about 2nd observation, so i wrote the proof here…
okay thanks taran but one request if you implement it in future please provide the solution link.
If i implement, i will post solution link.