Marco Melandri left it late to clinch top spot at the end of the second qualifying session ahead of the World Superbike race at Laguna Seca.