Roads in chefland

Can anyone tell, why i am getting time limit exceeded
Roads In Chefland - CodeChef: Practical coding for everyone

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.