Hints: Looking at Towers (difficult version)
The algorithmic idea is identical to E1. The only difference is the constraints ($n \le 3 \times 10^5$ instead of $n \le 5000$), which demands an $O(n \log n)$ implementation. Read the E1 hints first if you haven’t.