Library display shelves feature one book at a time, and staff log every change.
Implement class ShelfLog:
put(shelf, book, t): from timeton,shelffeaturesbook.clear(shelf, t): from timeton,shelfis empty.at(shelf, t) -> str | None: the book from the latestput/clearon that shelf at or beforet;Noneif that was aclearor there's no change yet.featured(shelf, t1, t2) -> list[str]: every book featured at any moment in[t1, t2], in put order: the book att1(if any), then everyputwitht1 < time <= t2. Repeats count.
Each put/clear has a time strictly greater than all earlier ones. Queries may ask about any time, even negative.
log = ShelfLog()
log.put("window", "Dune", 10)
log.put("window", "Emma", 20)
log.clear("window", 30)
log.put("window", "Dune", 40)
log.at("window", 25) # "Emma"
log.at("window", 35) # None (cleared at 30)
log.at("window", 5) # None (nothing yet)
log.featured("window", 15, 40) # ["Dune", "Emma", "Dune"]
log.featured("window", 30, 39) # [] (empty the whole time)
log.featured("lobby", 0, 100) # []
⭐ Bonus: scanning a shelf's history per query still passes; a speed test with 100,000 changes and 100,000 queries earns a star at O(log n) per at (plus the answer's size for featured).
Show hint
per shelf, keep change times and a parallel list of books (None for a clear) and use bisect_right; featured is a slice from the change in effect at t1.