B. Heidi and Library (medium)
data structures; greedy
Whereas humans nowadays read fewer and fewer books on paper, book readership among marmots has surged. Heidi has expanded the library and is now serving longer request sequences.
Same as the easy version, but the limits have changed: 1 ≤ n, k ≤ 400 000.
Same as the easy version.