help in https://www.codechef.com/problems/CHEFPRES

can anyone please help me to understand how to do it in less than O(N^2) …i have read the editorial but couldn’t understand that part! please help me

//