you can find the problem statement here.
this question is just a modified/difficult model of spoj pebble.
as they introduced an extra condition k, I solved it using segment tree with lazy propagation. O(n*log(n))
but we can also solve it in O(n) as mentioned in its codechef-editorial.