Books Exchange (hard version)

Problem Name:
Books Exchange (hard version)
Site:CodeForces
Links:
Tags:
Problem setters: