Approach for Tree Sequence(TSEQ) of Aug Long

Can someone please share approach for [TSEQ] ?(https://www.codechef.com/AUG19B/problems/TSEQ)

I tried HLD + segment trees but got WA :frowning: