Problem in Offline Queries of seg tree

https://codeforces.com/contest/301/problem/D

Please share a seg tree code for it and also explain the approch