The Olympics will remain on the BBC until at least 2024 after the corporation agreed a deal with the US broadcasting giant Discovery to sub-licence the rights.