https://codeforces.com/contest/301/problem/D
Please share a seg tree code for it and also explain the approch