Roads in chefland

Can anyone tell, why i am getting time limit exceeded
Roads In Chefland - https://www.codechef.com/viewsolution/33314181

You have written a loop iterating j from 1 to n. As n is 1e9, the process takes more than 1 sec. Hence, TLE.

okay, thanks
can we done it into log(n) time.

yes,it can be done in O(log n) time per test case.