Xenie and trees

Codeforces to solve it using segment tree.The editorial says about sqrt decomposition but I am not able to get it.Please someone teach me how to solve it using segment tree.Please use pseudocode if possible

https://cp-algorithms.com/data_structures/sqrt_decomposition.html

you can go through this for better understanding

I know sqrt decomposition but I am not able to figure out how to apply segemnt tree for the problem .

its a famous/classical problem which can be solved by centroid decomposition
explained over here- https://www.quora.com/q/threadsiiithyderabad/Centroid-Decomposition-of-a-Tree