Difficulty Ratings Idea Creativity: 2/5 Techniques Used: 3/5 Implementation : 3/5 Link Simplified Chinese Japansese by semiexp Statement Imagine you have a range $[A,B]$. An operation is moving to left into $[A-1,B-1]$ using 1 cost, or moving to right into $[A+1,B+1]$ using 1 cost. You can do any operations at any number of times and each operation takes 0 time. Then, there will be $N$ events happening, in event $i$ you have to move your range to cover a point $x_i$. Let $f([A,B])$ be the minimal cost to handle the $N$ events. Given $Q$ queries of $[A_j,B_j]$, find out $f([A_j,B_j])$ for each of them. $N,Q \le 2e5$. Solution So I assume you realise that moving greedily (move towards the point you have to cover if you haven't) is optimal. Firstly, lets compare $f(0,B-A)$ and $f(A,B)$. Let's find the first position $i$ such that there exist $j < i$ that $|x_i-x_j|>B-A$. Two observations are to be made: 1) You will end up at the same position after the